From 6648751ef72e7b214c7c520e465c3274003dda2e Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Fri, 7 Apr 2023 16:01:03 -0700 Subject: [PATCH] More clean up --- .../java/com/jozufozu/flywheel/Flywheel.java | 17 +- .../flywheel/api/backend/BackendManager.java | 5 +- .../flywheel/api/event/RenderContext.java | 2 +- .../api/instance/BlockEntityInstance.java | 2 +- .../api/instance/DynamicInstance.java | 2 +- .../api/instance/TickableInstance.java | 2 +- .../flywheel/api/instancer/Instancer.java | 2 + .../api/instancer/InstancerProvider.java | 1 + .../flywheel/api/material/Material.java | 8 +- .../material/MaterialVertexTransformer.java | 9 + .../com/jozufozu/flywheel/api/model/Mesh.java | 13 +- .../api/{instancer => struct}/Handle.java | 3 +- .../{instancer => struct}/InstancePart.java | 4 +- .../flywheel/api/struct/StructType.java | 11 +- .../api/struct/StructVertexTransformer.java | 9 + .../flywheel/api/struct/StructWriter.java | 5 +- .../flywheel/api/vertex/VertexList.java | 1 - .../flywheel/backend/BackendUtil.java | 56 ----- .../flywheel/{lib => }/backend/Backends.java | 14 +- .../com/jozufozu/flywheel/backend/Loader.java | 14 +- .../{lib/pipeline => backend}/Pipelines.java | 4 +- .../flywheel/backend/compile/FlwCompiler.java | 2 +- .../backend/compile/ShaderCompiler.java | 3 +- .../backend/engine/AbstractInstancer.java | 68 +++--- .../flywheel/backend/engine/HandleImpl.java | 3 +- .../flywheel/backend/engine/InstancerKey.java | 2 +- .../backend/engine/UniformBuffer.java | 6 +- .../engine/batching/BatchedMeshPool.java | 8 +- .../engine/batching/BatchingEngine.java | 2 +- .../batching/BatchingTransformManager.java | 4 +- .../backend/engine/batching/CPUInstancer.java | 19 +- .../backend/engine/batching/DrawBuffer.java | 2 +- .../engine/batching/DrawBufferSet.java | 3 - .../engine/batching/TransformCall.java | 45 +--- .../engine/indirect/IndirectComponent.java | 2 +- .../engine/indirect/IndirectCullingGroup.java | 8 +- .../backend/engine/indirect/IndirectDraw.java | 53 +++-- .../engine/indirect/IndirectDrawManager.java | 3 +- .../engine/indirect/IndirectDrawSet.java | 5 +- .../engine/indirect/IndirectEngine.java | 2 +- .../engine/indirect/IndirectInstancer.java | 37 ++- .../engine/indirect/IndirectMeshPool.java | 42 ++-- .../engine/indirect}/Textures.java | 2 +- .../backend/engine/instancing/DrawCall.java | 70 ++---- .../engine/instancing/GPUInstancer.java | 93 ++++---- .../engine/instancing/InstancedMeshPool.java | 15 +- .../instancing/InstancingDrawManager.java | 13 +- .../engine/instancing/InstancingEngine.java | 14 +- .../engine/instancing/ShaderState.java | 2 +- .../backend/task/FlwTaskExecutor.java | 18 ++ .../backend/task/ParallelTaskExecutor.java | 22 ++ .../flywheel/config/BackendArgument.java | 11 +- .../flywheel/glsl/error/ErrorReporter.java | 6 +- .../flywheel/handler/EntityWorldHandler.java | 6 +- .../flywheel/handler/ForgeEvents.java | 4 +- .../flywheel/impl/BackendManagerImpl.java | 71 ++++-- .../impl/instancing/InstanceWorld.java | 17 +- .../instancing/InstancedRenderDispatcher.java | 36 ++- .../manager/BlockEntityInstanceManager.java | 4 +- .../manager/EntityInstanceManager.java | 4 +- .../instancing/manager/InstanceManager.java | 12 +- .../impl/vertex/InferredVertexListImpl.java | 2 +- .../flywheel/lib/backend/SimpleBackend.java | 3 +- .../flywheel/lib/box/ImmutableBox.java | 2 +- .../jozufozu/flywheel/lib/box/MutableBox.java | 3 +- .../flywheel/lib/context/Contexts.java | 10 +- .../flywheel/lib/format/BlockVertexList.java | 2 +- .../jozufozu/flywheel/lib/format/Formats.java | 10 +- .../lib/format/PosTexNormalVertexList.java | 2 +- .../flywheel/lib/layout/CommonItems.java | 6 +- .../flywheel/lib/light/LightUpdater.java | 5 +- .../lib/material/MaterialIndices.java | 2 +- .../flywheel/lib/material/Materials.java | 13 +- .../flywheel/lib/material/SimpleMaterial.java | 12 +- .../math}/DiffuseLightCalculator.java | 2 +- .../{util => lib/math}/MatrixUtil.java | 34 ++- .../jozufozu/flywheel/lib/math/MoreMath.java | 166 ++++++++++++++ .../{util => lib/math}/RenderMath.java | 5 +- .../flywheel/lib/memory/FlwMemoryTracker.java | 2 +- .../flywheel/lib/model/ModelUtil.java | 19 +- .../jozufozu/flywheel/lib/model/Models.java | 2 +- .../jozufozu/flywheel/lib/model/QuadMesh.java | 23 ++ .../flywheel/lib/model/SimpleMesh.java | 3 +- .../model/buffering/BakedModelBuilder.java | 11 +- .../model/buffering/BlockModelBuilder.java | 11 +- .../buffering/MultiBlockModelBuilder.java | 11 +- .../flywheel/lib/modelpart/ModelPart.java | 16 +- ...PartBuilder.java => ModelPartBuilder.java} | 47 ++-- .../lib/modelpart/VertexWriterImpl.java | 2 +- .../lib/struct/AbstractInstancePart.java | 7 +- .../flywheel/lib/struct/ColoredLitPart.java | 7 +- .../jozufozu/flywheel/lib/struct/FlatLit.java | 2 +- .../flywheel/lib/struct/OrientedPart.java | 10 +- .../flywheel/lib/struct/OrientedType.java | 39 +--- .../flywheel/lib/struct/StructTypes.java | 10 +- .../flywheel/lib/struct/TransformedPart.java | 9 +- .../flywheel/lib/struct/TransformedType.java | 39 +--- .../lib/struct/TransformedWriter.java | 2 +- .../jozufozu/flywheel/lib/task/WorkGroup.java | 2 +- .../lib/uniform/FlwShaderUniforms.java | 6 +- .../{ => lib}/util/AnimationTickHolder.java | 5 +- .../flywheel/lib/util/QuadConverter.java | 1 - .../flywheel/lib/util/ShadersModHandler.java | 47 ++-- .../lib/vertex/VertexTransformations.java | 36 +++ .../flywheel/mixin/ClientLevelMixin.java | 4 +- .../flywheel/mixin/LevelRendererMixin.java | 3 - .../ChunkRebuildHooksMixin.java | 4 +- .../instancemanage/InstanceAddMixin.java | 4 +- .../instancemanage/InstanceRemoveMixin.java | 4 +- .../instancemanage/InstanceUpdateMixin.java | 4 +- .../com/jozufozu/flywheel/util/CodecUtil.java | 24 -- .../com/jozufozu/flywheel/util/FlwUtil.java | 217 ++++-------------- .../flywheel/vanilla/BellInstance.java | 4 +- .../flywheel/vanilla/ChestInstance.java | 4 +- .../flywheel/vanilla/MinecartInstance.java | 2 +- .../flywheel/vanilla/ShulkerBoxInstance.java | 4 +- .../flywheel/vanilla/VanillaInstances.java | 1 - .../vanilla/effect/ExampleEffect.java | 2 +- 118 files changed, 899 insertions(+), 953 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/api/material/MaterialVertexTransformer.java rename src/main/java/com/jozufozu/flywheel/api/{instancer => struct}/Handle.java (60%) rename src/main/java/com/jozufozu/flywheel/api/{instancer => struct}/InstancePart.java (52%) create mode 100644 src/main/java/com/jozufozu/flywheel/api/struct/StructVertexTransformer.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java rename src/main/java/com/jozufozu/flywheel/{lib => }/backend/Backends.java (80%) rename src/main/java/com/jozufozu/flywheel/{lib/pipeline => backend}/Pipelines.java (93%) rename src/main/java/com/jozufozu/flywheel/{util => backend/engine/indirect}/Textures.java (89%) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/task/FlwTaskExecutor.java rename src/main/java/com/jozufozu/flywheel/{util => lib/math}/DiffuseLightCalculator.java (91%) rename src/main/java/com/jozufozu/flywheel/{util => lib/math}/MatrixUtil.java (74%) create mode 100644 src/main/java/com/jozufozu/flywheel/lib/math/MoreMath.java rename src/main/java/com/jozufozu/flywheel/{util => lib/math}/RenderMath.java (95%) create mode 100644 src/main/java/com/jozufozu/flywheel/lib/model/QuadMesh.java rename src/main/java/com/jozufozu/flywheel/lib/modelpart/{PartBuilder.java => ModelPartBuilder.java} (89%) rename src/main/java/com/jozufozu/flywheel/{ => lib}/util/AnimationTickHolder.java (93%) create mode 100644 src/main/java/com/jozufozu/flywheel/lib/vertex/VertexTransformations.java delete mode 100644 src/main/java/com/jozufozu/flywheel/util/CodecUtil.java diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index d1d7a51f7..a7d057b1e 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -3,7 +3,9 @@ package com.jozufozu.flywheel; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.slf4j.Logger; +import com.jozufozu.flywheel.backend.Backends; import com.jozufozu.flywheel.backend.Loader; +import com.jozufozu.flywheel.backend.Pipelines; import com.jozufozu.flywheel.backend.engine.batching.DrawBuffer; import com.jozufozu.flywheel.config.BackendArgument; import com.jozufozu.flywheel.config.FlwCommands; @@ -14,14 +16,12 @@ import com.jozufozu.flywheel.impl.BackendManagerImpl; import com.jozufozu.flywheel.impl.IdRegistryImpl; import com.jozufozu.flywheel.impl.RegistryImpl; import com.jozufozu.flywheel.impl.instancing.InstancedRenderDispatcher; -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.MaterialIndices; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.Models; import com.jozufozu.flywheel.lib.model.PartialModel; -import com.jozufozu.flywheel.lib.pipeline.Pipelines; import com.jozufozu.flywheel.lib.struct.StructTypes; import com.jozufozu.flywheel.lib.util.QuadConverter; import com.jozufozu.flywheel.lib.util.ShadersModHandler; @@ -36,7 +36,6 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.fml.CrashReportCallables; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.IExtensionPoint; import net.minecraftforge.fml.ModLoadingContext; @@ -47,7 +46,6 @@ import net.minecraftforge.network.NetworkConstants; @Mod(Flywheel.ID) public class Flywheel { - public static final String ID = "flywheel"; public static final Logger LOGGER = LogUtils.getLogger(); private static ArtifactVersion version; @@ -78,11 +76,12 @@ public class Flywheel { private static void clientInit(IEventBus forgeEventBus, IEventBus modEventBus) { forgeEventBus.addListener(FlwCommands::registerClientCommands); + forgeEventBus.addListener(BackendManagerImpl::onReloadRenderers); + forgeEventBus.addListener(EventPriority.HIGHEST, QuadConverter::onReloadRenderers); forgeEventBus.addListener(Models::onReloadRenderers); forgeEventBus.addListener(DrawBuffer::onReloadRenderers); - forgeEventBus.addListener(InstancedRenderDispatcher::onReloadRenderers); forgeEventBus.addListener(InstancedRenderDispatcher::onRenderStage); forgeEventBus.addListener(InstancedRenderDispatcher::onBeginFrame); forgeEventBus.addListener(InstancedRenderDispatcher::tick); @@ -100,23 +99,23 @@ public class Flywheel { // forgeEventBus.addListener(ExampleEffect::tick); // forgeEventBus.addListener(ExampleEffect::onReload); - ShadersModHandler.init(); + BackendManagerImpl.init(); + Pipelines.init(); Backends.init(); Loader.init(); + ShadersModHandler.init(); + Formats.init(); StructTypes.init(); Materials.init(); Contexts.init(); - Pipelines.init(); MaterialIndices.init(); VanillaInstances.init(); - CrashReportCallables.registerCrashCallable("Flywheel Backend", BackendManagerImpl::getBackendNameForCrashReport); - // 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. diff --git a/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java b/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java index 5819bea8e..50b0042ae 100644 --- a/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java +++ b/src/main/java/com/jozufozu/flywheel/api/backend/BackendManager.java @@ -17,9 +17,8 @@ public final class BackendManager { 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 getOffBackend() { + return BackendManagerImpl.getOffBackend(); } public static Backend getDefaultBackend() { diff --git a/src/main/java/com/jozufozu/flywheel/api/event/RenderContext.java b/src/main/java/com/jozufozu/flywheel/api/event/RenderContext.java index a3cd89d06..58bed0d54 100644 --- a/src/main/java/com/jozufozu/flywheel/api/event/RenderContext.java +++ b/src/main/java/com/jozufozu/flywheel/api/event/RenderContext.java @@ -3,7 +3,7 @@ package com.jozufozu.flywheel.api.event; import org.jetbrains.annotations.NotNull; import org.joml.FrustumIntersection; -import com.jozufozu.flywheel.util.MatrixUtil; +import com.jozufozu.flywheel.lib.math.MatrixUtil; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix4f; diff --git a/src/main/java/com/jozufozu/flywheel/api/instance/BlockEntityInstance.java b/src/main/java/com/jozufozu/flywheel/api/instance/BlockEntityInstance.java index 0fb3a750e..b5c13a03a 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instance/BlockEntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/BlockEntityInstance.java @@ -2,7 +2,7 @@ package com.jozufozu.flywheel.api.instance; import java.util.List; -import com.jozufozu.flywheel.api.instancer.InstancePart; +import com.jozufozu.flywheel.api.struct.InstancePart; import net.minecraft.world.level.block.entity.BlockEntity; diff --git a/src/main/java/com/jozufozu/flywheel/api/instance/DynamicInstance.java b/src/main/java/com/jozufozu/flywheel/api/instance/DynamicInstance.java index 4f9835cb3..ea8c22f79 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instance/DynamicInstance.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/DynamicInstance.java @@ -2,8 +2,8 @@ package com.jozufozu.flywheel.api.instance; import org.joml.FrustumIntersection; -import com.jozufozu.flywheel.api.instancer.InstancePart; import com.jozufozu.flywheel.api.instancer.Instancer; +import com.jozufozu.flywheel.api.struct.InstancePart; /** * An interface giving {@link Instance}s a hook to have a function called at diff --git a/src/main/java/com/jozufozu/flywheel/api/instance/TickableInstance.java b/src/main/java/com/jozufozu/flywheel/api/instance/TickableInstance.java index e0c8fd77d..d7b0afb8d 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instance/TickableInstance.java +++ b/src/main/java/com/jozufozu/flywheel/api/instance/TickableInstance.java @@ -1,7 +1,7 @@ package com.jozufozu.flywheel.api.instance; -import com.jozufozu.flywheel.api.instancer.InstancePart; import com.jozufozu.flywheel.api.instancer.Instancer; +import com.jozufozu.flywheel.api.struct.InstancePart; /** * An interface giving {@link Instance}s a hook to have a function called at diff --git a/src/main/java/com/jozufozu/flywheel/api/instancer/Instancer.java b/src/main/java/com/jozufozu/flywheel/api/instancer/Instancer.java index 9be9e5633..8e9b01dba 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instancer/Instancer.java +++ b/src/main/java/com/jozufozu/flywheel/api/instancer/Instancer.java @@ -1,5 +1,7 @@ package com.jozufozu.flywheel.api.instancer; +import com.jozufozu.flywheel.api.struct.InstancePart; + /** * An instancer is how you interact with an instanced model. *
diff --git a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java b/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java index 21ea65f18..13c52909d 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java +++ b/src/main/java/com/jozufozu/flywheel/api/instancer/InstancerProvider.java @@ -2,6 +2,7 @@ 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.InstancePart; import com.jozufozu.flywheel.api.struct.StructType; public interface InstancerProvider { 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 224ded5ef..5a666eaff 100644 --- a/src/main/java/com/jozufozu/flywheel/api/material/Material.java +++ b/src/main/java/com/jozufozu/flywheel/api/material/Material.java @@ -1,10 +1,8 @@ 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; @@ -21,9 +19,5 @@ public interface Material { RenderType getBatchingRenderType(); - VertexTransformer getVertexTransformer(); - - interface VertexTransformer { - void transform(MutableVertexList vertexList, ClientLevel level); - } + MaterialVertexTransformer getVertexTransformer(); } diff --git a/src/main/java/com/jozufozu/flywheel/api/material/MaterialVertexTransformer.java b/src/main/java/com/jozufozu/flywheel/api/material/MaterialVertexTransformer.java new file mode 100644 index 000000000..223bd5cd9 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/material/MaterialVertexTransformer.java @@ -0,0 +1,9 @@ +package com.jozufozu.flywheel.api.material; + +import com.jozufozu.flywheel.api.vertex.MutableVertexList; + +import net.minecraft.client.multiplayer.ClientLevel; + +public interface MaterialVertexTransformer { + void transform(MutableVertexList vertexList, ClientLevel level); +} 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 0d637caa4..ef8494a89 100644 --- a/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java +++ b/src/main/java/com/jozufozu/flywheel/api/model/Mesh.java @@ -5,13 +5,11 @@ import org.joml.Vector4fc; import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.api.vertex.VertexType; import com.jozufozu.flywheel.gl.buffer.ElementBuffer; -import com.jozufozu.flywheel.lib.util.QuadConverter; /** * A holder for arbitrary vertex data that can be written to memory or a vertex list. */ public interface Mesh { - VertexType getVertexType(); /** @@ -50,18 +48,9 @@ public interface Mesh { /** * Create an element buffer object that indexes the vertices of this mesh. - * - *
- * Very often models in minecraft are made up of sequential quads, which is a very predictable pattern. - * The default implementation accommodates this, however this can be overridden to change the behavior and - * support more complex models. - *
* @return an element buffer object indexing this model's vertices. */ - default ElementBuffer createEBO() { - return QuadConverter.getInstance() - .quads2Tris(getVertexCount() / 4); - } + ElementBuffer createEBO(); Vector4fc getBoundingSphere(); diff --git a/src/main/java/com/jozufozu/flywheel/api/instancer/Handle.java b/src/main/java/com/jozufozu/flywheel/api/struct/Handle.java similarity index 60% rename from src/main/java/com/jozufozu/flywheel/api/instancer/Handle.java rename to src/main/java/com/jozufozu/flywheel/api/struct/Handle.java index 208c73673..e688163ad 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instancer/Handle.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/Handle.java @@ -1,7 +1,6 @@ -package com.jozufozu.flywheel.api.instancer; +package com.jozufozu.flywheel.api.struct; public interface Handle { - void setChanged(); void setDeleted(); diff --git a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancePart.java b/src/main/java/com/jozufozu/flywheel/api/struct/InstancePart.java similarity index 52% rename from src/main/java/com/jozufozu/flywheel/api/instancer/InstancePart.java rename to src/main/java/com/jozufozu/flywheel/api/struct/InstancePart.java index ba6144f49..a59824584 100644 --- a/src/main/java/com/jozufozu/flywheel/api/instancer/InstancePart.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/InstancePart.java @@ -1,6 +1,4 @@ -package com.jozufozu.flywheel.api.instancer; - -import com.jozufozu.flywheel.api.struct.StructType; +package com.jozufozu.flywheel.api.struct; public interface InstancePart { StructType> type(); 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 c0a0bf002..1d734e5fc 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructType.java @@ -1,13 +1,9 @@ package com.jozufozu.flywheel.api.struct; -import com.jozufozu.flywheel.api.instancer.Handle; -import com.jozufozu.flywheel.api.instancer.InstancePart; 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; /** @@ -33,10 +29,5 @@ public interface StructType{ ResourceLocation instanceShader(); - VertexTransformer
getVertexTransformer(); - - interface VertexTransformer
{ - void transform(MutableVertexList vertexList, P struct, ClientLevel level); - } - + StructVertexTransformer
getVertexTransformer(); } diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StructVertexTransformer.java b/src/main/java/com/jozufozu/flywheel/api/struct/StructVertexTransformer.java new file mode 100644 index 000000000..b76e87bfa --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructVertexTransformer.java @@ -0,0 +1,9 @@ +package com.jozufozu.flywheel.api.struct; + +import com.jozufozu.flywheel.api.vertex.MutableVertexList; + +import net.minecraft.client.multiplayer.ClientLevel; + +public interface StructVertexTransformer
{ + void transform(MutableVertexList vertexList, P struct, ClientLevel level); +} diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java b/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java index 55468838f..5c8aed15d 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/StructWriter.java @@ -1,14 +1,11 @@ package com.jozufozu.flywheel.api.struct; -import com.jozufozu.flywheel.api.instancer.InstancePart; - /** - * StructWriters can quickly consume many instances of S and write them to some memory address. + * StructWriters can quickly consume many instances and write them to some memory address. */ public interface StructWriter
{ /** * Write the given struct to the given memory address. */ void write(final long ptr, final P struct); - } diff --git a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexList.java b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexList.java index 1b5070534..78f1a3ab9 100644 --- a/src/main/java/com/jozufozu/flywheel/api/vertex/VertexList.java +++ b/src/main/java/com/jozufozu/flywheel/api/vertex/VertexList.java @@ -7,7 +7,6 @@ package com.jozufozu.flywheel.api.vertex; * VertexList assumes nothing about the layout of the vertices. Implementations should feel free to return constants * for values that are unused in their layout. *
- * TODO: more flexible elements? */ public interface VertexList { float x(int index); diff --git a/src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java b/src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java deleted file mode 100644 index 15fc3e3e6..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/BackendUtil.java +++ /dev/null @@ -1,56 +0,0 @@ -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.LevelAccessor; - -public class BackendUtil { - 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 LevelAccessor 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 && flywheelLevel.supportsFlywheel()) { - return true; - } - - return level == Minecraft.getInstance().level; - } - - public static boolean isGameActive() { - return !(Minecraft.getInstance().level == null || Minecraft.getInstance().player == null); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/lib/backend/Backends.java b/src/main/java/com/jozufozu/flywheel/backend/Backends.java similarity index 80% rename from src/main/java/com/jozufozu/flywheel/lib/backend/Backends.java rename to src/main/java/com/jozufozu/flywheel/backend/Backends.java index 4535ab1fe..3cee46d2a 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/backend/Backends.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backends.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.lib.backend; +package com.jozufozu.flywheel.backend; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.backend.Backend; @@ -6,30 +6,20 @@ import com.jozufozu.flywheel.backend.engine.batching.BatchingEngine; import com.jozufozu.flywheel.backend.engine.indirect.IndirectEngine; import com.jozufozu.flywheel.backend.engine.instancing.InstancingEngine; import com.jozufozu.flywheel.gl.versioned.GlCompat; +import com.jozufozu.flywheel.lib.backend.SimpleBackend; import com.jozufozu.flywheel.lib.context.Contexts; -import com.jozufozu.flywheel.lib.pipeline.Pipelines; import com.jozufozu.flywheel.lib.util.ShadersModHandler; import net.minecraft.ChatFormatting; import net.minecraft.network.chat.TextComponent; public class Backends { - public static final Backend OFF = SimpleBackend.builder() - .engineMessage(new TextComponent("Disabled Flywheel").withStyle(ChatFormatting.RED)) - .engineFactory(level -> { - throw new IllegalStateException("Cannot create engine when backend is off."); - }) - .fallback(() -> Backends.OFF) - .supported(() -> true) - .register(Flywheel.rl("off")); - /** * Use a thread pool to buffer instances in parallel on the CPU. */ public static final Backend BATCHING = SimpleBackend.builder() .engineMessage(new TextComponent("Using Batching Engine").withStyle(ChatFormatting.GREEN)) .engineFactory(level -> new BatchingEngine()) - .fallback(() -> Backends.OFF) .supported(() -> !ShadersModHandler.isShaderPackInUse()) .register(Flywheel.rl("batching")); diff --git a/src/main/java/com/jozufozu/flywheel/backend/Loader.java b/src/main/java/com/jozufozu/flywheel/backend/Loader.java index 69f72dbbe..362c401ec 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Loader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Loader.java @@ -1,13 +1,11 @@ package com.jozufozu.flywheel.backend; -import com.jozufozu.flywheel.api.backend.BackendManager; import com.jozufozu.flywheel.backend.compile.FlwCompiler; import com.jozufozu.flywheel.glsl.ShaderSources; import com.jozufozu.flywheel.glsl.error.ErrorReporter; -import com.jozufozu.flywheel.impl.instancing.InstancedRenderDispatcher; +import com.jozufozu.flywheel.impl.BackendManagerImpl; import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.server.packs.resources.ReloadableResourceManager; import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManagerReloadListener; @@ -27,8 +25,6 @@ public class Loader implements ResourceManagerReloadListener { @Override public void onResourceManagerReload(ResourceManager manager) { - BackendManager.refresh(); - var errorReporter = new ErrorReporter(); ShaderSources sources = new ShaderSources(errorReporter, manager); @@ -38,14 +34,12 @@ public class Loader implements ResourceManagerReloadListener { FlwCompiler.INSTANCE = new FlwCompiler(sources); - ClientLevel level = Minecraft.getInstance().level; - if (level != null) { - InstancedRenderDispatcher.resetInstanceWorld(level); - } + // TODO: Move this to the impl package + BackendManagerImpl.refresh(Minecraft.getInstance().level); } public static void init() { - // Can be null when running datagenerators due to the unfortunate time we call this + // Can be null when running data generators due to the unfortunate time we call this Minecraft minecraft = Minecraft.getInstance(); if (minecraft == null) { return; diff --git a/src/main/java/com/jozufozu/flywheel/lib/pipeline/Pipelines.java b/src/main/java/com/jozufozu/flywheel/backend/Pipelines.java similarity index 93% rename from src/main/java/com/jozufozu/flywheel/lib/pipeline/Pipelines.java rename to src/main/java/com/jozufozu/flywheel/backend/Pipelines.java index f21a568cb..37b123de3 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/pipeline/Pipelines.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Pipelines.java @@ -1,9 +1,10 @@ -package com.jozufozu.flywheel.lib.pipeline; +package com.jozufozu.flywheel.backend; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.engine.indirect.IndirectComponent; import com.jozufozu.flywheel.backend.engine.instancing.InstancedArraysComponent; import com.jozufozu.flywheel.gl.GLSLVersion; +import com.jozufozu.flywheel.lib.pipeline.SimplePipeline; import net.minecraft.resources.ResourceLocation; @@ -22,7 +23,6 @@ public class Pipelines { .build(); public static void init() { - // noop } public static class Files { 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 910942501..2101a59c8 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwCompiler.java @@ -17,6 +17,7 @@ import com.jozufozu.flywheel.api.pipeline.Pipeline; 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.Pipelines; import com.jozufozu.flywheel.backend.engine.indirect.IndirectComponent; import com.jozufozu.flywheel.gl.GLSLVersion; import com.jozufozu.flywheel.gl.shader.GlProgram; @@ -27,7 +28,6 @@ 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.MaterialIndices; -import com.jozufozu.flywheel.lib.pipeline.Pipelines; import com.jozufozu.flywheel.util.StringUtil; public class FlwCompiler { 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 81622bc32..0326dbfb5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/ShaderCompiler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/ShaderCompiler.java @@ -13,7 +13,6 @@ 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 { private final Mapimplements Instancer
{ - public final StructType
type; // Lock for all instance data, only needs to be used in methods that may run on the TaskExecutor. @@ -42,30 +40,33 @@ public abstract class AbstractInstancer
implements Insta } } - /** - * Clear all instance data without freeing resources. - */ - public void clear() { - handles.forEach(HandleImpl::clear); - data.clear(); - handles.clear(); - changed.clear(); - deleted.clear(); - } - public int getInstanceCount() { return data.size(); } - public List
getRange(int start, int end) { - return data.subList(start, end); + public void notifyDirty(int index) { + if (index < 0 || index >= getInstanceCount()) { + return; + } + synchronized (lock) { + changed.set(index); + } } - public List
getAll() { - return data; + public void notifyRemoval(int index) { + if (index < 0 || index >= getInstanceCount()) { + return; + } + synchronized (lock) { + deleted.set(index); + } } protected void removeDeletedInstances() { + if (deleted.isEmpty()) { + return; + } + // Figure out which elements are to be removed. final int oldSize = this.data.size(); int removeCount = deleted.cardinality(); @@ -95,26 +96,19 @@ public abstract class AbstractInstancer
implements Insta .clear(); } + /** + * Clear all instance data without freeing resources. + */ + public void clear() { + handles.forEach(HandleImpl::clear); + data.clear(); + handles.clear(); + changed.clear(); + deleted.clear(); + } + @Override public String toString() { - return "Instancer[" + getInstanceCount() + ']'; - } - - public void notifyDirty(int index) { - if (index < 0 || index >= getInstanceCount()) { - return; - } - synchronized (lock) { - changed.set(index); - } - } - - public void notifyRemoval(int index) { - if (index < 0 || index >= getInstanceCount()) { - return; - } - synchronized (lock) { - deleted.set(index); - } + return "AbstractInstancer[" + getInstanceCount() + ']'; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/HandleImpl.java b/src/main/java/com/jozufozu/flywheel/backend/engine/HandleImpl.java index 915ef8249..28ebebcda 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/HandleImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/HandleImpl.java @@ -1,9 +1,8 @@ package com.jozufozu.flywheel.backend.engine; -import com.jozufozu.flywheel.api.instancer.Handle; +import com.jozufozu.flywheel.api.struct.Handle; public class HandleImpl implements Handle { - private final AbstractInstancer> instancer; private int index; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerKey.java b/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerKey.java index 39fa9840a..3cbf4458a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerKey.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerKey.java @@ -1,8 +1,8 @@ package com.jozufozu.flywheel.backend.engine; import com.jozufozu.flywheel.api.event.RenderStage; -import com.jozufozu.flywheel.api.instancer.InstancePart; import com.jozufozu.flywheel.api.model.Model; +import com.jozufozu.flywheel.api.struct.InstancePart; import com.jozufozu.flywheel.api.struct.StructType; public record InstancerKey
(StructType
type, Model model, RenderStage stage) {
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java
index 747bfcfd9..ee7497703 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/UniformBuffer.java
@@ -10,9 +10,9 @@ 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.math.MoreMath;
+import com.jozufozu.flywheel.lib.math.RenderMath;
import com.jozufozu.flywheel.lib.memory.MemoryBlock;
-import com.jozufozu.flywheel.util.FlwUtil;
-import com.jozufozu.flywheel.util.RenderMath;
public class UniformBuffer {
@@ -93,7 +93,7 @@ public class UniformBuffer {
var builder = ImmutableList. extends AbstractInstancer {
-
public CPUInstancer(StructType type) {
super(type);
}
- void update() {
- if (!deleted.isEmpty()) {
- removeDeletedInstances();
- }
+ public List getRange(int start, int end) {
+ return data.subList(start, end);
+ }
+
+ public List getAll() {
+ return data;
+ }
+
+ public void update() {
+ removeDeletedInstances();
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java
index aabe7de76..7d5318750 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java
@@ -32,7 +32,7 @@ public class DrawBuffer {
private boolean prepared;
private int vertexCount;
- DrawBuffer(RenderType renderType, VertexFormat format, int stride, VertexListProvider provider) {
+ public DrawBuffer(RenderType renderType, VertexFormat format, int stride, VertexListProvider provider) {
this.renderType = renderType;
this.format = format;
this.stride = stride;
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java
index c4c15093f..884435ab8 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBufferSet.java
@@ -3,8 +3,6 @@ package com.jozufozu.flywheel.backend.engine.batching;
import java.util.EnumMap;
import java.util.Map;
-import org.jetbrains.annotations.ApiStatus;
-
import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.vertex.VertexListProvider;
import com.jozufozu.flywheel.api.vertex.VertexListProviderRegistry;
@@ -20,7 +18,6 @@ public class DrawBufferSet {
private final Map {
return meshVertexCount * instancer.getInstanceCount();
}
- void setup() {
+ public void setup() {
instancer.update();
}
- void submitTasks(TaskExecutor executor, DrawBuffer buffer, int startVertex, PoseStack.Pose matrices, ClientLevel level) {
+ public void submitTasks(TaskExecutor executor, DrawBuffer buffer, int startVertex, PoseStack.Pose matrices, ClientLevel level) {
int instances = instancer.getInstanceCount();
while (instances > 0) {
@@ -57,21 +56,21 @@ public class TransformCall {
}
}
- private void transformRange(ReusableVertexList vertexList, int from, int to, PoseStack.Pose matrices, ClientLevel level) {
+ public void transformRange(ReusableVertexList vertexList, int from, int to, PoseStack.Pose matrices, ClientLevel level) {
transformList(vertexList, instancer.getRange(from, to), matrices, level);
}
- void transformAll(ReusableVertexList vertexList, PoseStack.Pose matrices, ClientLevel level) {
+ public void transformAll(ReusableVertexList vertexList, PoseStack.Pose matrices, ClientLevel level) {
transformList(vertexList, instancer.getAll(), matrices, level);
}
- private void transformList(ReusableVertexList vertexList, List parts, PoseStack.Pose matrices, ClientLevel level) {
+ public void transformList(ReusableVertexList vertexList, List parts, PoseStack.Pose matrices, ClientLevel level) {
long anchorPtr = vertexList.ptr();
int totalVertexCount = vertexList.vertexCount();
vertexList.vertexCount(meshVertexCount);
- StructType.VertexTransformer structVertexTransformer = instancer.type.getVertexTransformer();
+ StructVertexTransformer structVertexTransformer = instancer.type.getVertexTransformer();
for (P p : parts) {
mesh.copyTo(vertexList.ptr());
@@ -88,34 +87,12 @@ public class TransformCall {
}
private static void applyMatrices(MutableVertexList vertexList, PoseStack.Pose matrices) {
- Vector4f pos = new Vector4f();
- Vector3f normal = new Vector3f();
-
Matrix4f modelMatrix = matrices.pose();
Matrix3f normalMatrix = matrices.normal();
for (int i = 0; i < vertexList.vertexCount(); i++) {
- pos.set(
- vertexList.x(i),
- vertexList.y(i),
- vertexList.z(i),
- 1f
- );
- pos.transform(modelMatrix);
- vertexList.x(i, pos.x());
- vertexList.y(i, pos.y());
- vertexList.z(i, pos.z());
-
- normal.set(
- vertexList.normalX(i),
- vertexList.normalY(i),
- vertexList.normalZ(i)
- );
- normal.transform(normalMatrix);
- normal.normalize();
- vertexList.normalX(i, normal.x());
- vertexList.normalY(i, normal.y());
- vertexList.normalZ(i, normal.z());
+ VertexTransformations.transformPos(vertexList, i, modelMatrix);
+ VertexTransformations.transformNormal(vertexList, i, normalMatrix);
}
}
}
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 f82f04f41..ecdabd46e 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
@@ -8,6 +8,7 @@ import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.layout.LayoutItem;
import com.jozufozu.flywheel.api.pipeline.Pipeline;
import com.jozufozu.flywheel.api.struct.StructType;
+import com.jozufozu.flywheel.backend.Pipelines;
import com.jozufozu.flywheel.glsl.ShaderSources;
import com.jozufozu.flywheel.glsl.SourceComponent;
import com.jozufozu.flywheel.glsl.SourceFile;
@@ -15,7 +16,6 @@ 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.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 b5f9bed66..9636b512b 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
@@ -12,14 +12,14 @@ import static org.lwjgl.opengl.GL45.glVertexArrayElementBuffer;
import static org.lwjgl.opengl.GL45.glVertexArrayVertexBuffer;
import com.jozufozu.flywheel.api.event.RenderStage;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.vertex.VertexType;
+import com.jozufozu.flywheel.backend.Pipelines;
import com.jozufozu.flywheel.backend.compile.FlwCompiler;
import com.jozufozu.flywheel.backend.engine.UniformBuffer;
import com.jozufozu.flywheel.gl.shader.GlProgram;
import com.jozufozu.flywheel.lib.context.Contexts;
-import com.jozufozu.flywheel.lib.pipeline.Pipelines;
import com.jozufozu.flywheel.lib.util.QuadConverter;
public class IndirectCullingGroup {
@@ -109,7 +109,7 @@ public class IndirectCullingGroup {
}
buffers.updateCounts(instanceCountThisFrame, drawSet.size());
- meshPool.uploadAll();
+ meshPool.flush();
uploadInstanceData();
uploadIndirectCommands();
@@ -174,7 +174,7 @@ public class IndirectCullingGroup {
int baseInstance = 0;
for (var batch : drawSet.indirectDraws) {
batch.prepare(baseInstance);
- baseInstance += batch.instancer().instanceCount;
+ baseInstance += batch.instancer().getInstanceCount();
}
return baseInstance;
}
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 bbc352f4c..d9a18a032 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
@@ -3,32 +3,48 @@ package com.jozufozu.flywheel.backend.engine.indirect;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.api.event.RenderStage;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
import com.jozufozu.flywheel.api.material.Material;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.lib.material.MaterialIndices;
-public final class IndirectDraw {
+public class IndirectDraw {
private final IndirectInstancer instancer;
private final IndirectMeshPool.BufferedMesh mesh;
private final Material material;
private final RenderStage stage;
- int baseInstance = -1;
- final int vertexMaterialID;
- final int fragmentMaterialID;
+ private final int vertexMaterialID;
+ private final int fragmentMaterialID;
- boolean needsFullWrite = true;
+ private int baseInstance = -1;
+ private boolean needsFullWrite = true;
- IndirectDraw(IndirectInstancer instancer, Material material, RenderStage stage, IndirectMeshPool.BufferedMesh mesh) {
+ public IndirectDraw(IndirectInstancer instancer, Material material, IndirectMeshPool.BufferedMesh mesh, RenderStage stage) {
this.instancer = instancer;
this.material = material;
- this.stage = stage;
this.mesh = mesh;
+ this.stage = stage;
this.vertexMaterialID = MaterialIndices.getVertexShaderIndex(material);
this.fragmentMaterialID = MaterialIndices.getFragmentShaderIndex(material);
}
+ public IndirectInstancer instancer() {
+ return instancer;
+ }
+
+ public Material material() {
+ return material;
+ }
+
+ public IndirectMeshPool.BufferedMesh mesh() {
+ return mesh;
+ }
+
+ public RenderStage stage() {
+ return stage;
+ }
+
public void prepare(int baseInstance) {
instancer.update();
if (baseInstance == this.baseInstance) {
@@ -39,7 +55,7 @@ public final class IndirectDraw {
needsFullWrite = true;
}
- void writeObjects(long objectPtr, long batchIDPtr, int batchID) {
+ public void writeObjects(long objectPtr, long batchIDPtr, int batchID) {
if (needsFullWrite) {
instancer.writeFull(objectPtr, batchIDPtr, batchID);
} else {
@@ -48,7 +64,7 @@ public final class IndirectDraw {
}
public void writeIndirectCommand(long ptr) {
- var boundingSphere = mesh.mesh.getBoundingSphere();
+ var boundingSphere = mesh.getMesh().getBoundingSphere();
MemoryUtil.memPutInt(ptr, mesh.getIndexCount()); // count
MemoryUtil.memPutInt(ptr + 4, 0); // instanceCount - to be incremented by the compute shader
@@ -59,22 +75,5 @@ public final class IndirectDraw {
boundingSphere.getToAddress(ptr + 20); // boundingSphere
MemoryUtil.memPutInt(ptr + 36, vertexMaterialID); // vertexMaterialID
MemoryUtil.memPutInt(ptr + 40, fragmentMaterialID); // fragmentMaterialID
-
- }
-
- public IndirectInstancer instancer() {
- return instancer;
- }
-
- public IndirectMeshPool.BufferedMesh mesh() {
- return mesh;
- }
-
- public Material material() {
- return material;
- }
-
- public RenderStage stage() {
- return stage;
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java
index 73eb23973..49ab8402c 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectDrawManager.java
@@ -6,16 +6,15 @@ import java.util.List;
import java.util.Map;
import com.jozufozu.flywheel.api.event.RenderStage;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
import com.jozufozu.flywheel.api.instancer.Instancer;
import com.jozufozu.flywheel.api.model.Model;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.engine.InstancerKey;
import com.jozufozu.flywheel.util.Pair;
public class IndirectDrawManager {
-
private final Map {
@@ -31,7 +30,7 @@ public class IndirectDrawSet {
}
public void add(IndirectInstancer instancer, Material material, RenderStage stage, IndirectMeshPool.BufferedMesh bufferedMesh) {
- indirectDraws.add(new IndirectDraw<>(instancer, material, stage, bufferedMesh));
+ indirectDraws.add(new IndirectDraw<>(instancer, material, bufferedMesh, stage));
determineMultiDraws();
}
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 be3e9bb30..5c53a2261 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
@@ -7,9 +7,9 @@ import org.lwjgl.opengl.GL32;
import com.jozufozu.flywheel.api.backend.Engine;
import com.jozufozu.flywheel.api.event.RenderContext;
import com.jozufozu.flywheel.api.event.RenderStage;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
import com.jozufozu.flywheel.api.instancer.Instancer;
import com.jozufozu.flywheel.api.model.Model;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.task.TaskExecutor;
import com.jozufozu.flywheel.gl.GlStateTracker;
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java
index 4d50bafc5..e5f6282fa 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectInstancer.java
@@ -2,52 +2,43 @@ package com.jozufozu.flywheel.backend.engine.indirect;
import org.lwjgl.system.MemoryUtil;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.backend.engine.AbstractInstancer;
public class IndirectInstancer extends AbstractInstancer {
-
- private final long objectStride;
- private final StructWriter writer;
- int instanceCount = 0;
+ private final long instanceStride;
public IndirectInstancer(StructType type) {
super(type);
- this.objectStride = type.getLayout()
+ this.instanceStride = type.getLayout()
.getStride();
- writer = type.getWriter();
}
- public boolean isEmpty() {
- return changed.isEmpty() && deleted.isEmpty() && instanceCount == 0;
- }
-
- void update() {
- if (!deleted.isEmpty()) {
- removeDeletedInstances();
- }
-
- instanceCount = data.size();
+ public void update() {
+ removeDeletedInstances();
}
public void writeSparse(long objectPtr, long batchIDPtr, int batchID) {
- final int size = data.size();
+ int count = data.size();
+ StructWriter writer = type.getWriter();
+ for (int i = changed.nextSetBit(0); i >= 0 && i < count; i = changed.nextSetBit(i + 1)) {
+ // write object
+ writer.write(objectPtr + instanceStride * i, data.get(i));
- for (int i = changed.nextSetBit(0); i >= 0 && i < size; i = changed.nextSetBit(i + 1)) {
- writer.write(objectPtr + i * objectStride, data.get(i));
-
- MemoryUtil.memPutInt(batchIDPtr + i * IndirectBuffers.INT_SIZE, batchID);
+ // write batchID
+ MemoryUtil.memPutInt(batchIDPtr + IndirectBuffers.INT_SIZE * i, batchID);
}
changed.clear();
}
public void writeFull(long objectPtr, long batchIDPtr, int batchID) {
+ StructWriter writer = type.getWriter();
for (var object : data) {
// write object
writer.write(objectPtr, object);
- objectPtr += objectStride;
+ objectPtr += instanceStride;
// write batchID
MemoryUtil.memPutInt(batchIDPtr, batchID);
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java
index f7da98d36..c966ab56b 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/IndirectMeshPool.java
@@ -39,6 +39,10 @@ public class IndirectMeshPool {
clientStorage = MemoryBlock.malloc(byteCapacity);
}
+ public VertexType getVertexType() {
+ return vertexType;
+ }
+
/**
* Allocate a model in the arena.
*
@@ -60,24 +64,26 @@ public class IndirectMeshPool {
return meshes.get(mesh);
}
- void uploadAll() {
- if (!dirty) {
- return;
+ public void flush() {
+ if (dirty) {
+ uploadAll();
+ dirty = false;
}
- dirty = false;
+ }
+ private void uploadAll() {
final long ptr = clientStorage.ptr();
int byteIndex = 0;
int baseVertex = 0;
- for (BufferedMesh model : meshList) {
- model.byteIndex = byteIndex;
- model.baseVertex = baseVertex;
+ for (BufferedMesh mesh : meshList) {
+ mesh.byteIndex = byteIndex;
+ mesh.baseVertex = baseVertex;
- model.buffer(ptr);
+ mesh.buffer(ptr);
- byteIndex += model.size();
- baseVertex += model.mesh.getVertexCount();
+ byteIndex += mesh.size();
+ baseVertex += mesh.mesh.getVertexCount();
}
nglNamedBufferSubData(vbo, 0, byteIndex, ptr);
@@ -90,26 +96,18 @@ public class IndirectMeshPool {
meshList.clear();
}
- public VertexType getVertexType() {
- return vertexType;
- }
-
public class BufferedMesh {
- public final Mesh mesh;
+ private final Mesh mesh;
private final int vertexCount;
+
private long byteIndex;
private int baseVertex;
private BufferedMesh(Mesh mesh) {
this.mesh = mesh;
-
vertexCount = mesh.getVertexCount();
}
- private void buffer(long ptr) {
- mesh.write(ptr + byteIndex);
- }
-
public Mesh getMesh() {
return mesh;
}
@@ -129,5 +127,9 @@ public class IndirectMeshPool {
public VertexType getVertexType() {
return vertexType;
}
+
+ private void buffer(long ptr) {
+ mesh.write(ptr + byteIndex);
+ }
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/util/Textures.java b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/Textures.java
similarity index 89%
rename from src/main/java/com/jozufozu/flywheel/util/Textures.java
rename to src/main/java/com/jozufozu/flywheel/backend/engine/indirect/Textures.java
index f6292e966..7cc18e82e 100644
--- a/src/main/java/com/jozufozu/flywheel/util/Textures.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/indirect/Textures.java
@@ -1,4 +1,4 @@
-package com.jozufozu.flywheel.util;
+package com.jozufozu.flywheel.backend.engine.indirect;
import org.lwjgl.opengl.GL32;
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/DrawCall.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/DrawCall.java
index 7c86e5f3e..9f152f392 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/DrawCall.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/DrawCall.java
@@ -1,86 +1,50 @@
package com.jozufozu.flywheel.backend.engine.instancing;
-import com.jozufozu.flywheel.api.material.Material;
-import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.gl.GlStateTracker;
import com.jozufozu.flywheel.gl.array.GlVertexArray;
public class DrawCall {
+ private final GPUInstancer> instancer;
+ private final InstancedMeshPool.BufferedMesh mesh;
- final GPUInstancer> instancer;
- final Material material;
private final int meshAttributes;
- InstancedMeshPool.BufferedMesh bufferedMesh;
- GlVertexArray vao;
+ private GlVertexArray vao;
- DrawCall(GPUInstancer> instancer, Material material, InstancedMeshPool.BufferedMesh mesh) {
+ public DrawCall(GPUInstancer> instancer, InstancedMeshPool.BufferedMesh mesh) {
this.instancer = instancer;
- this.material = material;
- this.vao = new GlVertexArray();
- this.bufferedMesh = mesh;
- this.meshAttributes = this.bufferedMesh.getAttributeCount();
- this.vao.enableArrays(this.meshAttributes + instancer.instanceFormat.getAttributeCount());
+ this.mesh = mesh;
+
+ meshAttributes = this.mesh.getAttributeCount();
+ vao = new GlVertexArray();
+ vao.enableArrays(meshAttributes + this.instancer.getAttributeCount());
}
- public Material getMaterial() {
- return material;
- }
-
- public VertexType getVertexType() {
- return bufferedMesh.getVertexType();
+ public boolean isInvalid() {
+ return instancer.isInvalid() || vao == null;
}
public void render() {
- if (invalid()) {
+ if (isInvalid()) {
return;
}
try (var ignored = GlStateTracker.getRestoreState()) {
+ instancer.update();
- this.instancer.update();
+ instancer.bindToVAO(vao, meshAttributes);
- bindInstancerToVAO();
-
- if (this.instancer.glInstanceCount > 0) {
- bufferedMesh.drawInstances(vao, this.instancer.glInstanceCount);
+ if (instancer.getInstanceCount() > 0) {
+ mesh.drawInstances(vao, instancer.getInstanceCount());
}
}
}
- public boolean shouldRemove() {
- return invalid();
- }
-
- /**
- * Only {@code true} if the InstancedModel has been destroyed.
- */
- private boolean invalid() {
- return this.instancer.vbo == null || bufferedMesh == null || vao == null;
- }
-
- private void bindInstancerToVAO() {
- if (!this.instancer.boundTo.add(vao)) {
- return;
- }
-
- var instanceFormat = this.instancer.instanceFormat;
-
- vao.bindAttributes(this.instancer.vbo, this.meshAttributes, instanceFormat, 0L);
-
- for (int i = 0; i < instanceFormat.getAttributeCount(); i++) {
- vao.setAttributeDivisor(this.meshAttributes + i, 1);
- }
- }
-
public void delete() {
- if (invalid()) {
+ if (vao == null) {
return;
}
vao.delete();
- bufferedMesh.delete();
-
vao = null;
- bufferedMesh = null;
}
}
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/GPUInstancer.java
index 55b48a75c..dbd5040c1 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/GPUInstancer.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/GPUInstancer.java
@@ -4,8 +4,8 @@ import java.util.HashSet;
import java.util.Set;
import com.jozufozu.flywheel.Flywheel;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
import com.jozufozu.flywheel.api.layout.BufferLayout;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.struct.StructWriter;
import com.jozufozu.flywheel.backend.engine.AbstractInstancer;
@@ -16,17 +16,24 @@ import com.jozufozu.flywheel.gl.buffer.GlBufferUsage;
import com.jozufozu.flywheel.gl.buffer.MappedBuffer;
public class GPUInstancer extends AbstractInstancer {
+ private final BufferLayout instanceFormat;
+ private final int instanceStride;
- final BufferLayout instanceFormat;
- final Set type) {
super(type);
- this.instanceFormat = type.getLayout();
+ instanceFormat = type.getLayout();
+ instanceStride = instanceFormat.getStride();
+ }
+
+ public int getAttributeCount() {
+ return instanceFormat.getAttributeCount();
+ }
+
+ public boolean isInvalid() {
+ return vbo == null;
}
public void init() {
@@ -35,63 +42,59 @@ public class GPUInstancer extends AbstractInstancer {
}
vbo = new GlBuffer(GlBufferType.ARRAY_BUFFER, GlBufferUsage.DYNAMIC_DRAW);
- vbo.setGrowthMargin(instanceFormat.getStride() * 16);
+ vbo.setGrowthMargin(instanceStride * 16);
}
- public boolean isEmpty() {
- return deleted.isEmpty() && changed.isEmpty() && glInstanceCount == 0;
+ public void update() {
+ removeDeletedInstances();
+ ensureBufferCapacity();
+ updateBuffer();
}
- void update() {
- if (!deleted.isEmpty()) {
- removeDeletedInstances();
- }
-
- if (checkAndGrowBuffer()) {
- // The instance vbo has moved, so we need to re-bind attributes
+ private void ensureBufferCapacity() {
+ int count = data.size();
+ int byteSize = instanceStride * count;
+ if (vbo.ensureCapacity(byteSize)) {
+ // The vbo has moved, so we need to re-bind attributes
boundTo.clear();
}
-
- if (!changed.isEmpty()) {
- clearAndUpdateBuffer();
- }
-
- glInstanceCount = data.size();
}
- private void clearAndUpdateBuffer() {
- final int size = data.size();
- final long clearStart = (long) size * instanceFormat.getStride();
- final long clearLength = vbo.getSize() - clearStart;
+ private void updateBuffer() {
+ if (changed.isEmpty()) {
+ return;
+ }
+
+ int count = data.size();
+ long clearStart = instanceStride * (long) count;
+ long clearLength = vbo.getSize() - clearStart;
try (MappedBuffer buf = vbo.map()) {
buf.clear(clearStart, clearLength);
- if (size > 0) {
- final long ptr = buf.getPtr();
- final long stride = type.getLayout()
- .getStride();
- final StructWriter writer = type.getWriter();
+ long ptr = buf.getPtr();
+ StructWriter writer = type.getWriter();
- for (int i = changed.nextSetBit(0); i >= 0 && i < size; i = changed.nextSetBit(i + 1)) {
- writer.write(ptr + i * stride, data.get(i));
- }
- changed.clear();
+ for (int i = changed.nextSetBit(0); i >= 0 && i < count; i = changed.nextSetBit(i + 1)) {
+ writer.write(ptr + instanceStride * i, data.get(i));
}
+
+ changed.clear();
} catch (Exception e) {
Flywheel.LOGGER.error("Error updating GPUInstancer:", e);
}
}
- /**
- * @return {@code true} if the buffer moved.
- */
- private boolean checkAndGrowBuffer() {
- int size = this.data.size();
- int stride = instanceFormat.getStride();
- int requiredSize = size * stride;
+ public void bindToVAO(GlVertexArray vao, int attributeOffset) {
+ if (!boundTo.add(vao)) {
+ return;
+ }
- return vbo.ensureCapacity(requiredSize);
+ vao.bindAttributes(vbo, attributeOffset, instanceFormat, 0L);
+
+ for (int i = 0; i < instanceFormat.getAttributeCount(); i++) {
+ vao.setAttributeDivisor(attributeOffset + i, 1);
+ }
}
public void delete() {
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 e9f9320c2..603b7bce8 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
@@ -39,9 +39,12 @@ public class InstancedMeshPool {
public InstancedMeshPool(VertexType vertexType) {
this.vertexType = vertexType;
int stride = vertexType.getLayout().getStride();
- this.vbo = new GlBuffer(GlBufferType.ARRAY_BUFFER);
+ vbo = new GlBuffer(GlBufferType.ARRAY_BUFFER);
+ vbo.setGrowthMargin(stride * 32);
+ }
- this.vbo.setGrowthMargin(stride * 32);
+ public VertexType getVertexType() {
+ return vertexType;
}
/**
@@ -161,10 +164,6 @@ public class InstancedMeshPool {
pendingUpload.clear();
}
- public VertexType getVertexType() {
- return vertexType;
- }
-
@Override
public String toString() {
return "InstancedMeshPool{" + "vertexType=" + vertexType + ", byteSize=" + byteSize + ", meshCount=" + meshes.size() + '}';
@@ -210,10 +209,6 @@ public class InstancedMeshPool {
boundTo.clear();
}
- public void drawCall(GlVertexArray vao) {
- drawInstances(vao, 1);
- }
-
public void drawInstances(GlVertexArray vao, int instanceCount) {
if (isEmpty()) {
return;
diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingDrawManager.java
index 10c3fd563..d6165ddb8 100644
--- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingDrawManager.java
+++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancingDrawManager.java
@@ -14,16 +14,15 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import com.jozufozu.flywheel.api.event.RenderStage;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
import com.jozufozu.flywheel.api.instancer.Instancer;
import com.jozufozu.flywheel.api.model.Mesh;
import com.jozufozu.flywheel.api.model.Model;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.api.struct.StructType;
import com.jozufozu.flywheel.api.vertex.VertexType;
import com.jozufozu.flywheel.backend.engine.InstancerKey;
public class InstancingDrawManager {
-
private final Map
+ * Uses a different format that is friendly towards an optimized instruction-parallel
+ * implementation of sphere-frustum intersection.
+ * The format is as follows:
+ * {@code vec4(nxX, pxX, nyX, pyX)}
+ * Writes 96 bytes to the buffer.
+ *
+ * @param ptr The buffer to write the planes to.
+ * @param m The projection matrix to compute the frustum planes for.
+ */
+ public static void writePackedFrustumPlanes(long ptr, Matrix4f m) {
+ float nxX, nxY, nxZ, nxW;
+ float pxX, pxY, pxZ, pxW;
+ float nyX, nyY, nyZ, nyW;
+ float pyX, pyY, pyZ, pyW;
+ float nzX, nzY, nzZ, nzW;
+ float pzX, pzY, pzZ, pzW;
+
+ float invl;
+ nxX = m.m03() + m.m00();
+ nxY = m.m13() + m.m10();
+ nxZ = m.m23() + m.m20();
+ nxW = m.m33() + m.m30();
+ invl = Math.invsqrt(nxX * nxX + nxY * nxY + nxZ * nxZ);
+ nxX *= invl;
+ nxY *= invl;
+ nxZ *= invl;
+ nxW *= invl;
+
+ pxX = m.m03() - m.m00();
+ pxY = m.m13() - m.m10();
+ pxZ = m.m23() - m.m20();
+ pxW = m.m33() - m.m30();
+ invl = Math.invsqrt(pxX * pxX + pxY * pxY + pxZ * pxZ);
+ pxX *= invl;
+ pxY *= invl;
+ pxZ *= invl;
+ pxW *= invl;
+
+ nyX = m.m03() + m.m01();
+ nyY = m.m13() + m.m11();
+ nyZ = m.m23() + m.m21();
+ nyW = m.m33() + m.m31();
+ invl = Math.invsqrt(nyX * nyX + nyY * nyY + nyZ * nyZ);
+ nyX *= invl;
+ nyY *= invl;
+ nyZ *= invl;
+ nyW *= invl;
+
+ pyX = m.m03() - m.m01();
+ pyY = m.m13() - m.m11();
+ pyZ = m.m23() - m.m21();
+ pyW = m.m33() - m.m31();
+ invl = Math.invsqrt(pyX * pyX + pyY * pyY + pyZ * pyZ);
+ pyX *= invl;
+ pyY *= invl;
+ pyZ *= invl;
+ pyW *= invl;
+
+ nzX = m.m03() + m.m02();
+ nzY = m.m13() + m.m12();
+ nzZ = m.m23() + m.m22();
+ nzW = m.m33() + m.m32();
+ invl = Math.invsqrt(nzX * nzX + nzY * nzY + nzZ * nzZ);
+ nzX *= invl;
+ nzY *= invl;
+ nzZ *= invl;
+ nzW *= invl;
+
+ pzX = m.m03() - m.m02();
+ pzY = m.m13() - m.m12();
+ pzZ = m.m23() - m.m22();
+ pzW = m.m33() - m.m32();
+ invl = Math.invsqrt(pzX * pzX + pzY * pzY + pzZ * pzZ);
+ pzX *= invl;
+ pzY *= invl;
+ pzZ *= invl;
+ pzW *= invl;
+
+ MemoryUtil.memPutFloat(ptr, nxX);
+ MemoryUtil.memPutFloat(ptr + 4, pxX);
+ MemoryUtil.memPutFloat(ptr + 8, nyX);
+ MemoryUtil.memPutFloat(ptr + 12, pyX);
+ MemoryUtil.memPutFloat(ptr + 16, nxY);
+ MemoryUtil.memPutFloat(ptr + 20, pxY);
+ MemoryUtil.memPutFloat(ptr + 24, nyY);
+ MemoryUtil.memPutFloat(ptr + 28, pyY);
+ MemoryUtil.memPutFloat(ptr + 32, nxZ);
+ MemoryUtil.memPutFloat(ptr + 36, pxZ);
+ MemoryUtil.memPutFloat(ptr + 40, nyZ);
+ MemoryUtil.memPutFloat(ptr + 44, pyZ);
+ MemoryUtil.memPutFloat(ptr + 48, nxW);
+ MemoryUtil.memPutFloat(ptr + 52, pxW);
+ MemoryUtil.memPutFloat(ptr + 56, nyW);
+ MemoryUtil.memPutFloat(ptr + 60, pyW);
+ MemoryUtil.memPutFloat(ptr + 64, nzX);
+ MemoryUtil.memPutFloat(ptr + 68, pzX);
+ MemoryUtil.memPutFloat(ptr + 72, nzY);
+ MemoryUtil.memPutFloat(ptr + 76, pzY);
+ MemoryUtil.memPutFloat(ptr + 80, nzZ);
+ MemoryUtil.memPutFloat(ptr + 84, pzZ);
+ MemoryUtil.memPutFloat(ptr + 88, nzW);
+ MemoryUtil.memPutFloat(ptr + 92, pzW);
+ }
+}
diff --git a/src/main/java/com/jozufozu/flywheel/util/RenderMath.java b/src/main/java/com/jozufozu/flywheel/lib/math/RenderMath.java
similarity index 95%
rename from src/main/java/com/jozufozu/flywheel/util/RenderMath.java
rename to src/main/java/com/jozufozu/flywheel/lib/math/RenderMath.java
index 213cc2819..d7d015161 100644
--- a/src/main/java/com/jozufozu/flywheel/util/RenderMath.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/math/RenderMath.java
@@ -1,9 +1,8 @@
-package com.jozufozu.flywheel.util;
+package com.jozufozu.flywheel.lib.math;
import net.minecraftforge.client.model.pipeline.LightUtil;
-public class RenderMath {
-
+public final class RenderMath {
/**
* Convert a signed byte into a signed, normalized float.
*/
diff --git a/src/main/java/com/jozufozu/flywheel/lib/memory/FlwMemoryTracker.java b/src/main/java/com/jozufozu/flywheel/lib/memory/FlwMemoryTracker.java
index c66e852a9..6aec1b3e3 100644
--- a/src/main/java/com/jozufozu/flywheel/lib/memory/FlwMemoryTracker.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/memory/FlwMemoryTracker.java
@@ -7,7 +7,7 @@ import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.util.StringUtil;
-public class FlwMemoryTracker {
+public final class FlwMemoryTracker {
public static final boolean DEBUG_MEMORY_SAFETY = System.getProperty("flw.debugMemorySafety") != null;
static final Cleaner CLEANER = Cleaner.create();
diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java
index 654d5ef9f..aa930bbda 100644
--- a/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java
+++ b/src/main/java/com/jozufozu/flywheel/lib/model/ModelUtil.java
@@ -6,21 +6,21 @@ import java.nio.ByteBuffer;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector4f;
import org.lwjgl.system.MemoryUtil;
+import org.slf4j.Logger;
import com.dreizak.miniball.highdim.Miniball;
import com.dreizak.miniball.model.PointSet;
-import com.jozufozu.flywheel.Flywheel;
import com.jozufozu.flywheel.api.material.Material;
import com.jozufozu.flywheel.api.vertex.ReusableVertexList;
import com.jozufozu.flywheel.api.vertex.VertexList;
import com.jozufozu.flywheel.api.vertex.VertexListProviderRegistry;
import com.jozufozu.flywheel.api.vertex.VertexType;
-import com.jozufozu.flywheel.lib.format.Formats;
import com.jozufozu.flywheel.lib.material.Materials;
import com.jozufozu.flywheel.lib.memory.MemoryBlock;
import com.mojang.blaze3d.vertex.BufferBuilder.DrawState;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.datafixers.util.Pair;
+import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
@@ -28,7 +28,9 @@ import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.block.ModelBlockRenderer;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
-public class ModelUtil {
+public final class ModelUtil {
+ private static final Logger LOGGER = LogUtils.getLogger();
+
/**
* An alternative BlockRenderDispatcher that circumvents the Forge rendering pipeline to ensure consistency.
* Meant to be used for virtual rendering.
@@ -45,25 +47,24 @@ public class ModelUtil {
}
ObfuscationReflectionHelper.setPrivateValue(BlockRenderDispatcher.class, dispatcher, new ModelBlockRenderer(Minecraft.getInstance().getBlockColors()), "f_110900_");
} catch (Exception e) {
- Flywheel.LOGGER.error("Failed to initialize vanilla BlockRenderDispatcher!", e);
+ LOGGER.error("Failed to initialize vanilla BlockRenderDispatcher!", e);
return defaultDispatcher;
}
return dispatcher;
}
- public static Pair
+ * Very often models in minecraft are made up of sequential quads, which is a very predictable pattern.
+ * The default implementation accommodates this, however this can be overridden to change the behavior and
+ * support more complex models.
+ *
- * Uses a different format that is friendly towards an optimized instruction-parallel
- * implementation of sphere-frustum intersection.
- * The format is as follows:
- * {@code vec4(nxX, pxX, nyX, pyX)}
- * Writes 96 bytes to the buffer.
- *
- * @param ptr The buffer to write the planes to.
- * @param m The projection matrix to compute the frustum planes for.
- */
- public static void writePackedFrustumPlanes(long ptr, Matrix4f m) {
- float nxX, nxY, nxZ, nxW;
- float pxX, pxY, pxZ, pxW;
- float nyX, nyY, nyZ, nyW;
- float pyX, pyY, pyZ, pyW;
- float nzX, nzY, nzZ, nzW;
- float pzX, pzY, pzZ, pzW;
-
- float invl;
- nxX = m.m03() + m.m00();
- nxY = m.m13() + m.m10();
- nxZ = m.m23() + m.m20();
- nxW = m.m33() + m.m30();
- invl = Math.invsqrt(nxX * nxX + nxY * nxY + nxZ * nxZ);
- nxX *= invl;
- nxY *= invl;
- nxZ *= invl;
- nxW *= invl;
-
- pxX = m.m03() - m.m00();
- pxY = m.m13() - m.m10();
- pxZ = m.m23() - m.m20();
- pxW = m.m33() - m.m30();
- invl = Math.invsqrt(pxX * pxX + pxY * pxY + pxZ * pxZ);
- pxX *= invl;
- pxY *= invl;
- pxZ *= invl;
- pxW *= invl;
-
- nyX = m.m03() + m.m01();
- nyY = m.m13() + m.m11();
- nyZ = m.m23() + m.m21();
- nyW = m.m33() + m.m31();
- invl = Math.invsqrt(nyX * nyX + nyY * nyY + nyZ * nyZ);
- nyX *= invl;
- nyY *= invl;
- nyZ *= invl;
- nyW *= invl;
-
- pyX = m.m03() - m.m01();
- pyY = m.m13() - m.m11();
- pyZ = m.m23() - m.m21();
- pyW = m.m33() - m.m31();
- invl = Math.invsqrt(pyX * pyX + pyY * pyY + pyZ * pyZ);
- pyX *= invl;
- pyY *= invl;
- pyZ *= invl;
- pyW *= invl;
-
- nzX = m.m03() + m.m02();
- nzY = m.m13() + m.m12();
- nzZ = m.m23() + m.m22();
- nzW = m.m33() + m.m32();
- invl = Math.invsqrt(nzX * nzX + nzY * nzY + nzZ * nzZ);
- nzX *= invl;
- nzY *= invl;
- nzZ *= invl;
- nzW *= invl;
-
- pzX = m.m03() - m.m02();
- pzY = m.m13() - m.m12();
- pzZ = m.m23() - m.m22();
- pzW = m.m33() - m.m32();
- invl = Math.invsqrt(pzX * pzX + pzY * pzY + pzZ * pzZ);
- pzX *= invl;
- pzY *= invl;
- pzZ *= invl;
- pzW *= invl;
-
- MemoryUtil.memPutFloat(ptr, nxX);
- MemoryUtil.memPutFloat(ptr + 4, pxX);
- MemoryUtil.memPutFloat(ptr + 8, nyX);
- MemoryUtil.memPutFloat(ptr + 12, pyX);
- MemoryUtil.memPutFloat(ptr + 16, nxY);
- MemoryUtil.memPutFloat(ptr + 20, pxY);
- MemoryUtil.memPutFloat(ptr + 24, nyY);
- MemoryUtil.memPutFloat(ptr + 28, pyY);
- MemoryUtil.memPutFloat(ptr + 32, nxZ);
- MemoryUtil.memPutFloat(ptr + 36, pxZ);
- MemoryUtil.memPutFloat(ptr + 40, nyZ);
- MemoryUtil.memPutFloat(ptr + 44, pyZ);
- MemoryUtil.memPutFloat(ptr + 48, nxW);
- MemoryUtil.memPutFloat(ptr + 52, pxW);
- MemoryUtil.memPutFloat(ptr + 56, nyW);
- MemoryUtil.memPutFloat(ptr + 60, pyW);
- MemoryUtil.memPutFloat(ptr + 64, nzX);
- MemoryUtil.memPutFloat(ptr + 68, pzX);
- MemoryUtil.memPutFloat(ptr + 72, nzY);
- MemoryUtil.memPutFloat(ptr + 76, pzY);
- MemoryUtil.memPutFloat(ptr + 80, nzZ);
- MemoryUtil.memPutFloat(ptr + 84, pzZ);
- MemoryUtil.memPutFloat(ptr + 88, nzW);
- MemoryUtil.memPutFloat(ptr + 92, pzW);
- }
}
diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java
index 971f24d1c..aa9526224 100644
--- a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java
+++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java
@@ -7,14 +7,14 @@ 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.controller.InstanceContext;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.lib.instance.AbstractBlockEntityInstance;
import com.jozufozu.flywheel.lib.material.Materials;
import com.jozufozu.flywheel.lib.model.SimpleLazyModel;
import com.jozufozu.flywheel.lib.modelpart.ModelPart;
import com.jozufozu.flywheel.lib.struct.OrientedPart;
import com.jozufozu.flywheel.lib.struct.StructTypes;
-import com.jozufozu.flywheel.util.AnimationTickHolder;
+import com.jozufozu.flywheel.lib.util.AnimationTickHolder;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java
index 4b95610ca..4e862a238 100644
--- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java
+++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java
@@ -7,7 +7,7 @@ import java.util.function.BiFunction;
import com.jozufozu.flywheel.api.event.RenderStage;
import com.jozufozu.flywheel.api.instance.DynamicInstance;
import com.jozufozu.flywheel.api.instance.controller.InstanceContext;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.lib.instance.AbstractBlockEntityInstance;
import com.jozufozu.flywheel.lib.material.Materials;
import com.jozufozu.flywheel.lib.model.SimpleLazyModel;
@@ -15,7 +15,7 @@ import com.jozufozu.flywheel.lib.modelpart.ModelPart;
import com.jozufozu.flywheel.lib.struct.OrientedPart;
import com.jozufozu.flywheel.lib.struct.StructTypes;
import com.jozufozu.flywheel.lib.struct.TransformedPart;
-import com.jozufozu.flywheel.util.AnimationTickHolder;
+import com.jozufozu.flywheel.lib.util.AnimationTickHolder;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java
index d4b3bd9da..1e1cc638f 100644
--- a/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java
+++ b/src/main/java/com/jozufozu/flywheel/vanilla/MinecartInstance.java
@@ -15,7 +15,7 @@ import com.jozufozu.flywheel.lib.modelpart.ModelPart;
import com.jozufozu.flywheel.lib.struct.StructTypes;
import com.jozufozu.flywheel.lib.struct.TransformedPart;
import com.jozufozu.flywheel.lib.transform.TransformStack;
-import com.jozufozu.flywheel.util.AnimationTickHolder;
+import com.jozufozu.flywheel.lib.util.AnimationTickHolder;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Vector3f;
diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java
index 3cee8063a..da5c1eb84 100644
--- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java
+++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxInstance.java
@@ -6,7 +6,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.instance.controller.InstanceContext;
-import com.jozufozu.flywheel.api.instancer.InstancePart;
+import com.jozufozu.flywheel.api.struct.InstancePart;
import com.jozufozu.flywheel.lib.instance.AbstractBlockEntityInstance;
import com.jozufozu.flywheel.lib.material.Materials;
import com.jozufozu.flywheel.lib.model.SimpleLazyModel;
@@ -14,7 +14,7 @@ import com.jozufozu.flywheel.lib.modelpart.ModelPart;
import com.jozufozu.flywheel.lib.struct.StructTypes;
import com.jozufozu.flywheel.lib.struct.TransformedPart;
import com.jozufozu.flywheel.lib.transform.TransformStack;
-import com.jozufozu.flywheel.util.AnimationTickHolder;
+import com.jozufozu.flywheel.lib.util.AnimationTickHolder;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java
index 5eef3079d..d372d4906 100644
--- a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java
+++ b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java
@@ -27,7 +27,6 @@ import net.minecraft.world.level.block.entity.BlockEntityType;
*
*/
public class VanillaInstances {
-
public static void init() {
configure(BlockEntityType.CHEST)
.alwaysSkipRender()
diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java b/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java
index fb6a8453e..c149585c4 100644
--- a/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java
+++ b/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java
@@ -22,7 +22,7 @@ import com.jozufozu.flywheel.lib.instance.AbstractInstance;
import com.jozufozu.flywheel.lib.model.Models;
import com.jozufozu.flywheel.lib.struct.StructTypes;
import com.jozufozu.flywheel.lib.struct.TransformedPart;
-import com.jozufozu.flywheel.util.AnimationTickHolder;
+import com.jozufozu.flywheel.lib.util.AnimationTickHolder;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
+ * {@code vec4(nxY, pxY, nyY, pyY)}
+ * {@code vec4(nxZ, pxZ, nyZ, pyZ)}
+ * {@code vec4(nxW, pxW, nyW, pyW)}
+ * {@code vec2(nzX, pzX)}
+ * {@code vec2(nzY, pzY)}
+ * {@code vec2(nzZ, pzZ)}
+ * {@code vec2(nzW, pzW)}
+ * > oneOrMore(Codec
- * {@code vec4(nxY, pxY, nyY, pyY)}
- * {@code vec4(nxZ, pxZ, nyZ, pyZ)}
- * {@code vec4(nxW, pxW, nyW, pyW)}
- * {@code vec2(nzX, pzX)}
- * {@code vec2(nzY, pzY)}
- * {@code vec2(nzZ, pzZ)}
- * {@code vec2(nzW, pzW)}
- *