From 2c3b4c54cabb4d13ecfda1aaced5de7e52bfdc44 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 26 Feb 2024 14:50:58 -0800 Subject: [PATCH] Stay inline - Inline all unnecessary context stuff - Move ContextShaders into backend.compile - Instancers are parameterized by a nullable VisualEmbedding - I don't like this, but the code currently compiles, so I want a checkpoint before I try my next idea --- .../java/com/jozufozu/flywheel/Flywheel.java | 2 - .../jozufozu/flywheel/api/backend/Engine.java | 4 +- .../api/visualization/EmbeddedLevel.java | 11 --- .../api/visualization/VisualEmbedding.java | 14 ++++ .../visualization/VisualizationContext.java | 10 +-- .../backend/compile/ContextShader.java | 6 ++ .../backend/compile/ContextShaders.java | 23 ++++++ .../flywheel/backend/compile/FlwPrograms.java | 3 +- .../backend/compile/IndirectPrograms.java | 1 - .../backend/compile/InstancingPrograms.java | 1 - .../backend/compile/PipelineProgramKey.java | 1 - .../flywheel/backend/context/Context.java | 18 ----- .../backend/context/ContextShader.java | 14 ---- .../backend/context/ContextShaders.java | 18 ----- .../flywheel/backend/context/Contexts.java | 9 --- .../backend/context/EmbeddedContext.java | 33 -------- .../backend/context/SimpleContext.java | 59 -------------- .../backend/context/SimpleContextShader.java | 16 ---- .../flywheel/backend/context/Texture.java | 8 -- .../backend/engine/AbstractEngine.java | 10 +-- .../backend/engine/AbstractInstancer.java | 13 ++-- .../flywheel/backend/engine/InstancerKey.java | 7 +- .../backend/engine/InstancerStorage.java | 8 +- .../engine/{textures => }/TextureBinder.java | 16 +++- .../engine/indirect/IndirectCullingGroup.java | 40 +++++++--- .../engine/indirect/IndirectDrawManager.java | 22 +++--- .../engine/indirect/IndirectInstancer.java | 5 +- .../instancing/InstancedDrawManager.java | 18 ++--- .../engine/instancing/InstancedInstancer.java | 4 +- .../engine/instancing/ShaderState.java | 6 +- .../engine/textures/IdentifiedTexture.java | 13 ---- .../engine/textures/TextureSource.java | 78 ------------------- .../flywheel/backend/gl/shader/GlProgram.java | 23 +++--- .../visualization/InstancerProviderImpl.java | 10 +-- .../VisualizationContextImpl.java | 4 +- 35 files changed, 159 insertions(+), 369 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java create mode 100644 src/main/java/com/jozufozu/flywheel/api/visualization/VisualEmbedding.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/compile/ContextShader.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/compile/ContextShaders.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/Context.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/Contexts.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/EmbeddedContext.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/context/Texture.java rename src/main/java/com/jozufozu/flywheel/backend/engine/{textures => }/TextureBinder.java (84%) delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/engine/textures/IdentifiedTexture.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index d50f2ebcb..0040cdc16 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -11,7 +11,6 @@ import com.jozufozu.flywheel.api.visualization.VisualizationManager; import com.jozufozu.flywheel.backend.Backends; import com.jozufozu.flywheel.backend.ShaderIndices; import com.jozufozu.flywheel.backend.compile.FlwPrograms; -import com.jozufozu.flywheel.backend.context.ContextShaders; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.config.BackendArgument; import com.jozufozu.flywheel.config.FlwCommands; @@ -128,7 +127,6 @@ public class Flywheel { CutoutShaders.init(); FogShaders.init(); StandardMaterialShaders.init(); - ContextShaders.init(); ShaderIndices.init(); 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 63a72723b..a4ddc163c 100644 --- a/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java +++ b/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java @@ -12,7 +12,7 @@ import com.jozufozu.flywheel.api.instance.InstancerProvider; import com.jozufozu.flywheel.api.model.Model; import com.jozufozu.flywheel.api.task.Plan; import com.jozufozu.flywheel.api.task.TaskExecutor; -import com.jozufozu.flywheel.api.visualization.EmbeddedLevel; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import net.minecraft.client.Camera; import net.minecraft.core.BlockPos; @@ -34,7 +34,7 @@ public interface Engine { */ Instancer instancer(InstanceType type, Model model, RenderStage stage); - Instancer instancer(EmbeddedLevel world, InstanceType type, Model model, RenderStage stage); + Instancer instancer(VisualEmbedding world, InstanceType type, Model model, RenderStage stage); /** * Create a plan that will be executed every frame. diff --git a/src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java b/src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java deleted file mode 100644 index b821c3447..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.jozufozu.flywheel.api.visualization; - -import com.mojang.blaze3d.vertex.PoseStack; - -import net.minecraft.world.phys.AABB; - -public interface EmbeddedLevel { - void transform(PoseStack stack); - - AABB boundingBox(); -} diff --git a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualEmbedding.java b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualEmbedding.java new file mode 100644 index 000000000..c308a8003 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualEmbedding.java @@ -0,0 +1,14 @@ +package com.jozufozu.flywheel.api.visualization; + +import org.joml.Matrix3fc; +import org.joml.Matrix4fc; + +import net.minecraft.world.phys.AABB; + +public interface VisualEmbedding { + Matrix4fc pose(); + + Matrix3fc normal(); + + AABB boundingBox(); +} diff --git a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java index 759841971..f8a74775c 100644 --- a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java +++ b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java @@ -3,7 +3,6 @@ package com.jozufozu.flywheel.api.visualization; import org.jetbrains.annotations.ApiStatus; import com.jozufozu.flywheel.api.instance.InstancerProvider; -import com.jozufozu.flywheel.backend.context.Context; import net.minecraft.core.Vec3i; @@ -24,13 +23,6 @@ public interface VisualizationContext { */ Vec3i renderOrigin(); - /** - * Create a new {@link VisualizationContext} with the given {@link Context} and render origin. - * - * @param context The new context. - * @param renderOrigin The new render origin. - * @return A new {@link VisualizationContext} for use with child visuals. - */ @ApiStatus.Experimental - VisualizationContext embed(EmbeddedLevel world); + VisualizationContext embed(VisualEmbedding world); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/ContextShader.java b/src/main/java/com/jozufozu/flywheel/backend/compile/ContextShader.java new file mode 100644 index 000000000..2cbf6a67f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/ContextShader.java @@ -0,0 +1,6 @@ +package com.jozufozu.flywheel.backend.compile; + +import net.minecraft.resources.ResourceLocation; + +public record ContextShader(ResourceLocation vertexShader, ResourceLocation fragmentShader) { +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/ContextShaders.java b/src/main/java/com/jozufozu/flywheel/backend/compile/ContextShaders.java new file mode 100644 index 000000000..63a1086ef --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/ContextShaders.java @@ -0,0 +1,23 @@ +package com.jozufozu.flywheel.backend.compile; + +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.api.internal.InternalFlywheelApi; +import com.jozufozu.flywheel.api.registry.Registry; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; + +public class ContextShaders { + public static final Registry REGISTRY = InternalFlywheelApi.INSTANCE.createRegistry(); + public static final ContextShader DEFAULT = REGISTRY.registerAndGet(new ContextShader(Flywheel.rl("internal/context/default.vert"), Flywheel.rl("internal/context/default.frag"))); + public static final ContextShader CRUMBLING = REGISTRY.registerAndGet(new ContextShader(Flywheel.rl("internal/context/crumbling.vert"), Flywheel.rl("internal/context/crumbling.frag"))); + public static final ContextShader EMBEDDED = REGISTRY.registerAndGet(new ContextShader(Flywheel.rl("internal/context/embedded.vert"), Flywheel.rl("internal/context/embedded.frag"))); + + public static ContextShader forEmbedding(@Nullable VisualEmbedding level) { + if (level == null) { + return DEFAULT; + } else { + return EMBEDDED; + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java index 528a7ffbe..9dbe4c5a3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java @@ -13,7 +13,6 @@ import com.jozufozu.flywheel.backend.ShaderIndices; import com.jozufozu.flywheel.backend.compile.component.UberShaderComponent; import com.jozufozu.flywheel.backend.compile.core.CompilerStats; import com.jozufozu.flywheel.backend.compile.core.SourceLoader; -import com.jozufozu.flywheel.backend.context.ContextShader; import com.jozufozu.flywheel.backend.glsl.ShaderSources; import com.jozufozu.flywheel.backend.glsl.SourceComponent; import com.jozufozu.flywheel.backend.glsl.generate.FnSignature; @@ -58,7 +57,7 @@ public final class FlwPrograms { private static ImmutableList createPipelineKeys() { ImmutableList.Builder builder = ImmutableList.builder(); - for (ContextShader contextShader : ContextShader.REGISTRY) { + for (ContextShader contextShader : ContextShaders.REGISTRY) { for (InstanceType instanceType : InstanceType.REGISTRY) { builder.add(new PipelineProgramKey(instanceType, contextShader)); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java b/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java index 5887a931a..7ebb62814 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java @@ -11,7 +11,6 @@ import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.backend.compile.component.IndirectComponent; import com.jozufozu.flywheel.backend.compile.core.CompilationHarness; import com.jozufozu.flywheel.backend.compile.core.Compile; -import com.jozufozu.flywheel.backend.context.ContextShader; import com.jozufozu.flywheel.backend.gl.GlCompat; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java b/src/main/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java index fd005d09e..ffbbe8f14 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java @@ -7,7 +7,6 @@ import org.jetbrains.annotations.Nullable; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.api.instance.InstanceType; -import com.jozufozu.flywheel.backend.context.ContextShader; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import com.jozufozu.flywheel.backend.glsl.ShaderSources; import com.jozufozu.flywheel.backend.glsl.SourceComponent; diff --git a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineProgramKey.java b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineProgramKey.java index fc0c27722..60819e06a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineProgramKey.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/PipelineProgramKey.java @@ -1,7 +1,6 @@ package com.jozufozu.flywheel.backend.compile; import com.jozufozu.flywheel.api.instance.InstanceType; -import com.jozufozu.flywheel.backend.context.ContextShader; /** * Represents the entire context of a program's usage. diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/Context.java b/src/main/java/com/jozufozu/flywheel/backend/context/Context.java deleted file mode 100644 index 2285e95d3..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/Context.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.backend.engine.textures.TextureSource; -import com.jozufozu.flywheel.backend.gl.shader.GlProgram; - -public interface Context { - ContextShader contextShader(); - - /** - * Prepare the shader for rendering with the given material and textures. - * - * @param material The material about to be rendered. - * @param shader The shader to prepare. - * @param textureSource Source of the textures to use. - */ - void prepare(Material material, GlProgram shader, TextureSource textureSource); -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java b/src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java deleted file mode 100644 index 916575933..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import com.jozufozu.flywheel.api.internal.InternalFlywheelApi; -import com.jozufozu.flywheel.api.registry.Registry; - -import net.minecraft.resources.ResourceLocation; - -public interface ContextShader { - static Registry REGISTRY = InternalFlywheelApi.INSTANCE.createRegistry(); - - ResourceLocation vertexShader(); - - ResourceLocation fragmentShader(); -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java b/src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java deleted file mode 100644 index bf4f71b64..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import org.jetbrains.annotations.ApiStatus; - -import com.jozufozu.flywheel.Flywheel; - -public class ContextShaders { - public static final SimpleContextShader DEFAULT = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("internal/context/default.vert"), Flywheel.rl("internal/context/default.frag"))); - public static final SimpleContextShader CRUMBLING = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("internal/context/crumbling.vert"), Flywheel.rl("internal/context/crumbling.frag"))); - public static final SimpleContextShader EMBEDDED = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("internal/context/embedded.vert"), Flywheel.rl("internal/context/embedded.frag"))); - - private ContextShaders() { - } - - @ApiStatus.Internal - public static void init() { - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/Contexts.java b/src/main/java/com/jozufozu/flywheel/backend/context/Contexts.java deleted file mode 100644 index 25ce84397..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/Contexts.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -public final class Contexts { - public static final Context DEFAULT = SimpleContext.builder(ContextShaders.DEFAULT) - .build(); - - private Contexts() { - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/EmbeddedContext.java b/src/main/java/com/jozufozu/flywheel/backend/context/EmbeddedContext.java deleted file mode 100644 index 818669030..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/EmbeddedContext.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.api.visualization.EmbeddedLevel; -import com.jozufozu.flywheel.backend.engine.textures.TextureSource; -import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.mojang.blaze3d.vertex.PoseStack; - -public class EmbeddedContext implements Context { - private final EmbeddedLevel world; - - public EmbeddedContext(EmbeddedLevel world) { - this.world = world; - } - - @Override - public ContextShader contextShader() { - return ContextShaders.EMBEDDED; - } - - @Override - public void prepare(Material material, GlProgram shader, TextureSource textureSource) { - var stack = new PoseStack(); - world.transform(stack); - - // shader.setVec3("create_oneOverLightBoxSize"); - // shader.setVec3("create_lightVolumeMin"); - shader.setMat4("_flw_model", stack.last() - .pose()); - shader.setMat3("_flw_normal", stack.last() - .normal()); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java b/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java deleted file mode 100644 index 51331a1dc..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.backend.engine.textures.TextureSource; -import com.jozufozu.flywheel.backend.gl.shader.GlProgram; - -public class SimpleContext implements Context { - private final ContextShader contextShader; - private final Preparation preparation; - - public SimpleContext(ContextShader contextShader, Preparation preparation) { - this.contextShader = contextShader; - this.preparation = preparation; - } - - public static Builder builder(ContextShader contextShader) { - return new Builder(contextShader); - } - - @Override - public ContextShader contextShader() { - return contextShader; - } - - @Override - public void prepare(Material material, GlProgram shader, TextureSource textureSource) { - preparation.prepare(material, shader, textureSource); - } - - @FunctionalInterface - public interface Preparation { - void prepare(Material material, GlProgram shader, TextureSource textureSource); - } - - public static class Builder { - private final ContextShader contextShader; - @Nullable - private Preparation preparation; - - public Builder(ContextShader contextShader) { - this.contextShader = contextShader; - } - - public Builder preparation(Preparation preparation) { - this.preparation = preparation; - return this; - } - - public SimpleContext build() { - if (preparation == null) { - preparation = (material, shader, textureSource) -> { - }; - } - return new SimpleContext(contextShader, preparation); - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java b/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java deleted file mode 100644 index 05b1723e1..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import net.minecraft.resources.ResourceLocation; - -public record SimpleContextShader(ResourceLocation vertexShader, - ResourceLocation fragmentShader) implements ContextShader { - @Override - public ResourceLocation vertexShader() { - return vertexShader; - } - - @Override - public ResourceLocation fragmentShader() { - return fragmentShader; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/Texture.java b/src/main/java/com/jozufozu/flywheel/backend/context/Texture.java deleted file mode 100644 index b5de27323..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/context/Texture.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.jozufozu.flywheel.backend.context; - -import com.jozufozu.flywheel.api.BackendImplemented; - -@BackendImplemented -public interface Texture { - void filter(boolean blur, boolean mipmap); -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractEngine.java b/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractEngine.java index b425b613c..e8e31a641 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractEngine.java @@ -6,9 +6,7 @@ import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.Instancer; import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.api.visualization.EmbeddedLevel; -import com.jozufozu.flywheel.backend.context.Contexts; -import com.jozufozu.flywheel.backend.context.EmbeddedContext; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import net.minecraft.client.Camera; import net.minecraft.core.BlockPos; @@ -25,12 +23,12 @@ public abstract class AbstractEngine implements Engine { @Override public Instancer instancer(InstanceType type, Model model, RenderStage stage) { - return getStorage().getInstancer(type, Contexts.DEFAULT, model, stage); + return getStorage().getInstancer(null, type, model, stage); } @Override - public Instancer instancer(EmbeddedLevel world, InstanceType type, Model model, RenderStage stage) { - return getStorage().getInstancer(type, new EmbeddedContext(world), model, stage); + public Instancer instancer(VisualEmbedding world, InstanceType type, Model model, RenderStage stage) { + return getStorage().getInstancer(world, type, model, stage); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractInstancer.java index 924884bce..6e81b6e26 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractInstancer.java @@ -2,15 +2,18 @@ package com.jozufozu.flywheel.backend.engine; import java.util.ArrayList; +import org.jetbrains.annotations.Nullable; + import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.Instancer; -import com.jozufozu.flywheel.backend.context.Context; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import com.jozufozu.flywheel.lib.util.AtomicBitset; public abstract class AbstractInstancer implements Instancer { public final InstanceType type; - public final Context context; + @Nullable + public final VisualEmbedding embedding; // Lock for all instances, only needs to be used in methods that may run on the TaskExecutor. protected final Object lock = new Object(); @@ -20,9 +23,9 @@ public abstract class AbstractInstancer implements Instancer protected final AtomicBitset changed = new AtomicBitset(); protected final AtomicBitset deleted = new AtomicBitset(); - protected AbstractInstancer(InstanceType type, Context context) { + protected AbstractInstancer(InstanceType type, @Nullable VisualEmbedding embedding) { this.type = type; - this.context = context; + this.embedding = embedding; } @Override @@ -38,7 +41,7 @@ public abstract class AbstractInstancer implements Instancer } @Override - public void stealInstance(I instance) { + public void stealInstance(@Nullable I instance) { if (instance == null) { return; } 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 87a91c598..3bb410c63 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerKey.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerKey.java @@ -1,10 +1,13 @@ package com.jozufozu.flywheel.backend.engine; +import org.jetbrains.annotations.Nullable; + import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.backend.context.Context; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; -public record InstancerKey(InstanceType type, Context context, Model model, RenderStage stage) { +public record InstancerKey(@Nullable VisualEmbedding embedding, InstanceType type, Model model, + RenderStage stage) { } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerStorage.java b/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerStorage.java index f56031ecc..f969df8e2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerStorage.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerStorage.java @@ -5,13 +5,15 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.jetbrains.annotations.Nullable; + import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.Instancer; import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.backend.context.Context; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; public abstract class InstancerStorage> { /** @@ -28,8 +30,8 @@ public abstract class InstancerStorage> { protected final List> initializationQueue = new ArrayList<>(); @SuppressWarnings("unchecked") - public Instancer getInstancer(InstanceType type, Context context, Model model, RenderStage stage) { - return (Instancer) instancers.computeIfAbsent(new InstancerKey<>(type, context, model, stage), this::createAndDeferInit); + public Instancer getInstancer(@Nullable VisualEmbedding level, InstanceType type, Model model, RenderStage stage) { + return (Instancer) instancers.computeIfAbsent(new InstancerKey<>(level, type, model, stage), this::createAndDeferInit); } public void delete() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java b/src/main/java/com/jozufozu/flywheel/backend/engine/TextureBinder.java similarity index 84% rename from src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java rename to src/main/java/com/jozufozu/flywheel/backend/engine/TextureBinder.java index f16fe14f5..96cc658d3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureBinder.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/TextureBinder.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.engine.textures; +package com.jozufozu.flywheel.backend.engine; import static org.lwjgl.opengl.GL13.GL_TEXTURE0; @@ -8,6 +8,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import it.unimi.dsi.fastutil.ints.Int2IntArrayMap; import it.unimi.dsi.fastutil.ints.Int2IntMap; import net.minecraft.client.Minecraft; +import net.minecraft.resources.ResourceLocation; public class TextureBinder { // TODO: some kind of cache eviction when the program changes @@ -69,4 +70,17 @@ public class TextureBinder { gameRenderer.lightTexture() .turnOffLightLayer(); } + + /** + * Get a built-in texture by its resource location. + * + * @param texture The texture's resource location. + * @return The texture. + */ + public static int byName(ResourceLocation texture) { + return Minecraft.getInstance() + .getTextureManager() + .getTexture(texture) + .getId(); + } } 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 77ccdcf0c..6b375d053 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,18 +16,20 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; +import org.jetbrains.annotations.Nullable; + import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.api.model.Model; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; +import com.jozufozu.flywheel.backend.compile.ContextShader; +import com.jozufozu.flywheel.backend.compile.ContextShaders; import com.jozufozu.flywheel.backend.compile.IndirectPrograms; -import com.jozufozu.flywheel.backend.context.Context; -import com.jozufozu.flywheel.backend.context.SimpleContextShader; import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.MeshPool; -import com.jozufozu.flywheel.backend.engine.textures.TextureBinder; -import com.jozufozu.flywheel.backend.engine.textures.TextureSource; +import com.jozufozu.flywheel.backend.engine.TextureBinder; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.gl.Driver; import com.jozufozu.flywheel.backend.gl.GlCompat; @@ -40,7 +42,8 @@ public class IndirectCullingGroup { private static final int DRAW_BARRIER_BITS = GL_SHADER_STORAGE_BARRIER_BIT | GL_COMMAND_BARRIER_BIT; private final InstanceType instanceType; - private final Context context; + @Nullable + private final VisualEmbedding embedding; private final long objectStride; private final IndirectBuffers buffers; private final List> instancers = new ArrayList<>(); @@ -56,18 +59,17 @@ public class IndirectCullingGroup { private boolean needsDrawSort; private int instanceCountThisFrame; - IndirectCullingGroup(InstanceType instanceType, Context context, IndirectPrograms programs) { + IndirectCullingGroup(InstanceType instanceType, @Nullable VisualEmbedding embedding, IndirectPrograms programs) { this.instanceType = instanceType; - this.context = context; + this.embedding = embedding; objectStride = instanceType.layout() .byteSize() + IndirectBuffers.INT_SIZE; buffers = new IndirectBuffers(objectStride); this.programs = programs; - // TODO: Culling programs need to be context aware. cullProgram = programs.getCullingProgram(instanceType); applyProgram = programs.getApplyProgram(); - drawProgram = programs.getIndirectProgram(instanceType, context.contextShader()); + drawProgram = programs.getIndirectProgram(instanceType, ContextShaders.forEmbedding(embedding)); } public void flushInstancers() { @@ -128,6 +130,14 @@ public class IndirectCullingGroup { Uniforms.bindFrame(); cullProgram.bind(); + + if (embedding != null) { + cullProgram.setBool("_flw_useEmbeddedModel", true); + cullProgram.setMat4("_flw_embeddedModel", embedding.pose()); + } else { + cullProgram.setBool("_flw_useEmbeddedModel", false); + } + buffers.bindForCompute(); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glDispatchCompute(GlCompat.getComputeGroupCount(instanceCountThisFrame), 1, 1); @@ -191,7 +201,7 @@ public class IndirectCullingGroup { needsDrawSort = true; } - public void submit(RenderStage stage, TextureSource textures) { + public void submit(RenderStage stage) { if (nothingToDo(stage)) { return; } @@ -199,6 +209,13 @@ public class IndirectCullingGroup { drawProgram.bind(); buffers.bindForDraw(); + if (embedding != null) { + drawProgram.setVec3("_flw_oneOverLightBoxSize", 1, 1, 1); + drawProgram.setVec3("_flw_lightVolumeMin", 0, 0, 0); + drawProgram.setMat4("_flw_model", embedding.pose()); + drawProgram.setMat3("_flw_normal", embedding.normal()); + } + drawBarrier(); var flwBaseDraw = drawProgram.getUniformLocation("_flw_baseDraw"); @@ -206,7 +223,6 @@ public class IndirectCullingGroup { for (var multiDraw : multiDraws.get(stage)) { glUniform1ui(flwBaseDraw, multiDraw.start); - context.prepare(multiDraw.material, drawProgram, textures); MaterialRenderState.setup(multiDraw.material); multiDraw.submit(); @@ -214,7 +230,7 @@ public class IndirectCullingGroup { } } - public GlProgram bindWithContextShader(SimpleContextShader override) { + public GlProgram bindWithContextShader(ContextShader override) { var program = programs.getIndirectProgram(instanceType, override); program.bind(); 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 382782c92..147455b98 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 @@ -11,21 +11,22 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.jetbrains.annotations.Nullable; + import com.jozufozu.flywheel.api.backend.Engine; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; +import com.jozufozu.flywheel.backend.compile.ContextShaders; import com.jozufozu.flywheel.backend.compile.IndirectPrograms; -import com.jozufozu.flywheel.backend.context.Context; -import com.jozufozu.flywheel.backend.context.ContextShaders; import com.jozufozu.flywheel.backend.engine.CommonCrumbling; import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl; import com.jozufozu.flywheel.backend.engine.InstancerKey; import com.jozufozu.flywheel.backend.engine.InstancerStorage; import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.MeshPool; -import com.jozufozu.flywheel.backend.engine.textures.TextureBinder; -import com.jozufozu.flywheel.backend.engine.textures.TextureSource; +import com.jozufozu.flywheel.backend.engine.TextureBinder; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; @@ -44,7 +45,6 @@ public class IndirectDrawManager extends InstancerStorage> private final StagingBuffer stagingBuffer; private final MeshPool meshPool; private final GlVertexArray vertexArray; - private final TextureSource textures = new TextureSource(); private final Map, IndirectCullingGroup> cullingGroups = new HashMap<>(); private final GlBuffer crumblingDrawBuffer = new GlBuffer(); @@ -61,13 +61,13 @@ public class IndirectDrawManager extends InstancerStorage> @Override protected IndirectInstancer create(InstancerKey key) { - return new IndirectInstancer<>(key.type(), key.context(), key.model()); + return new IndirectInstancer<>(key.type(), key.embedding(), key.model()); } @SuppressWarnings("unchecked") @Override protected void initialize(InstancerKey key, IndirectInstancer instancer) { - var groupKey = new GroupKey<>(key.type(), key.context()); + var groupKey = new GroupKey<>(key.type(), key.embedding()); var group = (IndirectCullingGroup) cullingGroups.computeIfAbsent(groupKey, t -> new IndirectCullingGroup<>(t.type, t.context, programs)); group.add((IndirectInstancer) instancer, key.model(), key.stage(), meshPool); } @@ -88,7 +88,7 @@ public class IndirectDrawManager extends InstancerStorage> Uniforms.bindForDraw(); for (var group : cullingGroups.values()) { - group.submit(stage, textures); + group.submit(stage); } MaterialRenderState.reset(); @@ -168,7 +168,7 @@ public class IndirectDrawManager extends InstancerStorage> .bindWithContextShader(ContextShaders.CRUMBLING); for (var progressEntry : byProgress.int2ObjectEntrySet()) { - program.setTexture("crumblingTex", textures.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()))); + program.setTexture("crumblingTex", TextureBinder.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()))); for (var instanceHandlePair : progressEntry.getValue()) { IndirectInstancer instancer = instanceHandlePair.first(); @@ -221,7 +221,7 @@ public class IndirectDrawManager extends InstancerStorage> continue; } - byType.computeIfAbsent(new GroupKey<>(instancer.type, instancer.context), $ -> new Int2ObjectArrayMap<>()) + byType.computeIfAbsent(new GroupKey<>(instancer.type, instancer.embedding), $ -> new Int2ObjectArrayMap<>()) .computeIfAbsent(progress, $ -> new ArrayList<>()) .add(Pair.of(instancer, impl)); } @@ -229,6 +229,6 @@ public class IndirectDrawManager extends InstancerStorage> return byType; } - public record GroupKey(InstanceType type, Context context) { + public record GroupKey(InstanceType type, @Nullable VisualEmbedding context) { } } 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 1e638aba0..22ed21e5e 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 @@ -3,6 +3,7 @@ package com.jozufozu.flywheel.backend.engine.indirect; import java.util.ArrayList; import java.util.List; +import org.jetbrains.annotations.Nullable; import org.joml.Vector4fc; import org.lwjgl.system.MemoryUtil; @@ -10,7 +11,7 @@ import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceWriter; import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.backend.context.Context; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import com.jozufozu.flywheel.backend.engine.AbstractInstancer; public class IndirectInstancer extends AbstractInstancer { @@ -25,7 +26,7 @@ public class IndirectInstancer extends AbstractInstancer private int lastModelIndex = -1; private long lastStartPos = -1; - public IndirectInstancer(InstanceType type, Context context, Model model) { + public IndirectInstancer(InstanceType type, @Nullable VisualEmbedding context, Model model) { super(type, context); this.objectStride = type.layout() .byteSize() + IndirectBuffers.INT_SIZE; diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java index 92df01184..2ba884f44 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedDrawManager.java @@ -19,8 +19,8 @@ import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.material.Material; import com.jozufozu.flywheel.backend.ShaderIndices; +import com.jozufozu.flywheel.backend.compile.ContextShaders; import com.jozufozu.flywheel.backend.compile.InstancingPrograms; -import com.jozufozu.flywheel.backend.context.ContextShaders; import com.jozufozu.flywheel.backend.engine.CommonCrumbling; import com.jozufozu.flywheel.backend.engine.InstanceHandleImpl; import com.jozufozu.flywheel.backend.engine.InstancerKey; @@ -28,8 +28,7 @@ import com.jozufozu.flywheel.backend.engine.InstancerStorage; import com.jozufozu.flywheel.backend.engine.MaterialEncoder; import com.jozufozu.flywheel.backend.engine.MaterialRenderState; import com.jozufozu.flywheel.backend.engine.MeshPool; -import com.jozufozu.flywheel.backend.engine.textures.TextureBinder; -import com.jozufozu.flywheel.backend.engine.textures.TextureSource; +import com.jozufozu.flywheel.backend.engine.TextureBinder; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.GlTextureUnit; @@ -53,7 +52,6 @@ public class InstancedDrawManager extends InstancerStorage */ private final MeshPool meshPool; private final GlVertexArray vao; - private final TextureSource textures; private final TextureBuffer instanceTexture; public InstancedDrawManager(InstancingPrograms programs) { @@ -62,7 +60,6 @@ public class InstancedDrawManager extends InstancerStorage meshPool = new MeshPool(); vao = GlVertexArray.create(); - textures = new TextureSource(); instanceTexture = new TextureBuffer(); meshPool.bind(vao); @@ -133,15 +130,14 @@ public class InstancedDrawManager extends InstancerStorage continue; } - var context = shader.context(); + var embedding = shader.embedding(); var material = shader.material(); - var program = programs.get(shader.instanceType(), context.contextShader()); + var program = programs.get(shader.instanceType(), ContextShaders.forEmbedding(embedding)); program.bind(); uploadMaterialUniform(program, material); - context.prepare(material, program, textures); MaterialRenderState.setup(material); GlTextureUnit.T3.makeActive(); @@ -160,7 +156,7 @@ public class InstancedDrawManager extends InstancerStorage @Override protected InstancedInstancer create(InstancerKey key) { - return new InstancedInstancer<>(key.type(), key.context()); + return new InstancedInstancer<>(key.type(), key.embedding()); } @Override @@ -174,7 +170,7 @@ public class InstancedDrawManager extends InstancerStorage for (var entry : meshes) { var mesh = meshPool.alloc(entry.mesh()); - ShaderState shaderState = new ShaderState(entry.material(), key.type(), key.context()); + ShaderState shaderState = new ShaderState(entry.material(), key.type(), key.embedding()); DrawCall drawCall = new DrawCall(instancer, mesh, shaderState); drawSet.put(shaderState, drawCall); @@ -222,7 +218,7 @@ public class InstancedDrawManager extends InstancerStorage continue; } - program.setTexture("crumblingTex", textures.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()))); + program.setTexture("crumblingTex", TextureBinder.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()))); GlTextureUnit.T3.makeActive(); program.setSamplerBinding("_flw_instances", 3); diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedInstancer.java index 297d357db..1682a7011 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/InstancedInstancer.java @@ -8,7 +8,7 @@ import org.jetbrains.annotations.Nullable; import com.jozufozu.flywheel.api.instance.Instance; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.InstanceWriter; -import com.jozufozu.flywheel.backend.context.Context; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import com.jozufozu.flywheel.backend.engine.AbstractInstancer; import com.jozufozu.flywheel.backend.gl.TextureBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; @@ -25,7 +25,7 @@ public class InstancedInstancer extends AbstractInstancer private final List drawCalls = new ArrayList<>(); - public InstancedInstancer(InstanceType type, Context context) { + public InstancedInstancer(InstanceType type, @Nullable VisualEmbedding context) { super(type, context); var layout = type.layout(); // Align to one texel in the texture buffer diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ShaderState.java b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ShaderState.java index 34ff28805..e70b6fb28 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ShaderState.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/instancing/ShaderState.java @@ -1,8 +1,10 @@ package com.jozufozu.flywheel.backend.engine.instancing; +import org.jetbrains.annotations.Nullable; + import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.backend.context.Context; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; -public record ShaderState(Material material, InstanceType instanceType, Context context) { +public record ShaderState(Material material, InstanceType instanceType, @Nullable VisualEmbedding embedding) { } diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/IdentifiedTexture.java b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/IdentifiedTexture.java deleted file mode 100644 index 8b1298e23..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/IdentifiedTexture.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.jozufozu.flywheel.backend.engine.textures; - -import com.jozufozu.flywheel.backend.context.Texture; - -/** - * Internal base interface that {@link com.jozufozu.flywheel.backend.gl.shader.GlProgram GlProgram} expects. - */ -public interface IdentifiedTexture extends Texture { - /** - * @return The GL texture id of this texture. - */ - int id(); -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java deleted file mode 100644 index 70af8a647..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.jozufozu.flywheel.backend.engine.textures; - -import java.util.HashMap; -import java.util.Map; - -import com.jozufozu.flywheel.backend.context.Texture; -import com.jozufozu.flywheel.backend.mixin.LightTextureAccessor; -import com.jozufozu.flywheel.backend.mixin.OverlayTextureAccessor; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.AbstractTexture; -import net.minecraft.resources.ResourceLocation; - -public class TextureSource { - - private final DirectTexture lightTexture; - private final DirectTexture overlayTexture; - private final Map wrappers = new HashMap<>(); - - public TextureSource() { - var gameRenderer = Minecraft.getInstance().gameRenderer; - - this.lightTexture = new DirectTexture(((LightTextureAccessor) gameRenderer.lightTexture()).flywheel$texture() - .getId()); - this.overlayTexture = new DirectTexture(((OverlayTextureAccessor) gameRenderer.overlayTexture()).flywheel$texture() - .getId()); - } - - - /** - * Get a built-in texture by its resource location. - * - * @param texture The texture's resource location. - * @return The texture. - */ - public Texture byName(ResourceLocation texture) { - return wrappers.computeIfAbsent(texture, key -> new WrappedTexture(Minecraft.getInstance() - .getTextureManager() - .getTexture(key))); - } - - /** - * Get the overlay texture. - * - * @return The overlay texture. - */ - public Texture overlay() { - return overlayTexture; - } - - /** - * Get the light texture. - * - * @return The light texture. - */ - public Texture light() { - return lightTexture; - } - - public record WrappedTexture(AbstractTexture texture) implements IdentifiedTexture { - @Override - public void filter(boolean blur, boolean mipmap) { - texture.setFilter(blur, mipmap); - } - - @Override - public int id() { - return texture.getId(); - } - } - - public record DirectTexture(int id) implements IdentifiedTexture { - @Override - public void filter(boolean blur, boolean mipmap) { - // no-op - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java index b3890e4b4..58e66233f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java @@ -18,9 +18,7 @@ import org.joml.Matrix3fc; import org.joml.Matrix4fc; import org.slf4j.Logger; -import com.jozufozu.flywheel.backend.context.Texture; -import com.jozufozu.flywheel.backend.engine.textures.IdentifiedTexture; -import com.jozufozu.flywheel.backend.engine.textures.TextureBinder; +import com.jozufozu.flywheel.backend.engine.TextureBinder; import com.jozufozu.flywheel.backend.gl.GlObject; import com.mojang.blaze3d.shaders.ProgramManager; import com.mojang.logging.LogUtils; @@ -45,19 +43,14 @@ public class GlProgram extends GlObject { ProgramManager.glUseProgram(0); } - public void setTexture(String glslName, Texture texture) { - if (!(texture instanceof IdentifiedTexture identified)) { - return; - } + public void setTexture(String glslName, int texture) { int uniform = getUniformLocation(glslName); if (uniform < 0) { return; } - int id = identified.id(); - - int binding = TextureBinder.bindTexture(id); + int binding = TextureBinder.bindTexture(texture); glUniform1i(uniform, binding); } @@ -122,6 +115,16 @@ public class GlProgram extends GlObject { glUniformMatrix3fv(uniform, false, matrix.get(new float[9])); } + public void setBool(String glslName, boolean bool) { + int uniform = getUniformLocation(glslName); + + if (uniform < 0) { + return; + } + + glUniform1i(uniform, bool ? 1 : 0); + } + /** * Retrieves the index of the uniform with the given name. * diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/InstancerProviderImpl.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/InstancerProviderImpl.java index 7aa96716c..2addd894d 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/InstancerProviderImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/InstancerProviderImpl.java @@ -9,7 +9,7 @@ import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.instance.Instancer; import com.jozufozu.flywheel.api.instance.InstancerProvider; import com.jozufozu.flywheel.api.model.Model; -import com.jozufozu.flywheel.api.visualization.EmbeddedLevel; +import com.jozufozu.flywheel.api.visualization.VisualEmbedding; import com.jozufozu.flywheel.api.visualization.VisualizationContext; public class InstancerProviderImpl implements InstancerProvider, Supplier { @@ -31,7 +31,7 @@ public class InstancerProviderImpl implements InstancerProvider, Supplier