From 2c010ffa06631426dbc594d9333d06cd20493661 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Sun, 2 Apr 2023 23:58:28 -0700 Subject: [PATCH] Registry refactors and namespaced backends --- .../java/com/jozufozu/flywheel/Flywheel.java | 32 ++-- .../flywheel/api/backend/Backend.java | 26 +++ .../flywheel/api/backend/BackendManager.java | 38 +++++ .../flywheel/api/backend/BackendType.java | 24 --- .../jozufozu/flywheel/api/backend/Engine.java | 4 +- .../api/component/ComponentRegistry.java | 140 ---------------- .../flywheel/api/component/ComponentType.java | 9 -- .../flywheel/api/context/Context.java | 4 + .../instance}/InstancedRenderRegistry.java | 152 +----------------- .../BlockEntityInstancingController.java | 9 +- .../entity/EntityInstancingController.java | 7 +- .../api/instancer/InstancerManager.java | 20 --- .../api/instancer/InstancerProvider.java | 19 +++ .../flywheel/api/layout/BufferLayout.java | 1 - .../{lib => api}/layout/LayoutItem.java | 3 +- .../flywheel/api/material/Material.java | 4 + .../com/jozufozu/flywheel/api/model/Mesh.java | 2 +- .../flywheel/api/pipeline/Pipeline.java | 1 + .../flywheel/api/registry/IdRegistry.java | 33 ++++ .../flywheel/api/registry/Registry.java | 20 +++ .../api/struct/StorageBufferWriter.java | 10 -- .../flywheel/api/struct/StructType.java | 3 + .../flywheel/api/uniform/ShaderUniforms.java | 4 + .../api/vertex/VertexListProvider.java | 2 +- .../flywheel/api/vertex/VertexType.java | 3 + .../jozufozu/flywheel/backend/Backend.java | 114 ------------- .../flywheel/backend/BackendUtil.java | 57 +++++++ .../com/jozufozu/flywheel/backend/Loader.java | 28 ++-- .../flywheel/backend/compile/Compilation.java | 9 +- .../flywheel/backend/compile/CompileUtil.java | 4 +- .../backend/compile/CullingContextSet.java | 3 +- .../flywheel/backend/compile/FlwCompiler.java | 15 +- .../flywheel/backend/compile/Includer.java | 2 +- .../compile/MaterialAdapterComponent.java | 2 +- .../backend/compile/PipelineContextSet.java | 30 ++-- .../backend/compile/RecursiveIncluder.java | 2 +- .../backend/compile/ShaderCompiler.java | 2 +- .../StringSubstitutionSourceComponent.java | 2 +- .../backend/compile/UniformComponent.java | 2 +- .../engine/batching/BatchingEngine.java | 2 +- .../engine/indirect/IndirectComponent.java | 4 +- .../engine/indirect/IndirectCullingGroup.java | 5 +- .../backend/engine/indirect/IndirectDraw.java | 6 +- .../engine/indirect/IndirectDrawSet.java | 4 +- .../engine/indirect/IndirectEngine.java | 2 +- .../instancing/InstancedArraysComponent.java | 4 +- .../engine/instancing/InstancedMeshPool.java | 5 +- .../engine/instancing/InstancingEngine.java | 11 +- .../backend/instancing/AbstractInstance.java | 6 +- .../backend/instancing/InstanceManager.java | 14 +- .../backend/instancing/InstanceWorld.java | 7 +- .../instancing/InstancedRenderDispatcher.java | 21 +-- .../blockentity/BlockEntityInstance.java | 4 +- .../BlockEntityInstanceManager.java | 12 +- ...SimpleBlockEntityInstancingController.java | 28 ---- .../backend/instancing/effect/Effect.java | 4 +- .../effect/EffectInstanceManager.java | 6 +- .../instancing/entity/EntityInstance.java | 4 +- .../entity/EntityInstanceManager.java | 10 +- .../SimpleEntityInstancingController.java | 28 ---- .../instancing/storage/AbstractStorage.java | 6 +- .../instancing/storage/One2OneStorage.java | 4 +- .../backend/uniform/UniformBuffer.java | 13 +- ...TypeArgument.java => BackendArgument.java} | 43 ++--- .../jozufozu/flywheel/config/FlwCommands.java | 46 +++--- .../jozufozu/flywheel/config/FlwConfig.java | 52 ++++-- .../extension/BlockEntityTypeExtension.java | 2 +- .../extension/EntityTypeExtension.java | 2 +- .../buffer}/ElementBuffer.java | 3 +- .../flywheel/gl/shader/GlProgram.java | 11 +- .../pipeline => glsl}/SourceComponent.java | 2 +- .../jozufozu/flywheel/glsl/SourceFile.java | 1 - .../flywheel/glsl/error/ErrorReporter.java | 7 +- .../flywheel/glsl/error/package-info.java | 6 - .../flywheel/handler/EntityWorldHandler.java | 7 +- .../flywheel/handler/ForgeEvents.java | 4 +- .../flywheel/impl/BackendManagerImpl.java | 56 +++++++ .../flywheel/impl/IdRegistryImpl.java | 117 ++++++++++++++ .../jozufozu/flywheel/impl/RegistryImpl.java | 101 ++++++++++++ .../vertex/InferredVertexFormatInfo.java | 2 +- .../vertex/InferredVertexListImpl.java | 2 +- .../InferredVertexListProviderImpl.java | 2 +- .../{BackendTypes.java => Backends.java} | 75 ++------- ...pleBackendType.java => SimpleBackend.java} | 38 ++--- .../flywheel/lib/context/Contexts.java | 6 +- .../jozufozu/flywheel/lib/format/Formats.java | 6 +- ...SimpleBlockEntityInstancingController.java | 100 ++++++++++++ .../SimpleEntityInstancingController.java | 100 ++++++++++++ .../flywheel/lib/light/LightUpdater.java | 6 +- .../lib/material/MaterialIndicies.java | 147 +++++++++++++++++ .../flywheel/lib/material/SimpleMaterial.java | 3 +- .../flywheel/lib/pipeline/SimplePipeline.java | 2 +- .../flywheel/lib/struct/StructTypes.java | 5 +- .../{backend => lib}/task/WorkGroup.java | 2 +- .../lib/uniform/FlwShaderUniforms.java | 3 +- .../flywheel/lib/util/QuadConverter.java | 2 +- .../flywheel/lib/util/ShadersModHandler.java | 12 +- .../flywheel/mixin/BlockEntityTypeMixin.java | 2 +- .../flywheel/mixin/ClientLevelMixin.java | 6 +- .../flywheel/mixin/EntityTypeMixin.java | 2 +- .../flywheel/mixin/LevelRendererMixin.java | 4 +- .../ChunkRebuildHooksMixin.java | 6 +- .../instancemanage/InstanceAddMixin.java | 4 +- .../instancemanage/InstanceRemoveMixin.java | 4 +- .../instancemanage/InstanceUpdateMixin.java | 4 +- .../flywheel/vanilla/BellInstance.java | 6 +- .../flywheel/vanilla/ChestInstance.java | 8 +- .../flywheel/vanilla/MinecartInstance.java | 8 +- .../flywheel/vanilla/ShulkerBoxInstance.java | 8 +- .../flywheel/vanilla/VanillaInstances.java | 3 +- .../vanilla/effect/ExampleEffect.java | 8 +- 111 files changed, 1214 insertions(+), 884 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/api/backend/Backend.java create mode 100644 src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java delete mode 100644 src/main/java/com/jozufozu/flywheel/api/backend/BackendType.java delete mode 100644 src/main/java/com/jozufozu/flywheel/api/component/ComponentRegistry.java delete mode 100644 src/main/java/com/jozufozu/flywheel/api/component/ComponentType.java rename src/main/java/com/jozufozu/flywheel/{backend/instancing => api/instance}/InstancedRenderRegistry.java (56%) rename src/main/java/com/jozufozu/flywheel/{backend/instancing => api/instance}/blockentity/BlockEntityInstancingController.java (69%) rename src/main/java/com/jozufozu/flywheel/{backend/instancing => api/instance}/entity/EntityInstancingController.java (71%) delete mode 100644 src/main/java/com/jozufozu/flywheel/api/instancer/InstancerManager.java create mode 100644 src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java rename src/main/java/com/jozufozu/flywheel/{lib => api}/layout/LayoutItem.java (84%) create mode 100644 src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java create mode 100644 src/main/java/com/jozufozu/flywheel/api/registry/Registry.java delete mode 100644 src/main/java/com/jozufozu/flywheel/api/struct/StorageBufferWriter.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/Backend.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/SimpleBlockEntityInstancingController.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/instancing/entity/SimpleEntityInstancingController.java rename src/main/java/com/jozufozu/flywheel/config/{BackendTypeArgument.java => BackendArgument.java} (59%) rename src/main/java/com/jozufozu/flywheel/{backend/engine/instancing => gl/buffer}/ElementBuffer.java (84%) rename src/main/java/com/jozufozu/flywheel/{api/pipeline => glsl}/SourceComponent.java (83%) delete mode 100644 src/main/java/com/jozufozu/flywheel/glsl/error/package-info.java create mode 100644 src/main/java/com/jozufozu/flywheel/impl/BackendManagerImpl.java create mode 100644 src/main/java/com/jozufozu/flywheel/impl/IdRegistryImpl.java create mode 100644 src/main/java/com/jozufozu/flywheel/impl/RegistryImpl.java rename src/main/java/com/jozufozu/flywheel/{backend => impl}/vertex/InferredVertexFormatInfo.java (97%) rename src/main/java/com/jozufozu/flywheel/{backend => impl}/vertex/InferredVertexListImpl.java (99%) rename src/main/java/com/jozufozu/flywheel/{backend => impl}/vertex/InferredVertexListProviderImpl.java (92%) rename src/main/java/com/jozufozu/flywheel/lib/backend/{BackendTypes.java => Backends.java} (52%) rename src/main/java/com/jozufozu/flywheel/lib/backend/{SimpleBackendType.java => SimpleBackend.java} (67%) create mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/SimpleBlockEntityInstancingController.java create mode 100644 src/main/java/com/jozufozu/flywheel/lib/instance/SimpleEntityInstancingController.java create mode 100644 src/main/java/com/jozufozu/flywheel/lib/material/MaterialIndicies.java rename src/main/java/com/jozufozu/flywheel/{backend => lib}/task/WorkGroup.java (97%) diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 2a6aade83..d56c37768 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -3,17 +3,21 @@ package com.jozufozu.flywheel; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.slf4j.Logger; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.backend.BackendManager; +import com.jozufozu.flywheel.backend.Loader; import com.jozufozu.flywheel.backend.engine.batching.DrawBuffer; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; -import com.jozufozu.flywheel.config.BackendTypeArgument; +import com.jozufozu.flywheel.config.BackendArgument; import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.handler.EntityWorldHandler; import com.jozufozu.flywheel.handler.ForgeEvents; -import com.jozufozu.flywheel.lib.backend.BackendTypes; +import com.jozufozu.flywheel.impl.IdRegistryImpl; +import com.jozufozu.flywheel.impl.RegistryImpl; +import com.jozufozu.flywheel.lib.backend.Backends; import com.jozufozu.flywheel.lib.context.Contexts; import com.jozufozu.flywheel.lib.format.Formats; +import com.jozufozu.flywheel.lib.material.MaterialIndicies; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.Models; import com.jozufozu.flywheel.lib.model.PartialModel; @@ -61,7 +65,7 @@ public class Flywheel { .getModEventBus(); modEventBus.addListener(Flywheel::setup); - FlwConfig.init(); + FlwConfig.get().registerSpecs(modLoadingContext); modLoadingContext.registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest( () -> NetworkConstants.IGNORESERVERONLY, @@ -72,12 +76,6 @@ public class Flywheel { } private static void clientInit(IEventBus forgeEventBus, IEventBus modEventBus) { - CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor); - - ShadersModHandler.init(); - BackendTypes.init(); - Backend.init(); - forgeEventBus.addListener(FlwCommands::registerClientCommands); forgeEventBus.addListener(EventPriority.HIGHEST, QuadConverter::onReloadRenderers); @@ -102,14 +100,23 @@ public class Flywheel { // forgeEventBus.addListener(ExampleEffect::tick); // forgeEventBus.addListener(ExampleEffect::onReload); + ShadersModHandler.init(); + + Backends.init(); + Loader.init(); + Formats.init(); StructTypes.init(); Materials.init(); Contexts.init(); Pipelines.init(); + MaterialIndicies.init(); + VanillaInstances.init(); + CrashReportCallables.registerCrashCallable("Flywheel Backend", BackendManager::getBackendDescriptor); + // https://github.com/Jozufozu/Flywheel/issues/69 // Weird issue with accessor loading. // Only thing I've seen that's close to a fix is to force the class to load before trying to use it. @@ -119,7 +126,10 @@ public class Flywheel { } private static void setup(final FMLCommonSetupEvent event) { - ArgumentTypes.register(rl("engine").toString(), BackendTypeArgument.class, new EmptyArgumentSerializer<>(BackendTypeArgument::getInstance)); + ArgumentTypes.register(rl("backend").toString(), BackendArgument.class, new EmptyArgumentSerializer<>(BackendArgument::getInstance)); + + RegistryImpl.freezeAll(); + IdRegistryImpl.freezeAll(); } public static ArtifactVersion getVersion() { diff --git a/src/main/java/com/jozufozu/flywheel/api/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/api/backend/Backend.java new file mode 100644 index 000000000..aec544945 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/backend/Backend.java @@ -0,0 +1,26 @@ +package com.jozufozu.flywheel.api.backend; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.api.pipeline.Pipeline; +import com.jozufozu.flywheel.api.registry.IdRegistry; +import com.jozufozu.flywheel.impl.IdRegistryImpl; + +import net.minecraft.network.chat.Component; + +public interface Backend { + static IdRegistry REGISTRY = IdRegistryImpl.create(); + + // TODO: remove and use ID instead? Currently this is only used for the crash log string. + String getProperName(); + + Component getEngineMessage(); + + Engine createEngine(); + + Backend findFallback(); + + boolean isSupported(); + + @Nullable Pipeline pipelineShader(); +} diff --git a/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java b/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java new file mode 100644 index 000000000..71fa52a21 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java @@ -0,0 +1,38 @@ +package com.jozufozu.flywheel.api.backend; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.impl.BackendManagerImpl; + +public final class BackendManager { + /** + * Get the current backend. + */ + @Nullable + public static Backend getBackend() { + return BackendManagerImpl.getBackend(); + } + + /** + * Get a string describing the current backend. + */ + public static String getBackendDescriptor() { + return BackendManagerImpl.getBackendDescriptor(); + } + + public static boolean isOn() { + return BackendManagerImpl.isOn(); + } + + // TODO: definitively sort existing calls to this method into API (include behavior in javadoc) or default backend code + public static void refresh() { + BackendManagerImpl.refresh(); + } + + public static Backend getDefaultBackend() { + return BackendManagerImpl.getDefaultBackend(); + } + + private BackendManager() { + } +} diff --git a/src/main/java/com/jozufozu/flywheel/api/backend/BackendType.java b/src/main/java/com/jozufozu/flywheel/api/backend/BackendType.java deleted file mode 100644 index 46a8bd2a2..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/backend/BackendType.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jozufozu.flywheel.api.backend; - -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.pipeline.Pipeline; - -import net.minecraft.network.chat.Component; - -public interface BackendType { - - String getProperName(); - - String getShortName(); - - Component getEngineMessage(); - - Engine createEngine(); - - BackendType findFallback(); - - boolean supported(); - - @Nullable Pipeline pipelineShader(); -} diff --git a/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java b/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java index 6a58260e8..5c43fec26 100644 --- a/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java +++ b/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java @@ -2,10 +2,10 @@ package com.jozufozu.flywheel.api.backend; import java.util.List; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.InstanceManager; -public interface Engine extends RenderDispatcher, InstancerManager { +public interface Engine extends RenderDispatcher, InstancerProvider { void attachManagers(InstanceManager... listener); void addDebugInfo(List info); diff --git a/src/main/java/com/jozufozu/flywheel/api/component/ComponentRegistry.java b/src/main/java/com/jozufozu/flywheel/api/component/ComponentRegistry.java deleted file mode 100644 index 5371bd73e..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/component/ComponentRegistry.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.jozufozu.flywheel.api.component; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.context.Context; -import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.api.struct.StructType; -import com.jozufozu.flywheel.api.uniform.ShaderUniforms; -import com.jozufozu.flywheel.api.vertex.VertexType; - -import net.minecraft.resources.ResourceLocation; - -public class ComponentRegistry { - private static final Registry uniformProviders = new Registry<>(); - - public static final MaterialRegistry materials = new MaterialRegistry(); - public static final Set> structTypes = new HashSet<>(); - public static final Set vertexTypes = new HashSet<>(); - public static final Set contextShaders = new HashSet<>(); - - // TODO: fill out the rest of the registry - - public static T register(T material) { - return materials.add(material); - } - - public static > T register(T type) { - structTypes.add(type); - return type; - } - - public static T register(T vertexType) { - vertexTypes.add(vertexType); - return vertexType; - } - - public static T register(T contextShader) { - contextShaders.add(contextShader); - return contextShader; - } - - public static T register(T provider) { - return uniformProviders.register(provider.uniformShader(), provider); - } - - public static Collection getAllUniformProviders() { - return Collections.unmodifiableCollection(uniformProviders.objects); - } - - @Nullable - public static ShaderUniforms getUniformProvider(ResourceLocation loc) { - return uniformProviders.get(loc); - } - - private static class Registry { - private final Map files = new HashMap<>(); - private final List objects = new ArrayList<>(); - - public O register(ResourceLocation loc, O object) { - if (files.containsKey(loc)) { - throw new IllegalArgumentException("Shader file already registered: " + loc); - } - files.put(loc, object); - objects.add(object); - return object; - } - - @Nullable - public T get(ResourceLocation loc) { - return files.get(loc); - } - } - - public static class MaterialRegistry { - - private final Set materials = new HashSet<>(); - private final List materialsOrdered = new ArrayList<>(); - private final MaterialSources vertexSources = new MaterialSources(); - private final MaterialSources fragmentSources = new MaterialSources(); - - public T add(T material) { - if (materials.add(material)) { - materialsOrdered.add(material); - vertexSources.register(material.vertexShader()); - fragmentSources.register(material.fragmentShader()); - } else { - throw new IllegalArgumentException("Material already registered: " + material); - } - - return material; - } - - /** - * @return a list of vertex shader sources where the index in the list is the shader's ID. - */ - public List vertexSources() { - return vertexSources.sourceView; - } - - /** - * @return a list of fragment shader sources where the index in the list is the shader's ID. - */ - public List fragmentSources() { - return fragmentSources.sourceView; - } - - public int getVertexID(Material material) { - return vertexSources.orderedSources.indexOf(material.vertexShader()); - } - - public int getFragmentID(Material material) { - return fragmentSources.orderedSources.indexOf(material.fragmentShader()); - } - - public int getMaterialID(Material material) { - return materialsOrdered.indexOf(material); - } - - private static class MaterialSources { - private final Set registered = new HashSet<>(); - private final List orderedSources = new ArrayList<>(); - private final List sourceView = Collections.unmodifiableList(orderedSources); - - public void register(ResourceLocation vertexShader) { - if (registered.add(vertexShader)) { - orderedSources.add(vertexShader); - } - } - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/api/component/ComponentType.java b/src/main/java/com/jozufozu/flywheel/api/component/ComponentType.java deleted file mode 100644 index 0ff129c9b..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/component/ComponentType.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.jozufozu.flywheel.api.component; - -public enum ComponentType { - MATERIAL, - INSTANCE, - LAYOUT, - CONTEXT, - UNIFORM_PROVIDER -} diff --git a/src/main/java/com/jozufozu/flywheel/api/context/Context.java b/src/main/java/com/jozufozu/flywheel/api/context/Context.java index 5be86a3cc..fe60f9294 100644 --- a/src/main/java/com/jozufozu/flywheel/api/context/Context.java +++ b/src/main/java/com/jozufozu/flywheel/api/context/Context.java @@ -1,10 +1,14 @@ package com.jozufozu.flywheel.api.context; +import com.jozufozu.flywheel.api.registry.Registry; import com.jozufozu.flywheel.gl.shader.GlProgram; +import com.jozufozu.flywheel.impl.RegistryImpl; import net.minecraft.resources.ResourceLocation; public interface Context { + static Registry REGISTRY = RegistryImpl.create(); + void onProgramLink(GlProgram program); ResourceLocation vertexShader(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java b/src/main/java/com/jozufozu/flywheel/api/instance/InstancedRenderRegistry.java similarity index 56% rename from src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java rename to src/main/java/com/jozufozu/flywheel/api/instance/InstancedRenderRegistry.java index dcbfbc45d..92b5bd487 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/InstancedRenderRegistry.java @@ -1,18 +1,12 @@ -package com.jozufozu.flywheel.backend.instancing; - -import java.util.Objects; -import java.util.function.BiFunction; -import java.util.function.Predicate; +package com.jozufozu.flywheel.api.instance; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instance.blockentity.BlockEntityInstancingController; +import com.jozufozu.flywheel.api.instance.entity.EntityInstancingController; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; -import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstancingController; -import com.jozufozu.flywheel.backend.instancing.blockentity.SimpleBlockEntityInstancingController; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; -import com.jozufozu.flywheel.backend.instancing.entity.EntityInstancingController; -import com.jozufozu.flywheel.backend.instancing.entity.SimpleEntityInstancingController; import com.jozufozu.flywheel.extension.BlockEntityTypeExtension; import com.jozufozu.flywheel.extension.EntityTypeExtension; @@ -54,7 +48,7 @@ public class InstancedRenderRegistry { * @return An instance of the block entity, or {@code null} if the block entity cannot be instanced. */ @Nullable - public static BlockEntityInstance createInstance(InstancerManager instancerManager, T blockEntity) { + public static BlockEntityInstance createInstance(InstancerProvider instancerManager, T blockEntity) { BlockEntityInstancingController controller = getController(getType(blockEntity)); if (controller == null) { return null; @@ -70,7 +64,7 @@ public class InstancedRenderRegistry { * @return An instance of the entity, or {@code null} if the entity cannot be instanced. */ @Nullable - public static EntityInstance createInstance(InstancerManager instancerManager, T entity) { + public static EntityInstance createInstance(InstancerProvider instancerManager, T entity) { EntityInstancingController controller = getController(getType(entity)); if (controller == null) { return null; @@ -106,26 +100,6 @@ public class InstancedRenderRegistry { return controller.shouldSkipRender(entity); } - /** - * Get an object to configure the instancing controller for the given block entity type. - * @param type The block entity type to configure. - * @param The type of the block entity. - * @return The configuration object. - */ - public static BlockEntityConfig configure(BlockEntityType type) { - return new BlockEntityConfig<>(type); - } - - /** - * Get an object to configure the instancing controller for the given entity type. - * @param type The entity type to configure. - * @param The type of the entity. - * @return The configuration object. - */ - public static EntityConfig configure(EntityType type) { - return new EntityConfig<>(type); - } - /** * Gets the instancing controller for the given block entity type, if one exists. * @param type The block entity type to get the instancing controller for. @@ -187,118 +161,4 @@ public class InstancedRenderRegistry { public static EntityType getType(T entity) { return (EntityType) entity.getType(); } - - /** - * An object to configure the instancing controller for a block entity. - * @param The type of the block entity. - */ - public static class BlockEntityConfig { - protected BlockEntityType type; - protected BiFunction> instanceFactory; - protected Predicate skipRender; - - public BlockEntityConfig(BlockEntityType type) { - this.type = type; - } - - /** - * Sets the instance factory for the block entity. - * @param instanceFactory The instance factory. - * @return {@code this} - */ - public BlockEntityConfig factory(BiFunction> instanceFactory) { - this.instanceFactory = instanceFactory; - return this; - } - - /** - * Sets a predicate to determine whether to skip rendering a block entity. - * @param skipRender The predicate. - * @return {@code this} - */ - public BlockEntityConfig skipRender(Predicate skipRender) { - this.skipRender = skipRender; - return this; - } - - /** - * Sets a predicate to always skip rendering for block entities of this type. - * @return {@code this} - */ - public BlockEntityConfig alwaysSkipRender() { - this.skipRender = be -> true; - return this; - } - - /** - * Constructs the block entity instancing controller, and sets it for the block entity type. - * @return The block entity instancing controller. - */ - public SimpleBlockEntityInstancingController apply() { - Objects.requireNonNull(instanceFactory, "Instance factory cannot be null!"); - if (skipRender == null) { - skipRender = be -> false; - } - SimpleBlockEntityInstancingController controller = new SimpleBlockEntityInstancingController<>(instanceFactory, skipRender); - setController(type, controller); - return controller; - } - } - - /** - * An object to configure the instancing controller for an entity. - * @param The type of the entity. - */ - public static class EntityConfig { - protected EntityType type; - protected BiFunction> instanceFactory; - protected Predicate skipRender; - - public EntityConfig(EntityType type) { - this.type = type; - } - - /** - * Sets the instance factory for the entity. - * @param instanceFactory The instance factory. - * @return {@code this} - */ - public EntityConfig factory(BiFunction> instanceFactory) { - this.instanceFactory = instanceFactory; - return this; - } - - /** - * Sets a predicate to determine whether to skip rendering an entity. - * @param skipRender The predicate. - * @return {@code this} - */ - public EntityConfig skipRender(Predicate skipRender) { - this.skipRender = skipRender; - return this; - } - - /** - * Sets a predicate to always skip rendering for entities of this type. - * @return {@code this} - */ - public EntityConfig alwaysSkipRender() { - this.skipRender = entity -> true; - return this; - } - - /** - * Constructs the entity instancing controller, and sets it for the entity type. - * @return The entity instancing controller. - */ - public SimpleEntityInstancingController apply() { - Objects.requireNonNull(instanceFactory, "Instance factory cannot be null!"); - if (skipRender == null) { - skipRender = entity -> false; - } - SimpleEntityInstancingController controller = new SimpleEntityInstancingController<>(instanceFactory, skipRender); - setController(type, controller); - return controller; - } - } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstancingController.java b/src/main/java/com/jozufozu/flywheel/api/instance/blockentity/BlockEntityInstancingController.java similarity index 69% rename from src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstancingController.java rename to src/main/java/com/jozufozu/flywheel/api/instance/blockentity/BlockEntityInstancingController.java index 37d0b8c6c..f8d8d93b7 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstancingController.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/blockentity/BlockEntityInstancingController.java @@ -1,12 +1,13 @@ -package com.jozufozu.flywheel.backend.instancing.blockentity; +package com.jozufozu.flywheel.api.instance.blockentity; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; +import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; import net.minecraft.world.level.block.entity.BlockEntity; /** * An instancing controller that will be keyed to a block entity type. - * @param The type of block entity this controller is for. + * @param The block entity type. */ public interface BlockEntityInstancingController { /** @@ -15,7 +16,7 @@ public interface BlockEntityInstancingController { * @param blockEntity The block entity to construct an instance for. * @return The instance. */ - BlockEntityInstance createInstance(InstancerManager instancerManager, T blockEntity); + BlockEntityInstance createInstance(InstancerProvider instancerManager, T blockEntity); /** * Checks if the given block entity should not be rendered normally. diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstancingController.java b/src/main/java/com/jozufozu/flywheel/api/instance/entity/EntityInstancingController.java similarity index 71% rename from src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstancingController.java rename to src/main/java/com/jozufozu/flywheel/api/instance/entity/EntityInstancingController.java index b86c8f005..3bc331a6f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstancingController.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/entity/EntityInstancingController.java @@ -1,6 +1,7 @@ -package com.jozufozu.flywheel.backend.instancing.entity; +package com.jozufozu.flywheel.api.instance.entity; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; +import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; import net.minecraft.world.entity.Entity; @@ -15,7 +16,7 @@ public interface EntityInstancingController { * @param entity The entity to construct an instance for. * @return The instance. */ - EntityInstance createInstance(InstancerManager instancerManager, T entity); + EntityInstance createInstance(InstancerProvider instancerManager, T entity); /** * Checks if the given entity should not render normally. diff --git a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerManager.java b/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerManager.java deleted file mode 100644 index 552ba0be9..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerManager.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jozufozu.flywheel.api.instancer; - -import com.jozufozu.flywheel.api.event.RenderStage; -import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.api.struct.StructType; - -import net.minecraft.core.Vec3i; - -public interface InstancerManager { - - /** - * Get an instancer for the given struct type and model. Calling this method twice with the same arguments will return the same instancer. - * - * @return An instancer for the given struct type and model. - */ - Instancer instancer(StructType type, Model model, RenderStage stage); - - Vec3i getOriginCoordinate(); - -} diff --git a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java b/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java new file mode 100644 index 000000000..b2738c603 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java @@ -0,0 +1,19 @@ +package com.jozufozu.flywheel.api.instancer; + +import com.jozufozu.flywheel.api.event.RenderStage; +import com.jozufozu.flywheel.api.model.Model; +import com.jozufozu.flywheel.api.struct.StructType; + +import net.minecraft.core.Vec3i; + +public interface InstancerProvider { + /** + * Get an instancer for the given struct type, model, and render stage. Calling this method twice with the same arguments will return the same instancer. + * + * @return An instancer for the given struct type, model, and render stage. + */ + Instancer getInstancer(StructType type, Model model, RenderStage stage); + + // TODO: this method does not belong in the interface + Vec3i getOriginCoordinate(); +} diff --git a/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java b/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java index 285ed97ff..3a6b37aef 100644 --- a/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java +++ b/src/main/java/com/jozufozu/flywheel/api/layout/BufferLayout.java @@ -5,7 +5,6 @@ import java.util.List; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.gl.array.VertexAttribute; -import com.jozufozu.flywheel.lib.layout.LayoutItem; /** * Classic Vertex Format struct with a clever name. diff --git a/src/main/java/com/jozufozu/flywheel/lib/layout/LayoutItem.java b/src/main/java/com/jozufozu/flywheel/api/layout/LayoutItem.java similarity index 84% rename from src/main/java/com/jozufozu/flywheel/lib/layout/LayoutItem.java rename to src/main/java/com/jozufozu/flywheel/api/layout/LayoutItem.java index 3ddb86df7..bc3953c6b 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/layout/LayoutItem.java +++ b/src/main/java/com/jozufozu/flywheel/api/layout/LayoutItem.java @@ -1,6 +1,5 @@ -package com.jozufozu.flywheel.lib.layout; +package com.jozufozu.flywheel.api.layout; -import com.jozufozu.flywheel.api.layout.InputType; import com.jozufozu.flywheel.glsl.generate.GlslExpr; import com.jozufozu.flywheel.glsl.generate.GlslStruct; diff --git a/src/main/java/com/jozufozu/flywheel/api/material/Material.java b/src/main/java/com/jozufozu/flywheel/api/material/Material.java index f7462c7df..224ded5ef 100644 --- a/src/main/java/com/jozufozu/flywheel/api/material/Material.java +++ b/src/main/java/com/jozufozu/flywheel/api/material/Material.java @@ -1,12 +1,16 @@ package com.jozufozu.flywheel.api.material; +import com.jozufozu.flywheel.api.registry.Registry; import com.jozufozu.flywheel.api.vertex.MutableVertexList; +import com.jozufozu.flywheel.impl.RegistryImpl; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.renderer.RenderType; import net.minecraft.resources.ResourceLocation; public interface Material { + static Registry REGISTRY = RegistryImpl.create(); + ResourceLocation vertexShader(); ResourceLocation fragmentShader(); diff --git a/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java b/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java index 07787f5d1..0d637caa4 100644 --- a/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java +++ b/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java @@ -4,7 +4,7 @@ import org.joml.Vector4fc; import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.VertexType; -import com.jozufozu.flywheel.backend.engine.instancing.ElementBuffer; +import com.jozufozu.flywheel.gl.buffer.ElementBuffer; import com.jozufozu.flywheel.lib.util.QuadConverter; /** diff --git a/src/main/java/com/jozufozu/flywheel/api/pipeline/Pipeline.java b/src/main/java/com/jozufozu/flywheel/api/pipeline/Pipeline.java index df5eda7ad..bb30e85ec 100644 --- a/src/main/java/com/jozufozu/flywheel/api/pipeline/Pipeline.java +++ b/src/main/java/com/jozufozu/flywheel/api/pipeline/Pipeline.java @@ -4,6 +4,7 @@ import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.gl.GLSLVersion; import com.jozufozu.flywheel.glsl.ShaderSources; +import com.jozufozu.flywheel.glsl.SourceComponent; import net.minecraft.resources.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java b/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java new file mode 100644 index 000000000..6beb286b3 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/registry/IdRegistry.java @@ -0,0 +1,33 @@ +package com.jozufozu.flywheel.api.registry; + +import java.util.Collection; +import java.util.Set; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; + +import net.minecraft.resources.ResourceLocation; + +@ApiStatus.NonExtendable +public interface IdRegistry extends Iterable { + void register(ResourceLocation id, T object); + + S registerAndGet(ResourceLocation id, S object); + + @Nullable + T get(ResourceLocation id); + + @Nullable + ResourceLocation getId(T object); + + @Unmodifiable + Set getAllIds(); + + @Unmodifiable + Collection getAll(); + + void addFreezeCallback(Runnable callback); + + boolean isFrozen(); +} diff --git a/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java b/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java new file mode 100644 index 000000000..8683b684c --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/registry/Registry.java @@ -0,0 +1,20 @@ +package com.jozufozu.flywheel.api.registry; + +import java.util.Set; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Unmodifiable; + +@ApiStatus.NonExtendable +public interface Registry extends Iterable { + void register(T object); + + S registerAndGet(S object); + + @Unmodifiable + Set getAll(); + + void addFreezeCallback(Runnable callback); + + boolean isFrozen(); +} diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StorageBufferWriter.java b/src/main/java/com/jozufozu/flywheel/api/struct/StorageBufferWriter.java deleted file mode 100644 index 02eb42db9..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StorageBufferWriter.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.jozufozu.flywheel.api.struct; - -import com.jozufozu.flywheel.api.instancer.InstancedPart; - -public interface StorageBufferWriter { - - void write(final long ptr, final T instance); - - int getAlignment(); -} diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java index 37b2d678c..f867d4df2 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java @@ -2,7 +2,9 @@ package com.jozufozu.flywheel.api.struct; import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.layout.BufferLayout; +import com.jozufozu.flywheel.api.registry.Registry; import com.jozufozu.flywheel.api.vertex.MutableVertexList; +import com.jozufozu.flywheel.impl.RegistryImpl; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.resources.ResourceLocation; @@ -12,6 +14,7 @@ import net.minecraft.resources.ResourceLocation; * @param The java representation of the instance struct. */ public interface StructType { + static Registry> REGISTRY = RegistryImpl.create(); /** * @return A new, zeroed instance of S. diff --git a/src/main/java/com/jozufozu/flywheel/api/uniform/ShaderUniforms.java b/src/main/java/com/jozufozu/flywheel/api/uniform/ShaderUniforms.java index 535c775a8..723ddda93 100644 --- a/src/main/java/com/jozufozu/flywheel/api/uniform/ShaderUniforms.java +++ b/src/main/java/com/jozufozu/flywheel/api/uniform/ShaderUniforms.java @@ -1,8 +1,12 @@ package com.jozufozu.flywheel.api.uniform; +import com.jozufozu.flywheel.api.registry.Registry; +import com.jozufozu.flywheel.impl.RegistryImpl; + import net.minecraft.resources.ResourceLocation; public interface ShaderUniforms { + static Registry REGISTRY = RegistryImpl.createForShaderUniforms(); Provider activate(long ptr); diff --git a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexListProvider.java b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexListProvider.java index 4ed8e74b6..8a8428ab7 100644 --- a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexListProvider.java +++ b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexListProvider.java @@ -1,7 +1,7 @@ package com.jozufozu.flywheel.api.vertex; -import com.jozufozu.flywheel.backend.vertex.InferredVertexListProviderImpl; import com.jozufozu.flywheel.extension.VertexFormatExtension; +import com.jozufozu.flywheel.impl.vertex.InferredVertexListProviderImpl; import com.mojang.blaze3d.vertex.VertexFormat; public interface VertexListProvider { diff --git a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java index b8b2f4634..8ade48a11 100644 --- a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java +++ b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexType.java @@ -1,6 +1,8 @@ package com.jozufozu.flywheel.api.vertex; import com.jozufozu.flywheel.api.layout.BufferLayout; +import com.jozufozu.flywheel.api.registry.Registry; +import com.jozufozu.flywheel.impl.RegistryImpl; import net.minecraft.resources.ResourceLocation; @@ -8,6 +10,7 @@ import net.minecraft.resources.ResourceLocation; * A vertex type containing metadata about a specific vertex layout. */ public interface VertexType extends VertexListProvider { + static Registry REGISTRY = RegistryImpl.create(); /** * The layout of this type of vertex when buffered. diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java deleted file mode 100644 index 4bee70011..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.jozufozu.flywheel.backend; - -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; - -import com.jozufozu.flywheel.api.FlywheelLevel; -import com.jozufozu.flywheel.api.backend.BackendType; -import com.jozufozu.flywheel.backend.task.ParallelTaskExecutor; -import com.jozufozu.flywheel.config.FlwConfig; -import com.jozufozu.flywheel.lib.backend.BackendTypes; -import com.mojang.logging.LogUtils; - -import net.minecraft.client.Minecraft; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.LevelAccessor; - -public class Backend { - public static final Logger LOGGER = LogUtils.getLogger(); - - public static final boolean DUMP_SHADER_SOURCE = System.getProperty("flw.dumpShaderSource") != null; - - private static BackendType TYPE; - - private static ParallelTaskExecutor EXECUTOR; - - private static final Loader LOADER = new Loader(); - - /** - * Get the current Flywheel backend type. - */ - public static BackendType getBackendType() { - return TYPE; - } - - /** - * Get a thread pool for running Flywheel related work in parallel. - * @return A global Flywheel thread pool. - */ - public static ParallelTaskExecutor getTaskExecutor() { - if (EXECUTOR == null) { - EXECUTOR = new ParallelTaskExecutor("Flywheel"); - EXECUTOR.startWorkers(); - } - - return EXECUTOR; - } - - /** - * 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 TYPE == null ? "Uninitialized" : TYPE.getProperName(); - } - - public static void refresh() { - TYPE = chooseEngine(); - } - - public static boolean isOn() { - return TYPE != BackendTypes.OFF; - } - - @Contract("null -> false") - public static boolean canUseInstancing(@Nullable Level level) { - return isOn() && isFlywheelLevel(level); - } - - /** - * Used to avoid calling Flywheel functions on (fake) levels that don't specifically support it. - */ - public static boolean isFlywheelLevel(@Nullable LevelAccessor level) { - if (level == null) return false; - - if (!level.isClientSide()) return false; - - if (level instanceof FlywheelLevel && ((FlywheelLevel) level).supportsFlywheel()) return true; - - return level == Minecraft.getInstance().level; - } - - public static boolean isGameActive() { - return !(Minecraft.getInstance().level == null || Minecraft.getInstance().player == null); - } - - public static void reloadWorldRenderers() { - Minecraft.getInstance().levelRenderer.allChanged(); - } - - private static BackendType chooseEngine() { - var preferred = FlwConfig.get() - .getBackendType(); - if (preferred == null) { - return BackendTypes.defaultForCurrentPC(); - } - var actual = preferred.findFallback(); - - if (preferred != actual) { - LOGGER.warn("Flywheel backend fell back from '{}' to '{}'", preferred.getShortName(), actual.getShortName()); - } - - return actual; - } - - public static void init() { - // noop - } - - private Backend() { - throw new UnsupportedOperationException("Backend is a static class!"); - } - -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java b/src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java new file mode 100644 index 000000000..6105c98db --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java @@ -0,0 +1,57 @@ +package com.jozufozu.flywheel.backend; + +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.api.FlywheelLevel; +import com.jozufozu.flywheel.api.backend.BackendManager; +import com.jozufozu.flywheel.backend.task.ParallelTaskExecutor; + +import net.minecraft.client.Minecraft; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelAccessor; + +public class BackendUtil { + public static final boolean DUMP_SHADER_SOURCE = System.getProperty("flw.dumpShaderSource") != null; + + private static ParallelTaskExecutor executor; + + /** + * Get a thread pool for running Flywheel related work in parallel. + * @return A global Flywheel thread pool. + */ + public static ParallelTaskExecutor getTaskExecutor() { + if (executor == null) { + executor = new ParallelTaskExecutor("Flywheel"); + executor.startWorkers(); + } + + return executor; + } + + @Contract("null -> false") + public static boolean canUseInstancing(@Nullable Level level) { + return BackendManager.isOn() && isFlywheelLevel(level); + } + + /** + * Used to avoid calling Flywheel functions on (fake) levels that don't specifically support it. + */ + public static boolean isFlywheelLevel(@Nullable LevelAccessor level) { + if (level == null) return false; + + if (!level.isClientSide()) return false; + + if (level instanceof FlywheelLevel && ((FlywheelLevel) level).supportsFlywheel()) return true; + + return level == Minecraft.getInstance().level; + } + + public static boolean isGameActive() { + return !(Minecraft.getInstance().level == null || Minecraft.getInstance().player == null); + } + + public static void reloadWorldRenderers() { + Minecraft.getInstance().levelRenderer.allChanged(); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/Loader.java b/src/main/java/com/jozufozu/flywheel/backend/Loader.java index a1f612cc6..3d3650bf7 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Loader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Loader.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.backend.compile.FlwCompiler; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.glsl.ShaderSources; @@ -19,22 +20,14 @@ import net.minecraft.server.packs.resources.ResourceManagerReloadListener; *

*/ public class Loader implements ResourceManagerReloadListener { + public static final Loader INSTANCE = new Loader(); - Loader() { - // Can be null when running datagenerators due to the unfortunate time we call this - Minecraft minecraft = Minecraft.getInstance(); - if (minecraft == null) { - return; - } - - if (minecraft.getResourceManager() instanceof ReloadableResourceManager reloadable) { - reloadable.registerReloadListener(this); - } + private Loader() { } @Override public void onResourceManagerReload(ResourceManager manager) { - Backend.refresh(); + BackendManager.refresh(); var errorReporter = new ErrorReporter(); ShaderSources sources = new ShaderSources(errorReporter, manager); @@ -46,9 +39,20 @@ public class Loader implements ResourceManagerReloadListener { FlwCompiler.INSTANCE = new FlwCompiler(sources); ClientLevel level = Minecraft.getInstance().level; - if (Backend.canUseInstancing(level)) { + if (BackendUtil.canUseInstancing(level)) { InstancedRenderDispatcher.resetInstanceLevel(level); } + } + public static void init() { + // Can be null when running datagenerators due to the unfortunate time we call this + Minecraft minecraft = Minecraft.getInstance(); + if (minecraft == null) { + return; + } + + if (minecraft.getResourceManager() instanceof ReloadableResourceManager reloadable) { + reloadable.registerReloadListener(INSTANCE); + } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/Compilation.java b/src/main/java/com/jozufozu/flywheel/backend/compile/Compilation.java index c97ad44b2..52ae6c39d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/Compilation.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/Compilation.java @@ -9,12 +9,13 @@ import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; import org.lwjgl.opengl.GL20; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.gl.GLSLVersion; import com.jozufozu.flywheel.gl.shader.GlShader; import com.jozufozu.flywheel.gl.shader.ShaderType; import com.jozufozu.flywheel.gl.versioned.GlCompat; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.SourceFile; import com.jozufozu.flywheel.util.StringUtil; @@ -130,7 +131,7 @@ public class Compilation { } private static void dumpSource(String source, String fileName) { - if (!Backend.DUMP_SHADER_SOURCE) { + if (!BackendUtil.DUMP_SHADER_SOURCE) { return; } @@ -140,7 +141,7 @@ public class Compilation { try (FileWriter writer = new FileWriter(file)) { writer.write(source); } catch (Exception e) { - Backend.LOGGER.error("Could not dump source.", e); + Flywheel.LOGGER.error("Could not dump source.", e); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/CompileUtil.java b/src/main/java/com/jozufozu/flywheel/backend/compile/CompileUtil.java index dfd28be45..6b5e0e290 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/CompileUtil.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/CompileUtil.java @@ -13,7 +13,7 @@ import java.util.stream.Stream; import org.jetbrains.annotations.NotNull; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.gl.GLSLVersion; import com.jozufozu.flywheel.gl.shader.ShaderType; import com.jozufozu.flywheel.glsl.SourceFile; @@ -76,7 +76,7 @@ public class CompileUtil { String log = glGetProgramInfoLog(handle); if (!log.isEmpty()) { - Backend.LOGGER.debug("Program link log: " + log); + Flywheel.LOGGER.debug("Program link log: " + log); } int result = glGetProgrami(handle, GL_LINK_STATUS); diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/CullingContextSet.java b/src/main/java/com/jozufozu/flywheel/backend/compile/CullingContextSet.java index 04d6c5d8d..bc12bb992 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/CullingContextSet.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/CullingContextSet.java @@ -4,13 +4,12 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.struct.StructType; public class CullingContextSet { static CullingContextSet create() { var builder = new CullingContextSet(); - for (StructType structType : ComponentRegistry.structTypes) { + for (StructType structType : StructType.REGISTRY.getAll()) { builder.add(structType); } return builder; diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwCompiler.java b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwCompiler.java index 0737c490f..d84d6aad2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwCompiler.java @@ -12,22 +12,21 @@ import java.util.stream.Collectors; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.pipeline.Pipeline; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.uniform.ShaderUniforms; import com.jozufozu.flywheel.api.vertex.VertexType; -import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.engine.indirect.IndirectComponent; import com.jozufozu.flywheel.gl.GLSLVersion; import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.gl.shader.ShaderType; import com.jozufozu.flywheel.glsl.ShaderLoadingException; import com.jozufozu.flywheel.glsl.ShaderSources; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.generate.FnSignature; import com.jozufozu.flywheel.glsl.generate.GlslExpr; +import com.jozufozu.flywheel.lib.material.MaterialIndicies; import com.jozufozu.flywheel.lib.pipeline.Pipelines; import com.jozufozu.flywheel.util.StringUtil; @@ -56,12 +55,12 @@ public class FlwCompiler { this.sources = sources; this.vertexMaterialComponent = MaterialAdapterComponent.builder(Flywheel.rl("vertex_material_adapter")) - .materialSources(ComponentRegistry.materials.vertexSources()) + .materialSources(MaterialIndicies.getAllVertexShaders()) .adapt(FnSignature.ofVoid("flw_materialVertex")) .switchOn(GlslExpr.variable("flw_materialVertexID")) .build(sources); this.fragmentMaterialComponent = MaterialAdapterComponent.builder(Flywheel.rl("fragment_material_adapter")) - .materialSources(ComponentRegistry.materials.fragmentSources()) + .materialSources(MaterialIndicies.getAllFragmentShaders()) .adapt(FnSignature.ofVoid("flw_materialFragment")) .adapt(FnSignature.create() .returnType("bool") @@ -76,7 +75,7 @@ public class FlwCompiler { .switchOn(GlslExpr.variable("flw_materialFragmentID")) .build(sources); this.uniformComponent = UniformComponent.builder(Flywheel.rl("uniforms")) - .sources(ComponentRegistry.getAllUniformProviders() + .sources(ShaderUniforms.REGISTRY.getAll() .stream() .map(ShaderUniforms::uniformShader) .toList()) @@ -102,12 +101,12 @@ public class FlwCompiler { private void finish() { long compileEnd = System.nanoTime(); - int programCount = pipelineContexts.size() + ComponentRegistry.structTypes.size(); + int programCount = pipelineContexts.size() + StructType.REGISTRY.getAll().size(); int shaderCount = shaderCompiler.shaderCount(); int errorCount = errors.size(); var elapsed = StringUtil.formatTime(compileEnd - compileStart); - Backend.LOGGER.info("Compiled " + programCount + " programs and " + shaderCount + " shaders in " + elapsed + " with " + errorCount + " errors."); + Flywheel.LOGGER.info("Compiled " + programCount + " programs and " + shaderCount + " shaders in " + elapsed + " with " + errorCount + " errors."); if (errorCount > 0) { var details = errors.stream() diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/Includer.java b/src/main/java/com/jozufozu/flywheel/backend/compile/Includer.java index 0ea86c1a1..3d562663e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/Includer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/Includer.java @@ -3,7 +3,7 @@ package com.jozufozu.flywheel.backend.compile; import java.util.function.Consumer; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; +import com.jozufozu.flywheel.glsl.SourceComponent; /** * A component of a ShaderCompiler, responsible for expanding root sources into the complete set of included sources. diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/MaterialAdapterComponent.java b/src/main/java/com/jozufozu/flywheel/backend/compile/MaterialAdapterComponent.java index 91c8ff5c0..f14483ca1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/MaterialAdapterComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/MaterialAdapterComponent.java @@ -11,8 +11,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.glsl.ShaderSources; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.generate.FnSignature; import com.jozufozu.flywheel.glsl.generate.GlslBlock; import com.jozufozu.flywheel.glsl.generate.GlslBuilder; diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineContextSet.java b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineContextSet.java index 21981a3a5..103e47406 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineContextSet.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineContextSet.java @@ -1,23 +1,31 @@ package com.jozufozu.flywheel.backend.compile; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; -import com.jozufozu.flywheel.api.component.ComponentRegistry; +import com.jozufozu.flywheel.api.backend.Backend; import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.pipeline.Pipeline; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.vertex.VertexType; -import com.jozufozu.flywheel.lib.backend.BackendTypes; import com.jozufozu.flywheel.lib.context.Contexts; public class PipelineContextSet { + private final List contexts = new ArrayList<>(); + private final List contextView = Collections.unmodifiableList(contexts); + + PipelineContextSet() { + } + static PipelineContextSet create() { var builder = new PipelineContextSet(); - for (Pipeline pipelineShader : BackendTypes.availablePipelineShaders()) { - for (StructType structType : ComponentRegistry.structTypes) { - for (VertexType vertexType : ComponentRegistry.vertexTypes) { + for (Pipeline pipelineShader : availablePipelineShaders()) { + for (StructType structType : StructType.REGISTRY.getAll()) { + for (VertexType vertexType : VertexType.REGISTRY.getAll()) { builder.add(vertexType, structType, Contexts.WORLD, pipelineShader); } } @@ -25,10 +33,13 @@ public class PipelineContextSet { return builder; } - private final List contexts = new ArrayList<>(); - private final List contextView = Collections.unmodifiableList(contexts); - - PipelineContextSet() { + private static Collection availablePipelineShaders() { + return Backend.REGISTRY.getAll() + .stream() + .filter(Backend::isSupported) + .map(Backend::pipelineShader) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } public List all() { @@ -42,7 +53,6 @@ public class PipelineContextSet { private void add(VertexType vertexType, StructType structType, Context world, Pipeline pipelineShader) { var ctx = new PipelineContext(vertexType, structType, world, pipelineShader); - contexts.add(ctx); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/RecursiveIncluder.java b/src/main/java/com/jozufozu/flywheel/backend/compile/RecursiveIncluder.java index 38766c5ce..bbf9258d9 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/RecursiveIncluder.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/RecursiveIncluder.java @@ -5,7 +5,7 @@ import java.util.Set; import java.util.function.Consumer; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; +import com.jozufozu.flywheel.glsl.SourceComponent; public class RecursiveIncluder implements Includer { diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/ShaderCompiler.java b/src/main/java/com/jozufozu/flywheel/backend/compile/ShaderCompiler.java index 2beadb80e..81622bc32 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/ShaderCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/ShaderCompiler.java @@ -9,10 +9,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.gl.GLSLVersion; import com.jozufozu.flywheel.gl.shader.GlShader; import com.jozufozu.flywheel.gl.shader.ShaderType; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.util.FlwUtil; public class ShaderCompiler { diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/StringSubstitutionSourceComponent.java b/src/main/java/com/jozufozu/flywheel/backend/compile/StringSubstitutionSourceComponent.java index d7c2eed12..375696bba 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/StringSubstitutionSourceComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/StringSubstitutionSourceComponent.java @@ -3,7 +3,7 @@ package com.jozufozu.flywheel.backend.compile; import java.util.Collection; import java.util.Map; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.util.ResourceUtil; import net.minecraft.resources.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/UniformComponent.java b/src/main/java/com/jozufozu/flywheel/backend/compile/UniformComponent.java index 10d9fe7c3..3a23b4da9 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/UniformComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/UniformComponent.java @@ -5,8 +5,8 @@ import java.util.Collection; import java.util.List; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.glsl.ShaderSources; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.SourceFile; import com.jozufozu.flywheel.glsl.generate.GlslBuilder; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java index 0b091f6b4..50500441d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/BatchingEngine.java @@ -25,7 +25,7 @@ public class BatchingEngine implements Engine { protected final BatchingDrawTracker drawTracker = new BatchingDrawTracker(); @Override - public Instancer instancer(StructType type, Model model, RenderStage stage) { + public Instancer getInstancer(StructType type, Model model, RenderStage stage) { return transformManager.getInstancer(type, model, stage); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectComponent.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectComponent.java index 7c0ce5533..f82f04f41 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectComponent.java @@ -5,16 +5,16 @@ import java.util.List; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.api.layout.LayoutItem; import com.jozufozu.flywheel.api.pipeline.Pipeline; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.glsl.ShaderSources; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.SourceFile; import com.jozufozu.flywheel.glsl.generate.FnSignature; import com.jozufozu.flywheel.glsl.generate.GlslBlock; import com.jozufozu.flywheel.glsl.generate.GlslBuilder; import com.jozufozu.flywheel.glsl.generate.GlslExpr; -import com.jozufozu.flywheel.lib.layout.LayoutItem; import com.jozufozu.flywheel.lib.pipeline.Pipelines; import net.minecraft.resources.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java index 1c7ef6524..16e7763aa 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectCullingGroup.java @@ -16,6 +16,7 @@ import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.backend.compile.FlwCompiler; +import com.jozufozu.flywheel.backend.uniform.UniformBuffer; import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.lib.context.Contexts; import com.jozufozu.flywheel.lib.pipeline.Pipelines; @@ -112,7 +113,7 @@ public class IndirectCullingGroup { uploadInstanceData(); uploadIndirectCommands(); - compute.bind(); + UniformBuffer.syncAndBind(compute); buffers.bindForCompute(); var groupCount = (instanceCountThisFrame + 31) >> 5; // ceil(instanceCount / 32) @@ -125,7 +126,7 @@ public class IndirectCullingGroup { return; } - draw.bind(); + UniformBuffer.syncAndBind(draw); glBindVertexArray(vertexArray); buffers.bindForDraw(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java index 6baadda7c..2ddf7903f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDraw.java @@ -2,10 +2,10 @@ package com.jozufozu.flywheel.backend.engine.indirect; import org.lwjgl.system.MemoryUtil; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.material.Material; +import com.jozufozu.flywheel.lib.material.MaterialIndicies; public final class IndirectDraw { private final IndirectInstancer instancer; @@ -25,8 +25,8 @@ public final class IndirectDraw { this.stage = stage; this.mesh = mesh; - this.vertexMaterialID = ComponentRegistry.materials.getVertexID(material); - this.fragmentMaterialID = ComponentRegistry.materials.getFragmentID(material); + this.vertexMaterialID = MaterialIndicies.getVertexShaderIndex(material.vertexShader()); + this.fragmentMaterialID = MaterialIndicies.getFragmentShaderIndex(material.fragmentShader()); } public void prepare(int baseInstance) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawSet.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawSet.java index 614a1866d..9a1e8c28f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawSet.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawSet.java @@ -10,10 +10,10 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.material.Material; +import com.jozufozu.flywheel.lib.material.MaterialIndicies; import com.jozufozu.flywheel.util.Textures; public class IndirectDrawSet { @@ -50,7 +50,7 @@ public class IndirectDrawSet { multiDraws.clear(); // sort by stage, then material indirectDraws.sort(Comparator.comparing(IndirectDraw::stage) - .thenComparing(draw -> ComponentRegistry.materials.getMaterialID(draw.material()))); + .thenComparing(draw -> MaterialIndicies.getMaterialIndex(draw.material()))); for (int start = 0, i = 0; i < indirectDraws.size(); i++) { var draw = indirectDraws.get(i); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java index d8056ab09..8dc9b338f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectEngine.java @@ -48,7 +48,7 @@ public class IndirectEngine implements Engine { } @Override - public Instancer instancer(StructType type, Model model, RenderStage stage) { + public Instancer getInstancer(StructType type, Model model, RenderStage stage) { return drawManager.getInstancer(type, model, stage); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedArraysComponent.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedArraysComponent.java index 97fd22f38..fa716004c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedArraysComponent.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedArraysComponent.java @@ -5,13 +5,13 @@ import java.util.Collections; import java.util.List; import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.api.layout.LayoutItem; import com.jozufozu.flywheel.api.pipeline.Pipeline; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; +import com.jozufozu.flywheel.glsl.SourceComponent; import com.jozufozu.flywheel.glsl.generate.FnSignature; import com.jozufozu.flywheel.glsl.generate.GlslBlock; import com.jozufozu.flywheel.glsl.generate.GlslBuilder; import com.jozufozu.flywheel.glsl.generate.GlslExpr; -import com.jozufozu.flywheel.lib.layout.LayoutItem; import net.minecraft.resources.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java index 155dd3e19..e9f9320c2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedMeshPool.java @@ -15,6 +15,7 @@ import com.jozufozu.flywheel.api.model.Mesh; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.gl.GlPrimitive; import com.jozufozu.flywheel.gl.array.GlVertexArray; +import com.jozufozu.flywheel.gl.buffer.ElementBuffer; import com.jozufozu.flywheel.gl.buffer.GlBuffer; import com.jozufozu.flywheel.gl.buffer.GlBufferType; import com.jozufozu.flywheel.gl.buffer.MappedBuffer; @@ -234,9 +235,9 @@ public class InstancedMeshPool { private void draw(int instanceCount) { if (instanceCount > 1) { - GL32.glDrawElementsInstanced(GlPrimitive.TRIANGLES.glEnum, ebo.elementCount, ebo.eboIndexType.asGLType, 0, instanceCount); + GL32.glDrawElementsInstanced(GlPrimitive.TRIANGLES.glEnum, ebo.getElementCount(), ebo.getEboIndexType().asGLType, 0, instanceCount); } else { - GL32.glDrawElements(GlPrimitive.TRIANGLES.glEnum, ebo.elementCount, ebo.eboIndexType.asGLType, 0); + GL32.glDrawElements(GlPrimitive.TRIANGLES.glEnum, ebo.getElementCount(), ebo.getEboIndexType().asGLType, 0); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java index fa3f88cdb..88e67b26d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingEngine.java @@ -7,7 +7,6 @@ import java.util.Set; import org.lwjgl.opengl.GL32; import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.event.RenderStage; @@ -18,8 +17,10 @@ import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.api.task.TaskExecutor; import com.jozufozu.flywheel.backend.compile.FlwCompiler; import com.jozufozu.flywheel.backend.instancing.InstanceManager; +import com.jozufozu.flywheel.backend.uniform.UniformBuffer; import com.jozufozu.flywheel.gl.GlStateTracker; import com.jozufozu.flywheel.gl.GlTextureUnit; +import com.jozufozu.flywheel.lib.material.MaterialIndicies; import com.jozufozu.flywheel.lib.pipeline.Pipelines; import com.jozufozu.flywheel.util.FlwUtil; import com.mojang.blaze3d.systems.RenderSystem; @@ -51,7 +52,7 @@ public class InstancingEngine implements Engine { } @Override - public Instancer instancer(StructType type, Model model, RenderStage stage) { + public Instancer getInstancer(StructType type, Model model, RenderStage stage) { return drawManager.getInstancer(type, model, stage); } @@ -117,11 +118,11 @@ public class InstancingEngine implements Engine { var material = desc.material(); var program = FlwCompiler.INSTANCE.getPipelineProgram(vertexType, structType, context, Pipelines.INSTANCED_ARRAYS); - program.bind(); + UniformBuffer.syncAndBind(program); var uniformLocation = program.getUniformLocation("_flw_materialID_instancing"); - var vertexID = ComponentRegistry.materials.getVertexID(material); - var fragmentID = ComponentRegistry.materials.getFragmentID(material); + var vertexID = MaterialIndicies.getVertexShaderIndex(material.vertexShader()); + var fragmentID = MaterialIndicies.getFragmentShaderIndex(material.fragmentShader()); GL32.glUniform2ui(uniformLocation, vertexID, fragmentID); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java index de904f5d5..610f6335f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java @@ -7,7 +7,7 @@ import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.instancer.FlatLit; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstanceManager; import com.jozufozu.flywheel.lib.light.LightListener; @@ -22,11 +22,11 @@ import net.minecraft.world.level.LightLayer; */ public abstract class AbstractInstance implements Instance, LightListener { - protected final InstancerManager instancerManager; + protected final InstancerProvider instancerManager; public final Level level; protected boolean removed = false; - public AbstractInstance(InstancerManager instancerManager, Level level) { + public AbstractInstance(InstancerProvider instancerManager, Level level) { this.instancerManager = instancerManager; this.level = level; } 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 10cf8a397..77cfe6e41 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -9,11 +9,11 @@ import java.util.function.Consumer; import org.joml.FrustumIntersection; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.task.TaskExecutor; -import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.instancing.ratelimit.BandedPrimeLimiter; import com.jozufozu.flywheel.backend.instancing.ratelimit.DistanceUpdateLimiter; import com.jozufozu.flywheel.backend.instancing.ratelimit.NonLimiter; @@ -164,7 +164,7 @@ public abstract class InstanceManager { } public void add(T obj) { - if (!Backend.isOn()) return; + if (!BackendManager.isOn()) return; if (canCreateInstance(obj)) { getStorage().add(obj); @@ -172,7 +172,7 @@ public abstract class InstanceManager { } public void queueAdd(T obj) { - if (!Backend.isOn()) { + if (!BackendManager.isOn()) { return; } @@ -186,7 +186,7 @@ public abstract class InstanceManager { } public void queueUpdate(T obj) { - if (!Backend.isOn()) return; + if (!BackendManager.isOn()) return; if (!canCreateInstance(obj)) { return; @@ -209,7 +209,7 @@ public abstract class InstanceManager { * @param obj the object to update. */ public void update(T obj) { - if (!Backend.isOn()) return; + if (!BackendManager.isOn()) return; if (canCreateInstance(obj)) { getStorage().update(obj); @@ -217,7 +217,7 @@ public abstract class InstanceManager { } public void remove(T obj) { - if (!Backend.isOn()) return; + if (!BackendManager.isOn()) return; getStorage().remove(obj); } @@ -267,7 +267,7 @@ public abstract class InstanceManager { } public void queueAddAll(Collection objects) { - if (!Backend.isOn() || objects.isEmpty()) { + if (!BackendManager.isOn() || objects.isEmpty()) { return; } 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 3ba4849a4..5b8cb0b46 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -1,12 +1,13 @@ package com.jozufozu.flywheel.backend.instancing; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.api.backend.Engine; import com.jozufozu.flywheel.api.event.BeginFrameEvent; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstanceManager; import com.jozufozu.flywheel.backend.instancing.effect.Effect; import com.jozufozu.flywheel.backend.instancing.effect.EffectInstanceManager; @@ -35,7 +36,7 @@ public class InstanceWorld implements AutoCloseable { private final InstanceManager effects; public static InstanceWorld create(LevelAccessor level) { - var engine = Backend.getBackendType() + var engine = BackendManager.getBackend() .createEngine(); var entities = new EntityInstanceManager(engine); @@ -53,7 +54,7 @@ public class InstanceWorld implements AutoCloseable { this.entities = entities; this.blockEntities = blockEntities; this.effects = effects; - this.taskExecutor = Backend.getTaskExecutor(); + this.taskExecutor = BackendUtil.getTaskExecutor(); } public InstanceManager getEntities() { 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 09ba5491b..5be6f8a6c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -2,10 +2,11 @@ package com.jozufozu.flywheel.backend.instancing; import java.util.List; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.api.event.BeginFrameEvent; import com.jozufozu.flywheel.api.event.ReloadRenderersEvent; import com.jozufozu.flywheel.api.event.RenderStageEvent; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.backend.instancing.effect.Effect; import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; @@ -29,7 +30,7 @@ public class InstancedRenderDispatcher { * @param blockEntity The block entity whose instance you want to update. */ public static void enqueueUpdate(BlockEntity blockEntity) { - if (Backend.isOn() && blockEntity.hasLevel() && blockEntity.getLevel() instanceof ClientLevel) { + if (BackendManager.isOn() && blockEntity.hasLevel() && blockEntity.getLevel() instanceof ClientLevel) { instanceWorlds.get(blockEntity.getLevel()) .getBlockEntities() .queueUpdate(blockEntity); @@ -41,7 +42,7 @@ public class InstancedRenderDispatcher { * @param entity The entity whose instance you want to update. */ public static void enqueueUpdate(Entity entity) { - if (Backend.isOn()) { + if (BackendManager.isOn()) { instanceWorlds.get(entity.level) .getEntities() .queueUpdate(entity); @@ -65,7 +66,7 @@ public class InstancedRenderDispatcher { * @throws NullPointerException if the backend is off */ public static InstanceWorld getInstanceWorld(LevelAccessor world) { - if (Backend.isOn()) { + if (BackendManager.isOn()) { return instanceWorlds.get(world); } else { throw new NullPointerException("Backend is off, cannot retrieve instance world."); @@ -73,21 +74,21 @@ public class InstancedRenderDispatcher { } public static void tick(TickEvent.ClientTickEvent event) { - if (!Backend.isGameActive() || event.phase == TickEvent.Phase.START) { + if (!BackendUtil.isGameActive() || event.phase == TickEvent.Phase.START) { return; } Minecraft mc = Minecraft.getInstance(); ClientLevel level = mc.level; AnimationTickHolder.tick(); - if (Backend.isOn()) { + if (BackendManager.isOn()) { instanceWorlds.get(level) .tick(); } } public static void onBeginFrame(BeginFrameEvent event) { - if (Backend.isGameActive() && Backend.isOn()) { + if (BackendUtil.isGameActive() && BackendManager.isOn()) { instanceWorlds.get(event.getContext().level()) .beginFrame(event); } @@ -95,14 +96,14 @@ public class InstancedRenderDispatcher { public static void onRenderStage(RenderStageEvent event) { ClientLevel level = event.getContext().level(); - if (!Backend.canUseInstancing(level)) return; + if (!BackendUtil.canUseInstancing(level)) return; instanceWorlds.get(level).renderStage(event.getContext(), event.getStage()); } public static void onReloadRenderers(ReloadRenderersEvent event) { ClientLevel level = event.getLevel(); - if (Backend.isOn() && level != null) { + if (BackendManager.isOn() && level != null) { resetInstanceLevel(level); } } @@ -113,7 +114,7 @@ public class InstancedRenderDispatcher { } public static void getDebugString(List debug) { - if (Backend.isOn()) { + if (BackendManager.isOn()) { InstanceWorld instanceWorld = instanceWorlds.get(Minecraft.getInstance().level); debug.add("Update limiting: " + FlwCommands.boolToText(FlwConfig.get().limitUpdates()).getString()); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java index 9c8d68d98..128cbfa20 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java @@ -8,7 +8,7 @@ import org.joml.FrustumIntersection; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.instancer.InstancedPart; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.lib.box.ImmutableBox; import com.jozufozu.flywheel.lib.box.MutableBox; @@ -41,7 +41,7 @@ public abstract class BlockEntityInstance extends Abstrac protected final BlockPos instancePos; protected final BlockState blockState; - public BlockEntityInstance(InstancerManager instancerManager, T blockEntity) { + public BlockEntityInstance(InstancerProvider instancerManager, T blockEntity) { super(instancerManager, blockEntity.getLevel()); this.blockEntity = blockEntity; this.pos = blockEntity.getBlockPos(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstanceManager.java index fa490ee2a..571d9df0d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstanceManager.java @@ -2,11 +2,11 @@ package com.jozufozu.flywheel.backend.instancing.blockentity; import java.util.List; -import com.jozufozu.flywheel.api.instancer.InstancerManager; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.instance.InstancedRenderRegistry; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.InstanceManager; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import com.jozufozu.flywheel.backend.instancing.storage.One2OneStorage; import com.jozufozu.flywheel.backend.instancing.storage.Storage; @@ -21,7 +21,7 @@ public class BlockEntityInstanceManager extends InstanceManager { private final BlockEntityStorage storage; - public BlockEntityInstanceManager(InstancerManager instancerManager) { + public BlockEntityInstanceManager(InstancerProvider instancerManager) { storage = new BlockEntityStorage(instancerManager); } @@ -57,7 +57,7 @@ public class BlockEntityInstanceManager extends InstanceManager { return false; } - if (Backend.isFlywheelLevel(level)) { + if (BackendUtil.isFlywheelLevel(level)) { BlockPos pos = blockEntity.getBlockPos(); BlockGetter existingChunk = level.getChunkForCollisions(pos.getX() >> 4, pos.getZ() >> 4); @@ -73,7 +73,7 @@ public class BlockEntityInstanceManager extends InstanceManager { final Long2ObjectMap> posLookup = new Long2ObjectOpenHashMap<>(); - public BlockEntityStorage(InstancerManager manager) { + public BlockEntityStorage(InstancerProvider manager) { super(manager); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/SimpleBlockEntityInstancingController.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/SimpleBlockEntityInstancingController.java deleted file mode 100644 index 6b86086fe..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/SimpleBlockEntityInstancingController.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.jozufozu.flywheel.backend.instancing.blockentity; - -import java.util.function.BiFunction; -import java.util.function.Predicate; - -import com.jozufozu.flywheel.api.instancer.InstancerManager; - -import net.minecraft.world.level.block.entity.BlockEntity; - -public class SimpleBlockEntityInstancingController implements BlockEntityInstancingController { - protected BiFunction> instanceFactory; - protected Predicate skipRender; - - public SimpleBlockEntityInstancingController(BiFunction> instanceFactory, Predicate skipRender) { - this.instanceFactory = instanceFactory; - this.skipRender = skipRender; - } - - @Override - public BlockEntityInstance createInstance(InstancerManager instancerManager, T blockEntity) { - return instanceFactory.apply(instancerManager, blockEntity); - } - - @Override - public boolean shouldSkipRender(T blockEntity) { - return skipRender.test(blockEntity); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/Effect.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/Effect.java index fd91116b0..d3bf8057e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/Effect.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/Effect.java @@ -2,10 +2,10 @@ package com.jozufozu.flywheel.backend.instancing.effect; import java.util.Collection; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; public interface Effect { - Collection createInstances(InstancerManager instancerManager); + Collection createInstances(InstancerProvider instancerManager); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java index 01116c389..8a0fe8da3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.storage.AbstractStorage; @@ -15,7 +15,7 @@ public class EffectInstanceManager extends InstanceManager { private final EffectStorage storage; - public EffectInstanceManager(InstancerManager instancerManager) { + public EffectInstanceManager(InstancerProvider instancerManager) { storage = new EffectStorage<>(instancerManager); } @@ -33,7 +33,7 @@ public class EffectInstanceManager extends InstanceManager { private final Multimap instances; - public EffectStorage(InstancerManager manager) { + public EffectStorage(InstancerProvider manager) { super(manager); this.instances = HashMultimap.create(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java index 044b137a8..58e6f2737 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java @@ -4,7 +4,7 @@ import org.joml.FrustumIntersection; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstanceManager; import com.jozufozu.flywheel.lib.box.MutableBox; @@ -40,7 +40,7 @@ public abstract class EntityInstance extends AbstractInstance protected final E entity; protected final MutableBox bounds; - public EntityInstance(InstancerManager instancerManager, E entity) { + public EntityInstance(InstancerProvider instancerManager, E entity) { super(instancerManager, entity.level); this.entity = entity; bounds = MutableBox.from(entity.getBoundingBox()); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java index 1832d8bdf..bb3c13c5d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java @@ -2,11 +2,11 @@ package com.jozufozu.flywheel.backend.instancing.entity; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.api.instancer.InstancerManager; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.instance.InstancedRenderRegistry; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.InstanceManager; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import com.jozufozu.flywheel.backend.instancing.storage.One2OneStorage; import net.minecraft.world.entity.Entity; @@ -16,7 +16,7 @@ public class EntityInstanceManager extends InstanceManager { private final One2OneStorage storage; - public EntityInstanceManager(InstancerManager instancerManager) { + public EntityInstanceManager(InstancerProvider instancerManager) { storage = new One2OneStorage<>(instancerManager) { @Override protected @Nullable AbstractInstance createRaw(Entity obj) { @@ -42,6 +42,6 @@ public class EntityInstanceManager extends InstanceManager { Level level = entity.level; - return Backend.isFlywheelLevel(level); + return BackendUtil.isFlywheelLevel(level); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/SimpleEntityInstancingController.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/SimpleEntityInstancingController.java deleted file mode 100644 index 95e964068..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/SimpleEntityInstancingController.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.jozufozu.flywheel.backend.instancing.entity; - -import java.util.function.BiFunction; -import java.util.function.Predicate; - -import com.jozufozu.flywheel.api.instancer.InstancerManager; - -import net.minecraft.world.entity.Entity; - -public class SimpleEntityInstancingController implements EntityInstancingController { - protected BiFunction> instanceFactory; - protected Predicate skipRender; - - public SimpleEntityInstancingController(BiFunction> instanceFactory, Predicate skipRender) { - this.instanceFactory = instanceFactory; - this.skipRender = skipRender; - } - - @Override - public EntityInstance createInstance(InstancerManager instancerManager, T entity) { - return instanceFactory.apply(instancerManager, entity); - } - - @Override - public boolean shouldSkipRender(T entity) { - return skipRender.test(entity); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java index ef09014f1..f2274b4da 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java @@ -5,16 +5,16 @@ import java.util.List; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.lib.light.LightUpdater; public abstract class AbstractStorage implements Storage { protected final List tickableInstances; protected final List dynamicInstances; - protected final InstancerManager instancerManager; + protected final InstancerProvider instancerManager; - protected AbstractStorage(InstancerManager instancerManager) { + protected AbstractStorage(InstancerProvider instancerManager) { this.instancerManager = instancerManager; this.dynamicInstances = new ArrayList<>(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java index b852a82ca..2ca0866cb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java @@ -5,14 +5,14 @@ import java.util.Map; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.lib.light.LightUpdater; public abstract class One2OneStorage extends AbstractStorage { private final Map instances; - public One2OneStorage(InstancerManager instancerManager) { + public One2OneStorage(InstancerProvider instancerManager) { super(instancerManager); this.instances = new HashMap<>(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/uniform/UniformBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/uniform/UniformBuffer.java index 540c1e0f3..d68c2c5b3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/uniform/UniformBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/uniform/UniformBuffer.java @@ -1,15 +1,15 @@ package com.jozufozu.flywheel.backend.uniform; -import java.util.Collection; import java.util.List; +import java.util.Set; import org.lwjgl.opengl.GL32; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.uniform.ShaderUniforms; import com.jozufozu.flywheel.gl.buffer.GlBuffer; import com.jozufozu.flywheel.gl.buffer.GlBufferType; +import com.jozufozu.flywheel.gl.shader.GlProgram; import com.jozufozu.flywheel.lib.memory.MemoryBlock; import com.jozufozu.flywheel.util.FlwUtil; import com.jozufozu.flywheel.util.RenderMath; @@ -35,7 +35,12 @@ public class UniformBuffer { private UniformBuffer() { buffer = new GlBuffer(GlBufferType.UNIFORM_BUFFER); - providerSet = new ProviderSet(ComponentRegistry.getAllUniformProviders()); + providerSet = new ProviderSet(ShaderUniforms.REGISTRY.getAll()); + } + + public static void syncAndBind(GlProgram program) { + getInstance().sync(); + program.bind(); } public void sync() { @@ -84,7 +89,7 @@ public class UniformBuffer { private final MemoryBlock data; - private ProviderSet(final Collection providers) { + private ProviderSet(final Set providers) { var builder = ImmutableList.builder(); int totalBytes = 0; for (ShaderUniforms provider : providers) { diff --git a/src/main/java/com/jozufozu/flywheel/config/BackendTypeArgument.java b/src/main/java/com/jozufozu/flywheel/config/BackendArgument.java similarity index 59% rename from src/main/java/com/jozufozu/flywheel/config/BackendTypeArgument.java rename to src/main/java/com/jozufozu/flywheel/config/BackendArgument.java index 209a765d3..86b0a99d9 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BackendTypeArgument.java +++ b/src/main/java/com/jozufozu/flywheel/config/BackendArgument.java @@ -1,10 +1,10 @@ package com.jozufozu.flywheel.config; import java.util.Collection; +import java.util.List; import java.util.concurrent.CompletableFuture; -import com.jozufozu.flywheel.api.backend.BackendType; -import com.jozufozu.flywheel.lib.backend.BackendTypes; +import com.jozufozu.flywheel.api.backend.Backend; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; @@ -15,38 +15,41 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; -public enum BackendTypeArgument implements ArgumentType { +public enum BackendArgument implements ArgumentType { INSTANCE; + private static final List STRING_IDS = Backend.REGISTRY.getAllIds().stream().map(ResourceLocation::toString).toList(); + private static final Dynamic2CommandExceptionType INVALID = new Dynamic2CommandExceptionType((found, constants) -> { // TODO: don't steal lang return new TranslatableComponent("commands.forge.arguments.enum.invalid", constants, found); }); - @Override - public BackendType parse(StringReader reader) throws CommandSyntaxException { - String string = reader.readUnquotedString(); - - BackendType engine = BackendTypes.getBackendType(string); - - if (engine == null) { - throw INVALID.createWithContext(reader, string, BackendTypes.validNames()); - } - - return engine; + public static BackendArgument getInstance() { + return INSTANCE; } + @Override + public Backend parse(StringReader reader) throws CommandSyntaxException { + ResourceLocation id = ResourceLocation.read(reader); + Backend backend = Backend.REGISTRY.get(id); + + if (backend == null) { + throw INVALID.createWithContext(reader, id.toString(), STRING_IDS); + } + + return backend; + } + + @Override public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { - return SharedSuggestionProvider.suggest(BackendTypes.validNames(), builder); + return SharedSuggestionProvider.suggest(STRING_IDS, builder); } @Override public Collection getExamples() { - return BackendTypes.validNames(); - } - - public static BackendTypeArgument getInstance() { - return INSTANCE; + return STRING_IDS; } } diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index 477a3c619..cca4aae70 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -2,12 +2,8 @@ package com.jozufozu.flywheel.config; import java.util.function.BiConsumer; -import org.jetbrains.annotations.NotNull; - -import com.jozufozu.flywheel.api.backend.BackendType; -import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.lib.backend.BackendTypes; -import com.jozufozu.flywheel.lib.backend.SimpleBackendType; +import com.jozufozu.flywheel.api.backend.Backend; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.lib.uniform.FlwShaderUniforms; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; @@ -15,6 +11,7 @@ import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.minecraft.ChatFormatting; +import net.minecraft.ResourceLocationException; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; import net.minecraft.commands.CommandSourceStack; @@ -24,6 +21,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.TextComponent; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; import net.minecraftforge.client.event.RegisterClientCommandsEvent; import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; @@ -39,22 +37,38 @@ public class FlwCommands { .executes(context -> { LocalPlayer player = Minecraft.getInstance().player; if (player != null) { - player.displayClientMessage(BackendTypes.getBackendType(value.get()) - .getEngineMessage(), false); + String backendIdStr = value.get(); + + ResourceLocation backendId; + try { + backendId = new ResourceLocation(backendIdStr); + } catch (ResourceLocationException e) { + player.displayClientMessage(new TextComponent("Config contains invalid backend ID '" + backendIdStr + "'!"), false); + return 0; + } + + Backend backend = Backend.REGISTRY.get(backendId); + if (backend == null) { + player.displayClientMessage(new TextComponent("Config contains non-existent backend with ID '" + backendId + "'!"), false); + return 0; + } + + Component message = backend.getEngineMessage(); + player.displayClientMessage(message, false); } return Command.SINGLE_SUCCESS; }) - .then(Commands.argument("type", BackendTypeArgument.INSTANCE) + .then(Commands.argument("id", BackendArgument.INSTANCE) .executes(context -> { LocalPlayer player = Minecraft.getInstance().player; if (player != null) { - BackendType type = context.getArgument("type", SimpleBackendType.class); - value.set(type.getShortName()); + Backend backend = context.getArgument("id", Backend.class); + value.set(Backend.REGISTRY.getId(backend).toString()); - Component message = getEngineMessage(type); + Component message = backend.getEngineMessage(); player.displayClientMessage(message, false); - Backend.reloadWorldRenderers(); + BackendUtil.reloadWorldRenderers(); } return Command.SINGLE_SUCCESS; }))); @@ -74,7 +88,7 @@ public class FlwCommands { Component text = boolToText(bool).append(new TextComponent(" update limiting.").withStyle(ChatFormatting.WHITE)); player.displayClientMessage(text, false); - Backend.reloadWorldRenderers(); + BackendUtil.reloadWorldRenderers(); } )); @@ -152,10 +166,6 @@ public class FlwCommands { return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED); } - public static Component getEngineMessage(@NotNull BackendType type) { - return type.getEngineMessage(); - } - public static class ConfigCommandBuilder { protected LiteralArgumentBuilder command; diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java index 047cd6d79..3652461c1 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java @@ -2,54 +2,82 @@ package com.jozufozu.flywheel.config; import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; -import com.jozufozu.flywheel.api.backend.BackendType; -import com.jozufozu.flywheel.lib.backend.BackendTypes; +import com.jozufozu.flywheel.api.backend.Backend; +import com.jozufozu.flywheel.api.backend.BackendManager; +import com.mojang.logging.LogUtils; +import net.minecraft.ResourceLocationException; +import net.minecraft.resources.ResourceLocation; import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec.BooleanValue; +import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.config.ModConfig; public class FlwConfig { - + private static final Logger LOGGER = LogUtils.getLogger(); private static final FlwConfig INSTANCE = new FlwConfig(); public final ClientConfig client; + private final ForgeConfigSpec clientSpec; public FlwConfig() { Pair client = new ForgeConfigSpec.Builder().configure(ClientConfig::new); this.client = client.getLeft(); - - ModLoadingContext.get() - .registerConfig(ModConfig.Type.CLIENT, client.getRight()); + clientSpec = client.getRight(); } public static FlwConfig get() { return INSTANCE; } + public Backend getBackend() { + Backend backend = parseBackend(client.backend.get()); + if (backend == null) { + backend = BackendManager.getDefaultBackend(); + client.backend.set(Backend.REGISTRY.getId(backend).toString()); + } + + return backend; + } + @Nullable - public BackendType getBackendType() { - return BackendTypes.getBackendType(client.backend.get()); + private static Backend parseBackend(String idStr) { + ResourceLocation backendId; + try { + backendId = new ResourceLocation(idStr); + } catch (ResourceLocationException e) { + LOGGER.warn("Config contains invalid backend ID '" + idStr + "'!"); + return null; + } + + Backend backend = Backend.REGISTRY.get(backendId); + if (backend == null) { + LOGGER.warn("Config contains non-existent backend with ID '" + backendId + "'!"); + return null; + } + + return backend; } public boolean limitUpdates() { return client.limitUpdates.get(); } - public static void init() { + public void registerSpecs(ModLoadingContext context) { + context.registerConfig(ModConfig.Type.CLIENT, clientSpec); } public static class ClientConfig { - public final ForgeConfigSpec.ConfigValue backend; + public final ConfigValue backend; public final BooleanValue limitUpdates; public ClientConfig(ForgeConfigSpec.Builder builder) { backend = builder.comment("Select the backend to use.") - .define("backend", BackendTypes.defaultForCurrentPC() - .getShortName()); + .define("backend", Backend.REGISTRY.getId(BackendManager.getDefaultBackend()).toString()); limitUpdates = builder.comment("Enable or disable instance update limiting with distance.") .define("limitUpdates", true); diff --git a/src/main/java/com/jozufozu/flywheel/extension/BlockEntityTypeExtension.java b/src/main/java/com/jozufozu/flywheel/extension/BlockEntityTypeExtension.java index cb0e671c1..2eba7485e 100644 --- a/src/main/java/com/jozufozu/flywheel/extension/BlockEntityTypeExtension.java +++ b/src/main/java/com/jozufozu/flywheel/extension/BlockEntityTypeExtension.java @@ -2,7 +2,7 @@ package com.jozufozu.flywheel.extension; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstancingController; +import com.jozufozu.flywheel.api.instance.blockentity.BlockEntityInstancingController; import net.minecraft.world.level.block.entity.BlockEntity; diff --git a/src/main/java/com/jozufozu/flywheel/extension/EntityTypeExtension.java b/src/main/java/com/jozufozu/flywheel/extension/EntityTypeExtension.java index 2fded954a..28427dd51 100644 --- a/src/main/java/com/jozufozu/flywheel/extension/EntityTypeExtension.java +++ b/src/main/java/com/jozufozu/flywheel/extension/EntityTypeExtension.java @@ -2,7 +2,7 @@ package com.jozufozu.flywheel.extension; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.backend.instancing.entity.EntityInstancingController; +import com.jozufozu.flywheel.api.instance.entity.EntityInstancingController; import net.minecraft.world.entity.Entity; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ElementBuffer.java b/src/main/java/com/jozufozu/flywheel/gl/buffer/ElementBuffer.java similarity index 84% rename from src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ElementBuffer.java rename to src/main/java/com/jozufozu/flywheel/gl/buffer/ElementBuffer.java index 556b8026d..590fe5ce3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ElementBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/gl/buffer/ElementBuffer.java @@ -1,6 +1,5 @@ -package com.jozufozu.flywheel.backend.engine.instancing; +package com.jozufozu.flywheel.gl.buffer; -import com.jozufozu.flywheel.gl.buffer.GlBufferType; import com.mojang.blaze3d.vertex.VertexFormat; public class ElementBuffer { diff --git a/src/main/java/com/jozufozu/flywheel/gl/shader/GlProgram.java b/src/main/java/com/jozufozu/flywheel/gl/shader/GlProgram.java index 58459e5ae..e3dac154e 100644 --- a/src/main/java/com/jozufozu/flywheel/gl/shader/GlProgram.java +++ b/src/main/java/com/jozufozu/flywheel/gl/shader/GlProgram.java @@ -4,21 +4,20 @@ import static org.lwjgl.opengl.GL20.glDeleteProgram; import static org.lwjgl.opengl.GL20.glGetUniformLocation; import static org.lwjgl.opengl.GL20.glUniform1i; -import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.uniform.UniformBuffer; +import org.slf4j.Logger; + import com.jozufozu.flywheel.gl.GlObject; import com.mojang.blaze3d.shaders.ProgramManager; +import com.mojang.logging.LogUtils; public class GlProgram extends GlObject { + private static final Logger LOGGER = LogUtils.getLogger(); public GlProgram(int handle) { setHandle(handle); } public void bind() { - // TODO: bind textures? - UniformBuffer.getInstance() - .sync(); ProgramManager.glUseProgram(handle()); } @@ -36,7 +35,7 @@ public class GlProgram extends GlObject { int index = glGetUniformLocation(this.handle(), uniform); if (index < 0) { - Backend.LOGGER.debug("No active uniform '{}' exists. Could be unused.", uniform); + LOGGER.debug("No active uniform '{}' exists. Could be unused.", uniform); } return index; diff --git a/src/main/java/com/jozufozu/flywheel/api/pipeline/SourceComponent.java b/src/main/java/com/jozufozu/flywheel/glsl/SourceComponent.java similarity index 83% rename from src/main/java/com/jozufozu/flywheel/api/pipeline/SourceComponent.java rename to src/main/java/com/jozufozu/flywheel/glsl/SourceComponent.java index f7a2931cc..c0cdd38aa 100644 --- a/src/main/java/com/jozufozu/flywheel/api/pipeline/SourceComponent.java +++ b/src/main/java/com/jozufozu/flywheel/glsl/SourceComponent.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.api.pipeline; +package com.jozufozu.flywheel.glsl; import java.util.Collection; diff --git a/src/main/java/com/jozufozu/flywheel/glsl/SourceFile.java b/src/main/java/com/jozufozu/flywheel/glsl/SourceFile.java index 1bed80c4c..5337f7e79 100644 --- a/src/main/java/com/jozufozu/flywheel/glsl/SourceFile.java +++ b/src/main/java/com/jozufozu/flywheel/glsl/SourceFile.java @@ -9,7 +9,6 @@ import java.util.regex.Matcher; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.glsl.parse.Import; import com.jozufozu.flywheel.glsl.parse.ShaderField; import com.jozufozu.flywheel.glsl.parse.ShaderFunction; diff --git a/src/main/java/com/jozufozu/flywheel/glsl/error/ErrorReporter.java b/src/main/java/com/jozufozu/flywheel/glsl/error/ErrorReporter.java index cce42d559..9d534c029 100644 --- a/src/main/java/com/jozufozu/flywheel/glsl/error/ErrorReporter.java +++ b/src/main/java/com/jozufozu/flywheel/glsl/error/ErrorReporter.java @@ -4,14 +4,17 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -import com.jozufozu.flywheel.Flywheel; +import org.slf4j.Logger; + import com.jozufozu.flywheel.glsl.ShaderLoadingException; import com.jozufozu.flywheel.glsl.SourceFile; import com.jozufozu.flywheel.glsl.span.Span; import com.jozufozu.flywheel.util.FlwUtil; import com.jozufozu.flywheel.util.StringUtil; +import com.mojang.logging.LogUtils; public class ErrorReporter { + private static final Logger LOGGER = LogUtils.getLogger(); private final List reportedErrors = new ArrayList<>(); @@ -107,7 +110,7 @@ public class ErrorReporter { .append('\n'); } - Flywheel.LOGGER.error(builder.toString()); + LOGGER.error(builder.toString()); } } diff --git a/src/main/java/com/jozufozu/flywheel/glsl/error/package-info.java b/src/main/java/com/jozufozu/flywheel/glsl/error/package-info.java deleted file mode 100644 index dec2a6ef1..000000000 --- a/src/main/java/com/jozufozu/flywheel/glsl/error/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.jozufozu.flywheel.glsl.error; - -import javax.annotation.ParametersAreNonnullByDefault; - -import net.minecraft.MethodsReturnNonnullByDefault; diff --git a/src/main/java/com/jozufozu/flywheel/handler/EntityWorldHandler.java b/src/main/java/com/jozufozu/flywheel/handler/EntityWorldHandler.java index c062705dd..d0889e5ca 100644 --- a/src/main/java/com/jozufozu/flywheel/handler/EntityWorldHandler.java +++ b/src/main/java/com/jozufozu/flywheel/handler/EntityWorldHandler.java @@ -1,20 +1,19 @@ package com.jozufozu.flywheel.handler; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.EntityLeaveWorldEvent; public class EntityWorldHandler { - public static void onEntityJoinWorld(EntityJoinWorldEvent event) { - if (event.getWorld().isClientSide && Backend.isOn()) InstancedRenderDispatcher.getEntities(event.getWorld()) + if (event.getWorld().isClientSide && BackendManager.isOn()) InstancedRenderDispatcher.getEntities(event.getWorld()) .queueAdd(event.getEntity()); } public static void onEntityLeaveWorld(EntityLeaveWorldEvent event) { - if (event.getWorld().isClientSide && Backend.isOn()) InstancedRenderDispatcher.getEntities(event.getWorld()) + if (event.getWorld().isClientSide && BackendManager.isOn()) InstancedRenderDispatcher.getEntities(event.getWorld()) .remove(event.getEntity()); } } diff --git a/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java index ea2375778..6d8ca0e17 100644 --- a/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java @@ -3,7 +3,7 @@ package com.jozufozu.flywheel.handler; import java.util.ArrayList; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.lib.light.LightUpdater; import com.jozufozu.flywheel.lib.memory.FlwMemoryTracker; @@ -34,7 +34,7 @@ public class ForgeEvents { } public static void tickLight(TickEvent.ClientTickEvent event) { - if (event.phase == TickEvent.Phase.END && Backend.isGameActive()) { + if (event.phase == TickEvent.Phase.END && BackendUtil.isGameActive()) { LightUpdater.get(Minecraft.getInstance().level) .tick(); } diff --git a/src/main/java/com/jozufozu/flywheel/impl/BackendManagerImpl.java b/src/main/java/com/jozufozu/flywheel/impl/BackendManagerImpl.java new file mode 100644 index 000000000..a1bee4e39 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/impl/BackendManagerImpl.java @@ -0,0 +1,56 @@ +package com.jozufozu.flywheel.impl; + +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; + +import com.jozufozu.flywheel.api.backend.Backend; +import com.jozufozu.flywheel.config.FlwConfig; +import com.jozufozu.flywheel.lib.backend.Backends; +import com.mojang.logging.LogUtils; + +public final class BackendManagerImpl { + private static final Logger LOGGER = LogUtils.getLogger(); + private static final Backend DEFAULT_BACKEND = findDefaultBackend(); + private static Backend backend; + + @Nullable + public static Backend getBackend() { + return backend; + } + + public static String getBackendDescriptor() { + return backend == null ? "Uninitialized" : backend.getProperName(); + } + + public static boolean isOn() { + return backend != null && backend != Backends.OFF; + } + + public static void refresh() { + backend = chooseBackend(); + } + + public static Backend getDefaultBackend() { + return DEFAULT_BACKEND; + } + + private static Backend chooseBackend() { + var preferred = FlwConfig.get().getBackend(); + var actual = preferred.findFallback(); + + if (preferred != actual) { + LOGGER.warn("Flywheel backend fell back from '{}' to '{}'", Backend.REGISTRY.getId(preferred), Backend.REGISTRY.getId(actual)); + } + + return actual; + } + + private static Backend findDefaultBackend() { + // TODO: Automatically select the best default config based on the user's driver + // TODO: Figure out how this will work if custom backends are registered + return Backends.INDIRECT; + } + + private BackendManagerImpl() { + } +} diff --git a/src/main/java/com/jozufozu/flywheel/impl/IdRegistryImpl.java b/src/main/java/com/jozufozu/flywheel/impl/IdRegistryImpl.java new file mode 100644 index 000000000..0493e402f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/impl/IdRegistryImpl.java @@ -0,0 +1,117 @@ +package com.jozufozu.flywheel.impl; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Set; + +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Unmodifiable; + +import com.jozufozu.flywheel.api.registry.IdRegistry; + +import it.unimi.dsi.fastutil.objects.Object2ReferenceMap; +import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectList; +import it.unimi.dsi.fastutil.objects.ObjectSet; +import it.unimi.dsi.fastutil.objects.ObjectSets; +import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ReferenceCollection; +import it.unimi.dsi.fastutil.objects.ReferenceCollections; +import net.minecraft.resources.ResourceLocation; + +public class IdRegistryImpl implements IdRegistry { + private static final ObjectList> ALL = new ObjectArrayList<>(); + + private final Object2ReferenceMap map = new Object2ReferenceOpenHashMap<>(); + private final Reference2ObjectMap reverseMap = new Reference2ObjectOpenHashMap<>(); + private final ObjectSet keysView = ObjectSets.unmodifiable(map.keySet()); + private final ReferenceCollection valuesView = ReferenceCollections.unmodifiable(map.values()); + private final ObjectList freezeCallbacks = new ObjectArrayList<>(); + private boolean frozen; + + private IdRegistryImpl() { + ALL.add(this); + } + + public static IdRegistry create() { + return new IdRegistryImpl<>(); + } + + @Override + public void register(ResourceLocation id, T object) { + if (frozen) { + throw new IllegalStateException("Cannot register to frozen registry!"); + } + T oldValue = map.put(id, object); + if (oldValue != null) { + throw new IllegalArgumentException("Cannot override registration for ID '" + id + "'!"); + } + ResourceLocation oldId = reverseMap.put(object, id); + if (oldId != null) { + throw new IllegalArgumentException("Cannot override ID '" + id + "' with registration for ID '" + oldId + "'!"); + } + } + + @Override + public S registerAndGet(ResourceLocation id, S object) { + register(id, object); + return object; + } + + @Override + @Nullable + public T get(ResourceLocation id) { + return map.get(id); + } + + @Override + @Nullable + public ResourceLocation getId(T object) { + return reverseMap.get(object); + } + + @Override + @Unmodifiable + public Set getAllIds() { + return keysView; + } + + @Override + @Unmodifiable + public Collection getAll() { + return valuesView; + } + + @Override + public void addFreezeCallback(Runnable callback) { + if (frozen) { + throw new IllegalStateException("Cannot add freeze callback to frozen registry!"); + } + freezeCallbacks.add(callback); + } + + @Override + public boolean isFrozen() { + return frozen; + } + + @Override + public Iterator iterator() { + return getAll().iterator(); + } + + public void freeze() { + frozen = true; + for (Runnable runnable : freezeCallbacks) { + runnable.run(); + } + } + + public static void freezeAll() { + for (IdRegistryImpl registry : ALL) { + registry.freeze(); + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/impl/RegistryImpl.java b/src/main/java/com/jozufozu/flywheel/impl/RegistryImpl.java new file mode 100644 index 000000000..4e7a8b1c5 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/impl/RegistryImpl.java @@ -0,0 +1,101 @@ +package com.jozufozu.flywheel.impl; + +import java.util.Iterator; +import java.util.Set; + +import org.jetbrains.annotations.Unmodifiable; + +import com.jozufozu.flywheel.api.registry.Registry; +import com.jozufozu.flywheel.api.uniform.ShaderUniforms; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectList; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import it.unimi.dsi.fastutil.objects.ObjectSet; +import it.unimi.dsi.fastutil.objects.ObjectSets; +import net.minecraft.resources.ResourceLocation; + +public class RegistryImpl implements Registry { + private static final ObjectList> ALL = new ObjectArrayList<>(); + + private final ObjectSet set = new ObjectOpenHashSet<>(); + private final ObjectSet setView = ObjectSets.unmodifiable(set); + private final ObjectList freezeCallbacks = new ObjectArrayList<>(); + private boolean frozen; + + private RegistryImpl() { + ALL.add(this); + } + + public static Registry create() { + return new RegistryImpl<>(); + } + + public static Registry createForShaderUniforms() { + return new RegistryImpl<>() { + private final ObjectSet files = new ObjectOpenHashSet<>(); + + @Override + public void register(T object) { + if (!files.add(object.uniformShader())) { + throw new IllegalArgumentException(); + } + super.register(object); + } + }; + } + + @Override + public void register(T object) { + if (frozen) { + throw new IllegalStateException("Cannot register to frozen registry!"); + } + boolean added = set.add(object); + if (!added) { + throw new IllegalArgumentException("Cannot override registration!"); + } + } + + @Override + public S registerAndGet(S object) { + register(object); + return object; + } + + @Override + @Unmodifiable + public Set getAll() { + return setView; + } + + @Override + public void addFreezeCallback(Runnable callback) { + if (frozen) { + throw new IllegalStateException("Cannot add freeze callback to frozen registry!"); + } + freezeCallbacks.add(callback); + } + + @Override + public boolean isFrozen() { + return frozen; + } + + @Override + public Iterator iterator() { + return getAll().iterator(); + } + + public void freeze() { + frozen = true; + for (Runnable runnable : freezeCallbacks) { + runnable.run(); + } + } + + public static void freezeAll() { + for (RegistryImpl registry : ALL) { + registry.freeze(); + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexFormatInfo.java b/src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexFormatInfo.java similarity index 97% rename from src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexFormatInfo.java rename to src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexFormatInfo.java index f5c0f0345..d0222ad75 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexFormatInfo.java +++ b/src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexFormatInfo.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.vertex; +package com.jozufozu.flywheel.impl.vertex; import com.mojang.blaze3d.vertex.DefaultVertexFormat; import com.mojang.blaze3d.vertex.VertexFormat; diff --git a/src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexListImpl.java b/src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexListImpl.java similarity index 99% rename from src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexListImpl.java rename to src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexListImpl.java index 4bf8aa6da..125143446 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexListImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexListImpl.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.vertex; +package com.jozufozu.flywheel.impl.vertex; import org.lwjgl.system.MemoryUtil; diff --git a/src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexListProviderImpl.java b/src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexListProviderImpl.java similarity index 92% rename from src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexListProviderImpl.java rename to src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexListProviderImpl.java index 71f75c014..186cd628a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/vertex/InferredVertexListProviderImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/vertex/InferredVertexListProviderImpl.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.vertex; +package com.jozufozu.flywheel.impl.vertex; import com.jozufozu.flywheel.api.vertex.ReusableVertexList; import com.jozufozu.flywheel.api.vertex.VertexListProvider; diff --git a/src/main/java/com/jozufozu/flywheel/lib/backend/BackendTypes.java b/src/main/java/com/jozufozu/flywheel/lib/backend/Backends.java similarity index 52% rename from src/main/java/com/jozufozu/flywheel/lib/backend/BackendTypes.java rename to src/main/java/com/jozufozu/flywheel/lib/backend/Backends.java index 91f5f8442..354a7e70f 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/backend/BackendTypes.java +++ b/src/main/java/com/jozufozu/flywheel/lib/backend/Backends.java @@ -1,16 +1,7 @@ package com.jozufozu.flywheel.lib.backend; -import java.util.Collection; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Collectors; - -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.backend.BackendType; -import com.jozufozu.flywheel.api.pipeline.Pipeline; +import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.api.backend.Backend; import com.jozufozu.flywheel.backend.engine.batching.BatchingEngine; import com.jozufozu.flywheel.backend.engine.indirect.IndirectEngine; import com.jozufozu.flywheel.backend.engine.instancing.InstancingEngine; @@ -22,90 +13,54 @@ import com.jozufozu.flywheel.lib.util.ShadersModHandler; import net.minecraft.ChatFormatting; import net.minecraft.network.chat.TextComponent; -public class BackendTypes { - public static final Map BACKEND_TYPES = new HashMap<>(); - - public static final BackendType OFF = SimpleBackendType.builder() +public class Backends { + public static final Backend OFF = SimpleBackend.builder() .properName("Off") - .shortName("off") .engineMessage(new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED)) .engineSupplier(() -> { throw new IllegalStateException("Cannot create engine when backend is off."); }) - .fallback(() -> BackendTypes.OFF) + .fallback(() -> Backends.OFF) .supported(() -> true) - .register(); + .register(Flywheel.rl("off")); /** * Use a thread pool to buffer instances in parallel on the CPU. */ - public static final BackendType BATCHING = SimpleBackendType.builder() + public static final Backend BATCHING = SimpleBackend.builder() .properName("Parallel Batching") - .shortName("batching") .engineMessage(new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN)) .engineSupplier(BatchingEngine::new) - .fallback(() -> BackendTypes.OFF) + .fallback(() -> Backends.OFF) .supported(() -> !ShadersModHandler.isShaderPackInUse()) - .register(); + .register(Flywheel.rl("batching")); /** * Use GPU instancing to render everything. */ - public static final BackendType INSTANCING = SimpleBackendType.builder() + public static final Backend INSTANCING = SimpleBackend.builder() .properName("GL33 Instanced Arrays") - .shortName("instancing") .engineMessage(new TextComponent("Using Instancing Engine").withStyle(ChatFormatting.GREEN)) .engineSupplier(() -> new InstancingEngine(Contexts.WORLD, 100 * 100)) - .fallback(() -> BackendTypes.BATCHING) + .fallback(() -> Backends.BATCHING) .supported(() -> !ShadersModHandler.isShaderPackInUse() && GlCompat.getInstance() .instancedArraysSupported()) .pipelineShader(Pipelines.INSTANCED_ARRAYS) - .register(); + .register(Flywheel.rl("instancing")); /** * Use Compute shaders to cull instances. */ - public static final BackendType INDIRECT = SimpleBackendType.builder() + public static final Backend INDIRECT = SimpleBackend.builder() .properName("GL46 Compute Culling") - .shortName("indirect") .engineMessage(new TextComponent("Using Indirect Engine").withStyle(ChatFormatting.GREEN)) .engineSupplier(() -> new IndirectEngine(Contexts.WORLD, 100 * 100)) - .fallback(() -> BackendTypes.INSTANCING) + .fallback(() -> Backends.INSTANCING) .supported(() -> !ShadersModHandler.isShaderPackInUse() && GlCompat.getInstance() .supportsIndirect()) .pipelineShader(Pipelines.INDIRECT) - .register(); - - public static BackendType register(BackendType type) { - BACKEND_TYPES.put(type.getShortName(), type); - return type; - } - - public static BackendType defaultForCurrentPC() { - // TODO: Automatically select the best default config based on the user's driver - return INDIRECT; - } - - @Nullable - public static BackendType getBackendType(String name) { - return BACKEND_TYPES.get(name.toLowerCase(Locale.ROOT)); - } - - public static Collection validNames() { - return BACKEND_TYPES.keySet(); - } + .register(Flywheel.rl("indirect")); public static void init() { - // noop - } - - - public static Collection availablePipelineShaders() { - return BACKEND_TYPES.values() - .stream() - .filter(BackendType::supported) - .map(BackendType::pipelineShader) - .filter(Objects::nonNull) - .collect(Collectors.toList()); } } diff --git a/src/main/java/com/jozufozu/flywheel/lib/backend/SimpleBackendType.java b/src/main/java/com/jozufozu/flywheel/lib/backend/SimpleBackend.java similarity index 67% rename from src/main/java/com/jozufozu/flywheel/lib/backend/SimpleBackendType.java rename to src/main/java/com/jozufozu/flywheel/lib/backend/SimpleBackend.java index a25bd51fc..97fdf8204 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/backend/SimpleBackendType.java +++ b/src/main/java/com/jozufozu/flywheel/lib/backend/SimpleBackend.java @@ -5,26 +5,23 @@ import java.util.function.Supplier; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.api.backend.BackendType; +import com.jozufozu.flywheel.api.backend.Backend; import com.jozufozu.flywheel.api.backend.Engine; import com.jozufozu.flywheel.api.pipeline.Pipeline; import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; -public class SimpleBackendType implements BackendType { - - +public class SimpleBackend implements Backend { private final String properName; - private final String shortName; private final Component engineMessage; private final Supplier engineSupplier; - private final Supplier fallback; + private final Supplier fallback; private final BooleanSupplier isSupported; private final Pipeline pipelineShader; - public SimpleBackendType(String properName, String shortName, Component engineMessage, Supplier engineSupplier, Supplier fallback, BooleanSupplier isSupported, @Nullable Pipeline pipelineShader) { + public SimpleBackend(String properName, Component engineMessage, Supplier engineSupplier, Supplier fallback, BooleanSupplier isSupported, @Nullable Pipeline pipelineShader) { this.properName = properName; - this.shortName = shortName; this.engineMessage = engineMessage; this.engineSupplier = engineSupplier; this.fallback = fallback; @@ -41,11 +38,6 @@ public class SimpleBackendType implements BackendType { return properName; } - @Override - public String getShortName() { - return shortName; - } - @Override public Component getEngineMessage() { return engineMessage; @@ -57,8 +49,8 @@ public class SimpleBackendType implements BackendType { } @Override - public BackendType findFallback() { - if (this.supported()) { + public Backend findFallback() { + if (this.isSupported()) { return this; } else { return fallback.get() @@ -67,7 +59,7 @@ public class SimpleBackendType implements BackendType { } @Override - public boolean supported() { + public boolean isSupported() { return isSupported.getAsBoolean(); } @@ -78,10 +70,9 @@ public class SimpleBackendType implements BackendType { public static class Builder { private String properName; - private String shortName; private Component engineMessage; private Supplier engineSupplier; - private Supplier fallback; + private Supplier fallback; private BooleanSupplier isSupported; private Pipeline pipelineShader; @@ -90,11 +81,6 @@ public class SimpleBackendType implements BackendType { return this; } - public Builder shortName(String shortName) { - this.shortName = shortName; - return this; - } - public Builder engineMessage(Component engineMessage) { this.engineMessage = engineMessage; return this; @@ -105,7 +91,7 @@ public class SimpleBackendType implements BackendType { return this; } - public Builder fallback(Supplier fallback) { + public Builder fallback(Supplier fallback) { this.fallback = fallback; return this; } @@ -120,8 +106,8 @@ public class SimpleBackendType implements BackendType { return this; } - public BackendType register() { - return BackendTypes.register(new SimpleBackendType(properName, shortName, engineMessage, engineSupplier, fallback, isSupported, pipelineShader)); + public Backend register(ResourceLocation id) { + return Backend.REGISTRY.registerAndGet(id, new SimpleBackend(properName, engineMessage, engineSupplier, fallback, isSupported, pipelineShader)); } } } diff --git a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java b/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java index de08ed077..a234f276c 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java +++ b/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java @@ -1,14 +1,14 @@ package com.jozufozu.flywheel.lib.context; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.component.ComponentRegistry; +import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.util.ResourceUtil; import net.minecraft.resources.ResourceLocation; public class Contexts { - public static final SimpleContext WORLD = ComponentRegistry.register(new SimpleContext(Files.WORLD_VERTEX, Files.WORLD_FRAGMENT)); - public static final SimpleContext CRUMBLING = ComponentRegistry.register(new SimpleContext(Files.WORLD_VERTEX, Files.CRUMBLING_FRAGMENT)); + public static final SimpleContext WORLD = Context.REGISTRY.registerAndGet(new SimpleContext(Files.WORLD_VERTEX, Files.WORLD_FRAGMENT)); + public static final SimpleContext CRUMBLING = Context.REGISTRY.registerAndGet(new SimpleContext(Files.WORLD_VERTEX, Files.CRUMBLING_FRAGMENT)); public static void init() { // noop diff --git a/src/main/java/com/jozufozu/flywheel/lib/format/Formats.java b/src/main/java/com/jozufozu/flywheel/lib/format/Formats.java index 08b3667e0..d48980171 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/format/Formats.java +++ b/src/main/java/com/jozufozu/flywheel/lib/format/Formats.java @@ -1,14 +1,14 @@ package com.jozufozu.flywheel.lib.format; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.component.ComponentRegistry; +import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.util.ResourceUtil; import net.minecraft.resources.ResourceLocation; public class Formats { - public static final BlockVertex BLOCK = ComponentRegistry.register(new BlockVertex()); - public static final PosTexNormalVertex POS_TEX_NORMAL = ComponentRegistry.register(new PosTexNormalVertex()); + public static final BlockVertex BLOCK = VertexType.REGISTRY.registerAndGet(new BlockVertex()); + public static final PosTexNormalVertex POS_TEX_NORMAL = VertexType.REGISTRY.registerAndGet(new PosTexNormalVertex()); public static void init() { // noop diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleBlockEntityInstancingController.java b/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleBlockEntityInstancingController.java new file mode 100644 index 000000000..99ee6b54f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleBlockEntityInstancingController.java @@ -0,0 +1,100 @@ +package com.jozufozu.flywheel.lib.instance; + +import java.util.Objects; +import java.util.function.BiFunction; +import java.util.function.Predicate; + +import com.jozufozu.flywheel.api.instance.InstancedRenderRegistry; +import com.jozufozu.flywheel.api.instance.blockentity.BlockEntityInstancingController; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; +import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; + +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; + +public class SimpleBlockEntityInstancingController implements BlockEntityInstancingController { + protected BiFunction> instanceFactory; + protected Predicate skipRender; + + public SimpleBlockEntityInstancingController(BiFunction> instanceFactory, Predicate skipRender) { + this.instanceFactory = instanceFactory; + this.skipRender = skipRender; + } + + @Override + public BlockEntityInstance createInstance(InstancerProvider instancerManager, T blockEntity) { + return instanceFactory.apply(instancerManager, blockEntity); + } + + @Override + public boolean shouldSkipRender(T blockEntity) { + return skipRender.test(blockEntity); + } + + /** + * Get an object to configure the instancing controller for the given block entity type. + * @param type The block entity type to configure. + * @param The type of the block entity. + * @return The configuration object. + */ + public static BlockEntityConfig configure(BlockEntityType type) { + return new BlockEntityConfig<>(type); + } + + /** + * An object to configure the instancing controller for a block entity. + * @param The type of the block entity. + */ + public static class BlockEntityConfig { + protected BlockEntityType type; + protected BiFunction> instanceFactory; + protected Predicate skipRender; + + public BlockEntityConfig(BlockEntityType type) { + this.type = type; + } + + /** + * Sets the instance factory for the block entity. + * @param instanceFactory The instance factory. + * @return {@code this} + */ + public BlockEntityConfig factory(BiFunction> instanceFactory) { + this.instanceFactory = instanceFactory; + return this; + } + + /** + * Sets a predicate to determine whether to skip rendering a block entity. + * @param skipRender The predicate. + * @return {@code this} + */ + public BlockEntityConfig skipRender(Predicate skipRender) { + this.skipRender = skipRender; + return this; + } + + /** + * Sets a predicate to always skip rendering for block entities of this type. + * @return {@code this} + */ + public BlockEntityConfig alwaysSkipRender() { + this.skipRender = be -> true; + return this; + } + + /** + * Constructs the block entity instancing controller, and sets it for the block entity type. + * @return The block entity instancing controller. + */ + public SimpleBlockEntityInstancingController apply() { + Objects.requireNonNull(instanceFactory, "Instance factory cannot be null!"); + if (skipRender == null) { + skipRender = be -> false; + } + SimpleBlockEntityInstancingController controller = new SimpleBlockEntityInstancingController<>(instanceFactory, skipRender); + InstancedRenderRegistry.setController(type, controller); + return controller; + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleEntityInstancingController.java b/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleEntityInstancingController.java new file mode 100644 index 000000000..780b18048 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/SimpleEntityInstancingController.java @@ -0,0 +1,100 @@ +package com.jozufozu.flywheel.lib.instance; + +import java.util.Objects; +import java.util.function.BiFunction; +import java.util.function.Predicate; + +import com.jozufozu.flywheel.api.instance.InstancedRenderRegistry; +import com.jozufozu.flywheel.api.instance.entity.EntityInstancingController; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; +import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; + +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; + +public class SimpleEntityInstancingController implements EntityInstancingController { + protected BiFunction> instanceFactory; + protected Predicate skipRender; + + public SimpleEntityInstancingController(BiFunction> instanceFactory, Predicate skipRender) { + this.instanceFactory = instanceFactory; + this.skipRender = skipRender; + } + + @Override + public EntityInstance createInstance(InstancerProvider instancerManager, T entity) { + return instanceFactory.apply(instancerManager, entity); + } + + @Override + public boolean shouldSkipRender(T entity) { + return skipRender.test(entity); + } + + /** + * Get an object to configure the instancing controller for the given entity type. + * @param type The entity type to configure. + * @param The type of the entity. + * @return The configuration object. + */ + public static EntityConfig configure(EntityType type) { + return new EntityConfig<>(type); + } + + /** + * An object to configure the instancing controller for an entity. + * @param The type of the entity. + */ + public static class EntityConfig { + protected EntityType type; + protected BiFunction> instanceFactory; + protected Predicate skipRender; + + public EntityConfig(EntityType type) { + this.type = type; + } + + /** + * Sets the instance factory for the entity. + * @param instanceFactory The instance factory. + * @return {@code this} + */ + public EntityConfig factory(BiFunction> instanceFactory) { + this.instanceFactory = instanceFactory; + return this; + } + + /** + * Sets a predicate to determine whether to skip rendering an entity. + * @param skipRender The predicate. + * @return {@code this} + */ + public EntityConfig skipRender(Predicate skipRender) { + this.skipRender = skipRender; + return this; + } + + /** + * Sets a predicate to always skip rendering for entities of this type. + * @return {@code this} + */ + public EntityConfig alwaysSkipRender() { + this.skipRender = entity -> true; + return this; + } + + /** + * Constructs the entity instancing controller, and sets it for the entity type. + * @return The entity instancing controller. + */ + public SimpleEntityInstancingController apply() { + Objects.requireNonNull(instanceFactory, "Instance factory cannot be null!"); + if (skipRender == null) { + skipRender = entity -> false; + } + SimpleEntityInstancingController controller = new SimpleEntityInstancingController<>(instanceFactory, skipRender); + InstancedRenderRegistry.setController(type, controller); + return controller; + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java b/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java index 06f6b89f6..52bba12df 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java @@ -5,9 +5,9 @@ import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.stream.Stream; -import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.task.WorkGroup; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.lib.box.ImmutableBox; +import com.jozufozu.flywheel.lib.task.WorkGroup; import com.jozufozu.flywheel.util.FlwUtil; import com.jozufozu.flywheel.util.WorldAttached; @@ -68,7 +68,7 @@ public class LightUpdater { } }) .onComplete(() -> listeners.forEach(this::addListener)) - .execute(Backend.getTaskExecutor()); + .execute(BackendUtil.getTaskExecutor()); } /** diff --git a/src/main/java/com/jozufozu/flywheel/lib/material/MaterialIndicies.java b/src/main/java/com/jozufozu/flywheel/lib/material/MaterialIndicies.java new file mode 100644 index 000000000..74d33d46a --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/material/MaterialIndicies.java @@ -0,0 +1,147 @@ +package com.jozufozu.flywheel.lib.material; + +import java.util.List; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Unmodifiable; + +import com.jozufozu.flywheel.api.material.Material; + +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntMaps; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import it.unimi.dsi.fastutil.objects.ObjectList; +import it.unimi.dsi.fastutil.objects.ObjectLists; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import it.unimi.dsi.fastutil.objects.ObjectSet; +import it.unimi.dsi.fastutil.objects.Reference2IntMap; +import it.unimi.dsi.fastutil.objects.Reference2IntMaps; +import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; +import net.minecraft.resources.ResourceLocation; + +// TODO: add messages to exceptions +public class MaterialIndicies { + private static Reference2IntMap materialIndicies; + private static Object2IntMap vertexShaderIndicies; + private static Object2IntMap fragmentShaderIndicies; + private static ObjectList materialsByIndex; + private static ObjectList vertexShadersByIndex; + private static ObjectList fragmentShadersByIndex; + private static boolean initialized; + + public static int getMaterialIndex(Material material) { + if (!initialized) { + throw new IllegalStateException(); + } + return materialIndicies.getInt(material); + } + + public static int getVertexShaderIndex(ResourceLocation vertexShader) { + if (!initialized) { + throw new IllegalStateException(); + } + return vertexShaderIndicies.getInt(vertexShader); + } + + public static int getFragmentShaderIndex(ResourceLocation fragmentShader) { + if (!initialized) { + throw new IllegalStateException(); + } + return fragmentShaderIndicies.getInt(fragmentShader); + } + + public static Material getMaterial(int index) { + if (!initialized) { + throw new IllegalStateException(); + } + return materialsByIndex.get(index); + } + + public static ResourceLocation getVertexShader(int index) { + if (!initialized) { + throw new IllegalStateException(); + } + return vertexShadersByIndex.get(index); + } + + public static ResourceLocation getFragmentShader(int index) { + if (!initialized) { + throw new IllegalStateException(); + } + return fragmentShadersByIndex.get(index); + } + + @Unmodifiable + public static List getAllMaterials() { + if (!initialized) { + throw new IllegalStateException(); + } + return materialsByIndex; + } + + @Unmodifiable + public static List getAllVertexShaders() { + if (!initialized) { + throw new IllegalStateException(); + } + return vertexShadersByIndex; + } + + @Unmodifiable + public static List getAllFragmentShaders() { + if (!initialized) { + throw new IllegalStateException(); + } + return fragmentShadersByIndex; + } + + private static void initInner() { + int amount = Material.REGISTRY.getAll().size(); + + Reference2IntMap materialIndicies = new Reference2IntOpenHashMap<>(); + Object2IntMap vertexShaderIndicies = new Object2IntOpenHashMap<>(); + Object2IntMap fragmentShaderIndicies = new Object2IntOpenHashMap<>(); + ObjectList materialsByIndex = new ObjectArrayList<>(amount); + ObjectList vertexShadersByIndex = new ObjectArrayList<>(amount); + ObjectList fragmentShadersByIndex = new ObjectArrayList<>(amount); + + ObjectSet allVertexShaders = new ObjectOpenHashSet<>(); + ObjectSet allFragmentShaders = new ObjectOpenHashSet<>(); + + int materialIndex = 0; + int vertexShaderIndex = 0; + int fragmentShaderIndex = 0; + for (Material material : Material.REGISTRY) { + materialIndicies.put(material, materialIndex); + materialsByIndex.add(material); + materialIndex++; + ResourceLocation vertexShader = material.vertexShader(); + if (allVertexShaders.add(vertexShader)) { + vertexShaderIndicies.put(vertexShader, vertexShaderIndex); + vertexShadersByIndex.add(vertexShader); + vertexShaderIndex++; + } + ResourceLocation fragmentShader = material.fragmentShader(); + if (allFragmentShaders.add(fragmentShader)) { + fragmentShaderIndicies.put(fragmentShader, fragmentShaderIndex); + fragmentShadersByIndex.add(fragmentShader); + fragmentShaderIndex++; + } + } + + MaterialIndicies.materialIndicies = Reference2IntMaps.unmodifiable(materialIndicies); + MaterialIndicies.vertexShaderIndicies = Object2IntMaps.unmodifiable(vertexShaderIndicies); + MaterialIndicies.fragmentShaderIndicies = Object2IntMaps.unmodifiable(fragmentShaderIndicies); + MaterialIndicies.materialsByIndex = ObjectLists.unmodifiable(materialsByIndex); + MaterialIndicies.vertexShadersByIndex = ObjectLists.unmodifiable(vertexShadersByIndex); + MaterialIndicies.fragmentShadersByIndex = ObjectLists.unmodifiable(fragmentShadersByIndex); + + initialized = true; + } + + @ApiStatus.Internal + public static void init() { + Material.REGISTRY.addFreezeCallback(MaterialIndicies::initInner); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java b/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java index 6b3cffbe6..f21a809c2 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/lib/material/SimpleMaterial.java @@ -1,6 +1,5 @@ package com.jozufozu.flywheel.lib.material; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.material.Material; import net.minecraft.client.renderer.RenderStateShard; @@ -107,7 +106,7 @@ public class SimpleMaterial implements Material { } public SimpleMaterial register() { - return ComponentRegistry.register(new SimpleMaterial(vertexShader, fragmentShader, setup, clear, batchingRenderType, vertexTransformer)); + return Material.REGISTRY.registerAndGet(new SimpleMaterial(vertexShader, fragmentShader, setup, clear, batchingRenderType, vertexTransformer)); } private static Runnable chain(Runnable runnable1, Runnable runnable2) { diff --git a/src/main/java/com/jozufozu/flywheel/lib/pipeline/SimplePipeline.java b/src/main/java/com/jozufozu/flywheel/lib/pipeline/SimplePipeline.java index df4b19b44..9e384b7bf 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/pipeline/SimplePipeline.java +++ b/src/main/java/com/jozufozu/flywheel/lib/pipeline/SimplePipeline.java @@ -1,9 +1,9 @@ package com.jozufozu.flywheel.lib.pipeline; import com.jozufozu.flywheel.api.pipeline.Pipeline; -import com.jozufozu.flywheel.api.pipeline.SourceComponent; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.gl.GLSLVersion; +import com.jozufozu.flywheel.glsl.SourceComponent; import net.minecraft.resources.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/lib/struct/StructTypes.java b/src/main/java/com/jozufozu/flywheel/lib/struct/StructTypes.java index ad607616d..fddeb37c9 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/struct/StructTypes.java +++ b/src/main/java/com/jozufozu/flywheel/lib/struct/StructTypes.java @@ -1,15 +1,14 @@ package com.jozufozu.flywheel.lib.struct; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.struct.StructType; import com.jozufozu.flywheel.util.ResourceUtil; import net.minecraft.resources.ResourceLocation; public class StructTypes { - public static final StructType TRANSFORMED = ComponentRegistry.register(new TransformedType()); - public static final StructType ORIENTED = ComponentRegistry.register(new OrientedType()); + public static final StructType TRANSFORMED = StructType.REGISTRY.registerAndGet(new TransformedType()); + public static final StructType ORIENTED = StructType.REGISTRY.registerAndGet(new OrientedType()); public static void init() { // noop diff --git a/src/main/java/com/jozufozu/flywheel/backend/task/WorkGroup.java b/src/main/java/com/jozufozu/flywheel/lib/task/WorkGroup.java similarity index 97% rename from src/main/java/com/jozufozu/flywheel/backend/task/WorkGroup.java rename to src/main/java/com/jozufozu/flywheel/lib/task/WorkGroup.java index 904687557..82dee6bee 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/task/WorkGroup.java +++ b/src/main/java/com/jozufozu/flywheel/lib/task/WorkGroup.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.task; +package com.jozufozu.flywheel.lib.task; import java.util.Iterator; import java.util.concurrent.Executor; diff --git a/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java b/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java index aac852689..7010671ce 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java +++ b/src/main/java/com/jozufozu/flywheel/lib/uniform/FlwShaderUniforms.java @@ -5,7 +5,6 @@ import java.util.function.Consumer; import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.component.ComponentRegistry; import com.jozufozu.flywheel.api.event.BeginFrameEvent; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.uniform.ShaderUniforms; @@ -20,7 +19,7 @@ import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.MinecraftForge; public class FlwShaderUniforms implements ShaderUniforms { - public static final FlwShaderUniforms INSTANCE = ComponentRegistry.register(new FlwShaderUniforms()); + public static final FlwShaderUniforms INSTANCE = ShaderUniforms.REGISTRY.registerAndGet(new FlwShaderUniforms()); public static final ResourceLocation FILE = Flywheel.rl("uniform/flywheel.glsl"); public static final int SIZE = 224; diff --git a/src/main/java/com/jozufozu/flywheel/lib/util/QuadConverter.java b/src/main/java/com/jozufozu/flywheel/lib/util/QuadConverter.java index 50683e23f..95ec577e9 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/util/QuadConverter.java +++ b/src/main/java/com/jozufozu/flywheel/lib/util/QuadConverter.java @@ -7,8 +7,8 @@ import org.lwjgl.opengl.GL32C; import org.lwjgl.system.MemoryUtil; import com.jozufozu.flywheel.api.event.ReloadRenderersEvent; -import com.jozufozu.flywheel.backend.engine.instancing.ElementBuffer; import com.jozufozu.flywheel.gl.GlNumericType; +import com.jozufozu.flywheel.gl.buffer.ElementBuffer; import com.jozufozu.flywheel.gl.buffer.GlBufferType; import com.jozufozu.flywheel.gl.buffer.GlBufferUsage; import com.mojang.blaze3d.vertex.VertexFormat; diff --git a/src/main/java/com/jozufozu/flywheel/lib/util/ShadersModHandler.java b/src/main/java/com/jozufozu/flywheel/lib/util/ShadersModHandler.java index 9aaf0f46c..0d4546980 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/util/ShadersModHandler.java +++ b/src/main/java/com/jozufozu/flywheel/lib/util/ShadersModHandler.java @@ -6,7 +6,9 @@ import java.util.function.BooleanSupplier; import javax.annotation.Nullable; -import com.jozufozu.flywheel.backend.Backend; +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; import net.irisshaders.iris.api.v0.IrisApi; import net.minecraft.client.Camera; @@ -14,6 +16,8 @@ import net.minecraft.client.renderer.culling.Frustum; import net.minecraftforge.fml.ModList; public final class ShadersModHandler { + private static final Logger LOGGER = LogUtils.getLogger(); + public static final String OPTIFINE_ROOT_PACKAGE = "net.optifine"; public static final String SHADER_PACKAGE = "net.optifine.shaders"; @@ -30,13 +34,13 @@ public final class ShadersModHandler { // optfine and oculus are assumed to be mutually exclusive if (isOptifineInstalled) { - Backend.LOGGER.info("Optifine detected."); + LOGGER.info("Optifine detected."); internalHandler = new Optifine(); } else if (isOculusLoaded) { - Backend.LOGGER.info("Oculus detected."); + LOGGER.info("Oculus detected."); internalHandler = new Oculus(); } else { - Backend.LOGGER.info("No shaders mod detected."); + LOGGER.info("No shaders mod detected."); internalHandler = new InternalHandler() {}; } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityTypeMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityTypeMixin.java index 78d3b68d4..6f54f1ee9 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityTypeMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityTypeMixin.java @@ -4,7 +4,7 @@ import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; -import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstancingController; +import com.jozufozu.flywheel.api.instance.blockentity.BlockEntityInstancingController; import com.jozufozu.flywheel.extension.BlockEntityTypeExtension; import net.minecraft.world.level.block.entity.BlockEntity; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/ClientLevelMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/ClientLevelMixin.java index c77f83d05..51565452e 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/ClientLevelMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/ClientLevelMixin.java @@ -9,8 +9,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.google.common.collect.Lists; -import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; +import com.jozufozu.flywheel.api.backend.BackendManager; +import com.jozufozu.flywheel.api.instance.InstancedRenderRegistry; import com.jozufozu.flywheel.extension.ClientLevelExtension; import net.minecraft.client.multiplayer.ClientLevel; @@ -29,7 +29,7 @@ public abstract class ClientLevelMixin implements ClientLevelExtension { @Inject(method = "entitiesForRendering", at = @At("RETURN"), cancellable = true) private void flywheel$filterEntities(CallbackInfoReturnable> cir) { - if (Backend.isOn()) { + if (BackendManager.isOn()) { Iterable entities = cir.getReturnValue(); ArrayList filtered = Lists.newArrayList(entities); diff --git a/src/main/java/com/jozufozu/flywheel/mixin/EntityTypeMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/EntityTypeMixin.java index 89620994d..e19a4fca9 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/EntityTypeMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/EntityTypeMixin.java @@ -4,7 +4,7 @@ import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; -import com.jozufozu.flywheel.backend.instancing.entity.EntityInstancingController; +import com.jozufozu.flywheel.api.instance.entity.EntityInstancingController; import com.jozufozu.flywheel.extension.EntityTypeExtension; import net.minecraft.world.entity.Entity; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java index ee4ec1be8..0da661d5e 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LevelRendererMixin.java @@ -10,12 +10,12 @@ import org.spongepowered.asm.mixin.injection.At.Shift; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.api.event.BeginFrameEvent; import com.jozufozu.flywheel.api.event.ReloadRenderersEvent; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.event.RenderStageEvent; -import com.jozufozu.flywheel.backend.Backend; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; @@ -56,7 +56,7 @@ public class LevelRendererMixin { @Inject(at = @At("TAIL"), method = "allChanged") private void flywheel$refresh(CallbackInfo ci) { - Backend.refresh(); + BackendManager.refresh(); MinecraftForge.EVENT_BUS.post(new ReloadRenderersEvent(level)); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java index 559ac6ba9..64cd248e8 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/ChunkRebuildHooksMixin.java @@ -7,9 +7,9 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.instance.InstancedRenderRegistry; +import com.jozufozu.flywheel.backend.BackendUtil; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher; import net.minecraft.world.level.block.entity.BlockEntity; @@ -18,7 +18,7 @@ import net.minecraft.world.level.block.entity.BlockEntity; public class ChunkRebuildHooksMixin { @Inject(method = "handleBlockEntity", at = @At("HEAD"), cancellable = true) private void flywheel$addAndFilterBEs(ChunkRenderDispatcher.CompiledChunk compiledChunk, Set set, E be, CallbackInfo ci) { - if (Backend.canUseInstancing(be.getLevel())) { + if (BackendUtil.canUseInstancing(be.getLevel())) { if (InstancedRenderRegistry.canInstance(be.getType())) InstancedRenderDispatcher.getBlockEntities(be.getLevel()).queueAdd(be); diff --git a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java index 7ca54cb1c..4c6ab5798 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceAddMixin.java @@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import net.minecraft.world.level.Level; @@ -23,7 +23,7 @@ public class InstanceAddMixin { @Inject(method = "setBlockEntity", at = @At(value = "INVOKE_ASSIGN", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;")) private void flywheel$onBlockEntityAdded(BlockEntity be, CallbackInfo ci) { - if (level.isClientSide && Backend.isOn()) { + if (level.isClientSide && BackendManager.isOn()) { InstancedRenderDispatcher.getBlockEntities(this.level) .add(be); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java index 2630faa34..4c504a462 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceRemoveMixin.java @@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import net.minecraft.client.multiplayer.ClientLevel; @@ -22,7 +22,7 @@ public class InstanceRemoveMixin { @Inject(at = @At("TAIL"), method = "setRemoved") private void flywheel$removeInstance(CallbackInfo ci) { - if (level instanceof ClientLevel && Backend.isOn()) { + if (level instanceof ClientLevel && BackendManager.isOn()) { InstancedRenderDispatcher.getBlockEntities(this.level) .remove((BlockEntity) (Object) this); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceUpdateMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceUpdateMixin.java index 72bfbd5e5..5674ae7dd 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceUpdateMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/instancemanage/InstanceUpdateMixin.java @@ -6,7 +6,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import net.minecraft.client.multiplayer.ClientLevel; @@ -25,7 +25,7 @@ public class InstanceUpdateMixin { */ @Inject(at = @At("TAIL"), method = "setBlockDirty(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;)V") private void flywheel$checkUpdate(BlockPos pos, BlockState lastState, BlockState newState, CallbackInfo ci) { - if (!Backend.isOn()) { + if (!BackendManager.isOn()) { return; } BlockEntity blockEntity = level.getBlockEntity(pos); diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java index 178d1378f..b13db86c1 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java @@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instancer.InstancedPart; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.SimpleLazyModel; @@ -31,7 +31,7 @@ public class BellInstance extends BlockEntityInstance implement private float lastRingTime = Float.NaN; - public BellInstance(InstancerManager instancerManager, BellBlockEntity blockEntity) { + public BellInstance(InstancerProvider instancerManager, BellBlockEntity blockEntity) { super(instancerManager, blockEntity); bell = createBellInstance() @@ -73,7 +73,7 @@ public class BellInstance extends BlockEntityInstance implement } private OrientedPart createBellInstance() { - return instancerManager.instancer(StructTypes.ORIENTED, MODEL, RenderStage.AFTER_BLOCK_ENTITIES) + return instancerManager.getInstancer(StructTypes.ORIENTED, MODEL, RenderStage.AFTER_BLOCK_ENTITIES) .createInstance(); } diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index 9488f6354..1e6a59f15 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instancer.InstancedPart; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.SimpleLazyModel; @@ -51,7 +51,7 @@ public class ChestInstance extends Block private float lastProgress = Float.NaN; - public ChestInstance(InstancerManager instancerManager, T blockEntity) { + public ChestInstance(InstancerProvider instancerManager, T blockEntity) { super(instancerManager, blockEntity); Block block = blockState.getBlock(); @@ -124,13 +124,13 @@ public class ChestInstance extends Block private OrientedPart baseInstance() { - return instancerManager.instancer(StructTypes.ORIENTED, BASE.apply(chestType, sprite), RenderStage.AFTER_BLOCK_ENTITIES) + return instancerManager.getInstancer(StructTypes.ORIENTED, BASE.apply(chestType, sprite), RenderStage.AFTER_BLOCK_ENTITIES) .createInstance(); } private TransformedPart lidInstance() { - return instancerManager.instancer(StructTypes.TRANSFORMED, LID.apply(chestType, sprite), RenderStage.AFTER_BLOCK_ENTITIES) + return instancerManager.getInstancer(StructTypes.TRANSFORMED, LID.apply(chestType, sprite), RenderStage.AFTER_BLOCK_ENTITIES) .createInstance(); } diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java index 7c5561ef8..2a2c60ad8 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.api.model.Mesh; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; import com.jozufozu.flywheel.lib.material.Materials; @@ -37,7 +37,7 @@ public class MinecartInstance extends EntityInstance private BlockState blockState; private boolean active; - public MinecartInstance(InstancerManager instancerManager, T entity) { + public MinecartInstance(InstancerProvider instancerManager, T entity) { super(instancerManager, entity); body = getBody(); @@ -168,12 +168,12 @@ public class MinecartInstance extends EntityInstance if (shape == RenderShape.INVISIBLE) return null; - return instancerManager.instancer(StructTypes.TRANSFORMED, Models.block(blockState), RenderStage.AFTER_ENTITIES) + return instancerManager.getInstancer(StructTypes.TRANSFORMED, Models.block(blockState), RenderStage.AFTER_ENTITIES) .createInstance(); } private TransformedPart getBody() { - return instancerManager.instancer(StructTypes.TRANSFORMED, MODEL, RenderStage.AFTER_ENTITIES) + return instancerManager.getInstancer(StructTypes.TRANSFORMED, MODEL, RenderStage.AFTER_ENTITIES) .createInstance(); } diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java index 3a423e3fb..289caf4eb 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java @@ -7,7 +7,7 @@ import java.util.function.Function; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instancer.InstancedPart; -import com.jozufozu.flywheel.api.instancer.InstancerManager; +import com.jozufozu.flywheel.api.instancer.InstancerProvider; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.SimpleLazyModel; @@ -41,7 +41,7 @@ public class ShulkerBoxInstance extends BlockEntityInstance createInstances(InstancerManager instancerManager) { + public Collection createInstances(InstancerProvider instancerManager) { effects.clear(); boids.clear(); for (int i = 0; i < INSTANCE_COUNT; i++) { @@ -242,14 +242,14 @@ public class ExampleEffect implements Effect { private final Boid self; TransformedPart instance; - public Instance(InstancerManager instancerManager, Level level, Boid self) { + public Instance(InstancerProvider instancerManager, Level level, Boid self) { super(instancerManager, level); this.self = self; } @Override public void init() { - instance = instancerManager.instancer(StructTypes.TRANSFORMED, Models.block(Blocks.SHROOMLIGHT.defaultBlockState()), RenderStage.AFTER_PARTICLES) + instance = instancerManager.getInstancer(StructTypes.TRANSFORMED, Models.block(Blocks.SHROOMLIGHT.defaultBlockState()), RenderStage.AFTER_PARTICLES) .createInstance(); instance.setBlockLight(15)