diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 891d1538a..d50f2ebcb 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -11,6 +11,7 @@ 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; @@ -19,7 +20,6 @@ import com.jozufozu.flywheel.impl.BackendManagerImpl; import com.jozufozu.flywheel.impl.registry.IdRegistryImpl; import com.jozufozu.flywheel.impl.registry.RegistryImpl; import com.jozufozu.flywheel.impl.visualization.VisualizationEventHandler; -import com.jozufozu.flywheel.lib.context.ContextShaders; import com.jozufozu.flywheel.lib.instance.InstanceTypes; import com.jozufozu.flywheel.lib.material.CutoutShaders; import com.jozufozu.flywheel.lib.material.FogShaders; 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 c43c21688..63a72723b 100644 --- a/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java +++ b/src/main/java/com/jozufozu/flywheel/api/backend/Engine.java @@ -3,7 +3,6 @@ package com.jozufozu.flywheel.api.backend; import java.util.List; import com.jozufozu.flywheel.api.BackendImplemented; -import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.api.event.RenderStage; import com.jozufozu.flywheel.api.instance.Instance; @@ -13,6 +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 net.minecraft.client.Camera; import net.minecraft.core.BlockPos; @@ -32,7 +32,9 @@ public interface Engine { * @return An instancer for the given instance type, model, and render stage. * @see InstancerProvider */ - Instancer instancer(InstanceType type, Context context, Model model, RenderStage stage); + Instancer instancer(InstanceType type, Model model, RenderStage stage); + + Instancer instancer(EmbeddedLevel 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/context/Shader.java b/src/main/java/com/jozufozu/flywheel/api/context/Shader.java deleted file mode 100644 index e91a9f312..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/context/Shader.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jozufozu.flywheel.api.context; - -import org.joml.Matrix4fc; - -import com.jozufozu.flywheel.api.BackendImplemented; - -@BackendImplemented -public interface Shader { - void setTexture(String glslName, Texture texture); - - void setFloat(String glslName, float value); - - void setVec2(String glslName, float x, float y); - - void setVec3(String glslName, float x, float y, float z); - - void setVec4(String glslName, float x, float y, float z, float w); - - void setMat4(String glslName, Matrix4fc matrix); -} diff --git a/src/main/java/com/jozufozu/flywheel/api/context/TextureSource.java b/src/main/java/com/jozufozu/flywheel/api/context/TextureSource.java deleted file mode 100644 index 152b087c7..000000000 --- a/src/main/java/com/jozufozu/flywheel/api/context/TextureSource.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jozufozu.flywheel.api.context; - -import com.jozufozu.flywheel.api.BackendImplemented; - -import net.minecraft.resources.ResourceLocation; - -@BackendImplemented -public interface TextureSource { - /** - * Get a built-in texture by its resource location. - * - * @param texture The texture's resource location. - * @return The texture. - */ - Texture byName(ResourceLocation texture); - - /** - * Get the overlay texture. - * - * @return The overlay texture. - */ - Texture overlay(); - - /** - * Get the light texture. - * - * @return The light texture. - */ - Texture light(); - - // TODO: Allow creating dynamic textures. -} diff --git a/src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java b/src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java new file mode 100644 index 000000000..b821c3447 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/api/visualization/EmbeddedLevel.java @@ -0,0 +1,11 @@ +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/VisualizationContext.java b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java index b813c25e7..759841971 100644 --- a/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java +++ b/src/main/java/com/jozufozu/flywheel/api/visualization/VisualizationContext.java @@ -2,8 +2,8 @@ package com.jozufozu.flywheel.api.visualization; import org.jetbrains.annotations.ApiStatus; -import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.instance.InstancerProvider; +import com.jozufozu.flywheel.backend.context.Context; import net.minecraft.core.Vec3i; @@ -32,5 +32,5 @@ public interface VisualizationContext { * @return A new {@link VisualizationContext} for use with child visuals. */ @ApiStatus.Experimental - VisualizationContext withContext(Context context, Vec3i renderOrigin); + VisualizationContext embed(EmbeddedLevel world); } 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 b01e35f4b..528a7ffbe 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/FlwPrograms.java @@ -8,12 +8,12 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.context.ContextShader; import com.jozufozu.flywheel.api.instance.InstanceType; 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; 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 7d4e16b5b..5887a931a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/IndirectPrograms.java @@ -7,11 +7,11 @@ import org.jetbrains.annotations.Nullable; import com.google.common.collect.ImmutableList; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.context.ContextShader; 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 e13890d2a..fd005d09e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/compile/InstancingPrograms.java @@ -6,8 +6,8 @@ import java.util.Map; import org.jetbrains.annotations.Nullable; import com.google.common.collect.ImmutableList; -import com.jozufozu.flywheel.api.context.ContextShader; 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 2f39c68da..fc0c27722 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,7 @@ package com.jozufozu.flywheel.backend.compile; -import com.jozufozu.flywheel.api.context.ContextShader; 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/api/context/Context.java b/src/main/java/com/jozufozu/flywheel/backend/context/Context.java similarity index 58% rename from src/main/java/com/jozufozu/flywheel/api/context/Context.java rename to src/main/java/com/jozufozu/flywheel/backend/context/Context.java index 40c4f51ea..2285e95d3 100644 --- a/src/main/java/com/jozufozu/flywheel/api/context/Context.java +++ b/src/main/java/com/jozufozu/flywheel/backend/context/Context.java @@ -1,6 +1,8 @@ -package com.jozufozu.flywheel.api.context; +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(); @@ -12,5 +14,5 @@ public interface Context { * @param shader The shader to prepare. * @param textureSource Source of the textures to use. */ - void prepare(Material material, Shader shader, TextureSource textureSource); + void prepare(Material material, GlProgram shader, TextureSource textureSource); } diff --git a/src/main/java/com/jozufozu/flywheel/api/context/ContextShader.java b/src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java similarity index 88% rename from src/main/java/com/jozufozu/flywheel/api/context/ContextShader.java rename to src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java index 8c8dfef15..916575933 100644 --- a/src/main/java/com/jozufozu/flywheel/api/context/ContextShader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/context/ContextShader.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.api.context; +package com.jozufozu.flywheel.backend.context; import com.jozufozu.flywheel.api.internal.InternalFlywheelApi; import com.jozufozu.flywheel.api.registry.Registry; diff --git a/src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java b/src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java new file mode 100644 index 000000000..bf4f71b64 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/context/ContextShaders.java @@ -0,0 +1,18 @@ +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 new file mode 100644 index 000000000..25ce84397 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/context/Contexts.java @@ -0,0 +1,9 @@ +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 new file mode 100644 index 000000000..818669030 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/context/EmbeddedContext.java @@ -0,0 +1,33 @@ +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/lib/context/SimpleContext.java b/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java similarity index 74% rename from src/main/java/com/jozufozu/flywheel/lib/context/SimpleContext.java rename to src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java index ecb8a2092..51331a1dc 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/context/SimpleContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContext.java @@ -1,12 +1,10 @@ -package com.jozufozu.flywheel.lib.context; +package com.jozufozu.flywheel.backend.context; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.api.context.Context; -import com.jozufozu.flywheel.api.context.ContextShader; -import com.jozufozu.flywheel.api.context.Shader; -import com.jozufozu.flywheel.api.context.TextureSource; 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; @@ -27,13 +25,13 @@ public class SimpleContext implements Context { } @Override - public void prepare(Material material, Shader shader, TextureSource textureSource) { + public void prepare(Material material, GlProgram shader, TextureSource textureSource) { preparation.prepare(material, shader, textureSource); } @FunctionalInterface public interface Preparation { - void prepare(Material material, Shader shader, TextureSource textureSource); + void prepare(Material material, GlProgram shader, TextureSource textureSource); } public static class Builder { diff --git a/src/main/java/com/jozufozu/flywheel/lib/context/SimpleContextShader.java b/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java similarity index 77% rename from src/main/java/com/jozufozu/flywheel/lib/context/SimpleContextShader.java rename to src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java index a14009894..05b1723e1 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/context/SimpleContextShader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/context/SimpleContextShader.java @@ -1,6 +1,4 @@ -package com.jozufozu.flywheel.lib.context; - -import com.jozufozu.flywheel.api.context.ContextShader; +package com.jozufozu.flywheel.backend.context; import net.minecraft.resources.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/api/context/Texture.java b/src/main/java/com/jozufozu/flywheel/backend/context/Texture.java similarity index 75% rename from src/main/java/com/jozufozu/flywheel/api/context/Texture.java rename to src/main/java/com/jozufozu/flywheel/backend/context/Texture.java index 362abf38e..b5de27323 100644 --- a/src/main/java/com/jozufozu/flywheel/api/context/Texture.java +++ b/src/main/java/com/jozufozu/flywheel/backend/context/Texture.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.api.context; +package com.jozufozu.flywheel.backend.context; import com.jozufozu.flywheel.api.BackendImplemented; 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 2a9d9f3d0..b425b613c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractEngine.java @@ -1,12 +1,14 @@ package com.jozufozu.flywheel.backend.engine; import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.context.Context; 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.api.visualization.EmbeddedLevel; +import com.jozufozu.flywheel.backend.context.Contexts; +import com.jozufozu.flywheel.backend.context.EmbeddedContext; import net.minecraft.client.Camera; import net.minecraft.core.BlockPos; @@ -22,8 +24,13 @@ public abstract class AbstractEngine implements Engine { } @Override - public Instancer instancer(InstanceType type, Context context, Model model, RenderStage stage) { - return getStorage().getInstancer(type, context, model, stage); + public Instancer instancer(InstanceType type, Model model, RenderStage stage) { + return getStorage().getInstancer(type, Contexts.DEFAULT, model, stage); + } + + @Override + public Instancer instancer(EmbeddedLevel world, InstanceType type, Model model, RenderStage stage) { + return getStorage().getInstancer(type, new EmbeddedContext(world), 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 4772766a3..924884bce 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/AbstractInstancer.java @@ -2,10 +2,10 @@ package com.jozufozu.flywheel.backend.engine; import java.util.ArrayList; -import com.jozufozu.flywheel.api.context.Context; 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.lib.util.AtomicBitset; public abstract class AbstractInstancer implements Instancer { 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 148c9963e..87a91c598 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,10 @@ package com.jozufozu.flywheel.backend.engine; -import com.jozufozu.flywheel.api.context.Context; 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; public record InstancerKey(InstanceType type, Context context, 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 aafb42d81..f56031ecc 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerStorage.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/InstancerStorage.java @@ -6,12 +6,12 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.context.Context; 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; public abstract class InstancerStorage> { /** 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 6e7b555d0..77ccdcf0c 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,22 +16,22 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; -import com.jozufozu.flywheel.api.context.Context; 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.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.TextureSourceImpl; +import com.jozufozu.flywheel.backend.engine.textures.TextureSource; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.gl.Driver; import com.jozufozu.flywheel.backend.gl.GlCompat; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.jozufozu.flywheel.lib.context.SimpleContextShader; public class IndirectCullingGroup { private static final Comparator DRAW_COMPARATOR = Comparator.comparing(IndirectDraw::stage) @@ -191,7 +191,7 @@ public class IndirectCullingGroup { needsDrawSort = true; } - public void submit(RenderStage stage, TextureSourceImpl textures) { + public void submit(RenderStage stage, TextureSource textures) { if (nothingToDo(stage)) { return; } 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 30c8d9e9c..382782c92 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 @@ -12,11 +12,12 @@ import java.util.List; import java.util.Map; import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.context.Context; 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.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; @@ -24,14 +25,12 @@ 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.TextureSourceImpl; +import com.jozufozu.flywheel.backend.engine.textures.TextureSource; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; -import com.jozufozu.flywheel.lib.context.ContextShaders; -import com.jozufozu.flywheel.lib.context.Contexts; import com.jozufozu.flywheel.lib.material.SimpleMaterial; import com.jozufozu.flywheel.lib.memory.MemoryBlock; import com.jozufozu.flywheel.lib.util.Pair; @@ -45,7 +44,7 @@ public class IndirectDrawManager extends InstancerStorage> private final StagingBuffer stagingBuffer; private final MeshPool meshPool; private final GlVertexArray vertexArray; - private final TextureSourceImpl textures = new TextureSourceImpl(); + private final TextureSource textures = new TextureSource(); private final Map, IndirectCullingGroup> cullingGroups = new HashMap<>(); private final GlBuffer crumblingDrawBuffer = new GlBuffer(); @@ -169,6 +168,8 @@ public class IndirectDrawManager extends InstancerStorage> .bindWithContextShader(ContextShaders.CRUMBLING); for (var progressEntry : byProgress.int2ObjectEntrySet()) { + program.setTexture("crumblingTex", textures.byName(ModelBakery.BREAKING_LOCATIONS.get(progressEntry.getIntKey()))); + for (var instanceHandlePair : progressEntry.getValue()) { IndirectInstancer instancer = instanceHandlePair.first(); int instanceIndex = instanceHandlePair.second().index; @@ -177,9 +178,7 @@ public class IndirectDrawManager extends InstancerStorage> // Transform the material to be suited for crumbling. CommonCrumbling.applyCrumblingProperties(crumblingMaterial, draw.material()); - var context = Contexts.CRUMBLING.get(progressEntry.getIntKey()); - context.prepare(crumblingMaterial, program, textures); MaterialRenderState.setup(crumblingMaterial); // Upload the draw command. @@ -188,10 +187,10 @@ public class IndirectDrawManager extends InstancerStorage> // Submit! Everything is already bound by here. glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, 0); - - TextureBinder.resetTextureBindings(); } } + + TextureBinder.resetTextureBindings(); } } 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 f7f42a0d2..1e638aba0 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 @@ -6,11 +6,11 @@ import java.util.List; import org.joml.Vector4fc; import org.lwjgl.system.MemoryUtil; -import com.jozufozu.flywheel.api.context.Context; 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.backend.engine.AbstractInstancer; public class IndirectInstancer extends AbstractInstancer { 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 82e08917d..92df01184 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 @@ -20,6 +20,7 @@ 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.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,15 +29,13 @@ 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.TextureSourceImpl; +import com.jozufozu.flywheel.backend.engine.textures.TextureSource; import com.jozufozu.flywheel.backend.engine.uniform.Uniforms; import com.jozufozu.flywheel.backend.gl.GlStateTracker; import com.jozufozu.flywheel.backend.gl.GlTextureUnit; import com.jozufozu.flywheel.backend.gl.TextureBuffer; import com.jozufozu.flywheel.backend.gl.array.GlVertexArray; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.jozufozu.flywheel.lib.context.ContextShaders; -import com.jozufozu.flywheel.lib.context.Contexts; import com.jozufozu.flywheel.lib.material.SimpleMaterial; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; @@ -54,7 +53,7 @@ public class InstancedDrawManager extends InstancerStorage */ private final MeshPool meshPool; private final GlVertexArray vao; - private final TextureSourceImpl textures; + private final TextureSource textures; private final TextureBuffer instanceTexture; public InstancedDrawManager(InstancingPrograms programs) { @@ -63,7 +62,7 @@ public class InstancedDrawManager extends InstancerStorage meshPool = new MeshPool(); vao = GlVertexArray.create(); - textures = new TextureSourceImpl(); + textures = new TextureSource(); instanceTexture = new TextureBuffer(); meshPool.bind(vao); @@ -223,8 +222,7 @@ public class InstancedDrawManager extends InstancerStorage continue; } - var context = Contexts.CRUMBLING.get(progressEntry.getIntKey()); - context.prepare(crumblingMaterial, program, textures); + program.setTexture("crumblingTex", textures.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 5f7fce310..297d357db 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 @@ -5,10 +5,10 @@ import java.util.List; import org.jetbrains.annotations.Nullable; -import com.jozufozu.flywheel.api.context.Context; 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.backend.engine.AbstractInstancer; import com.jozufozu.flywheel.backend.gl.TextureBuffer; import com.jozufozu.flywheel.backend.gl.buffer.GlBuffer; 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 e92992998..34ff28805 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,8 @@ package com.jozufozu.flywheel.backend.engine.instancing; -import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.instance.InstanceType; import com.jozufozu.flywheel.api.material.Material; +import com.jozufozu.flywheel.backend.context.Context; public record ShaderState(Material material, InstanceType instanceType, Context context) { } 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 index 4f441c8da..8b1298e23 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/IdentifiedTexture.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/IdentifiedTexture.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.backend.engine.textures; -import com.jozufozu.flywheel.api.context.Texture; +import com.jozufozu.flywheel.backend.context.Texture; /** * Internal base interface that {@link com.jozufozu.flywheel.backend.gl.shader.GlProgram GlProgram} expects. diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSourceImpl.java b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java similarity index 80% rename from src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSourceImpl.java rename to src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java index ad0e00d18..70af8a647 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSourceImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/textures/TextureSource.java @@ -3,8 +3,7 @@ package com.jozufozu.flywheel.backend.engine.textures; import java.util.HashMap; import java.util.Map; -import com.jozufozu.flywheel.api.context.Texture; -import com.jozufozu.flywheel.api.context.TextureSource; +import com.jozufozu.flywheel.backend.context.Texture; import com.jozufozu.flywheel.backend.mixin.LightTextureAccessor; import com.jozufozu.flywheel.backend.mixin.OverlayTextureAccessor; @@ -12,13 +11,13 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.AbstractTexture; import net.minecraft.resources.ResourceLocation; -public class TextureSourceImpl implements TextureSource { +public class TextureSource { private final DirectTexture lightTexture; private final DirectTexture overlayTexture; private final Map wrappers = new HashMap<>(); - public TextureSourceImpl() { + public TextureSource() { var gameRenderer = Minecraft.getInstance().gameRenderer; this.lightTexture = new DirectTexture(((LightTextureAccessor) gameRenderer.lightTexture()).flywheel$texture() @@ -28,19 +27,32 @@ public class TextureSourceImpl implements TextureSource { } - @Override + /** + * 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))); } - @Override + /** + * Get the overlay texture. + * + * @return The overlay texture. + */ public Texture overlay() { return overlayTexture; } - @Override + /** + * Get the light texture. + * + * @return The light texture. + */ public Texture light() { return lightTexture; } 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 8617fd3ee..b3890e4b4 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 @@ -8,16 +8,17 @@ import static org.lwjgl.opengl.GL20.glUniform1i; import static org.lwjgl.opengl.GL20.glUniform2f; import static org.lwjgl.opengl.GL20.glUniform3f; import static org.lwjgl.opengl.GL20.glUniform4f; +import static org.lwjgl.opengl.GL20.glUniformMatrix3fv; import static org.lwjgl.opengl.GL20.glUniformMatrix4fv; import static org.lwjgl.opengl.GL31.GL_INVALID_INDEX; import static org.lwjgl.opengl.GL31.glGetUniformBlockIndex; import static org.lwjgl.opengl.GL31.glUniformBlockBinding; +import org.joml.Matrix3fc; import org.joml.Matrix4fc; import org.slf4j.Logger; -import com.jozufozu.flywheel.api.context.Shader; -import com.jozufozu.flywheel.api.context.Texture; +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.gl.GlObject; @@ -27,7 +28,7 @@ import com.mojang.logging.LogUtils; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; -public class GlProgram extends GlObject implements Shader { +public class GlProgram extends GlObject { private static final Logger LOGGER = LogUtils.getLogger(); private final Object2IntMap uniformLocationCache = new Object2IntOpenHashMap<>(); @@ -44,10 +45,7 @@ public class GlProgram extends GlObject implements Shader { ProgramManager.glUseProgram(0); } - @Override public void setTexture(String glslName, Texture texture) { - throwIfReserved(glslName); - if (!(texture instanceof IdentifiedTexture identified)) { return; } @@ -64,10 +62,7 @@ public class GlProgram extends GlObject implements Shader { glUniform1i(uniform, binding); } - @Override public void setFloat(String glslName, float value) { - throwIfReserved(glslName); - int uniform = getUniformLocation(glslName); if (uniform < 0) { @@ -77,10 +72,7 @@ public class GlProgram extends GlObject implements Shader { glUniform1f(uniform, value); } - @Override public void setVec2(String glslName, float x, float y) { - throwIfReserved(glslName); - int uniform = getUniformLocation(glslName); if (uniform < 0) { @@ -90,10 +82,7 @@ public class GlProgram extends GlObject implements Shader { glUniform2f(uniform, x, y); } - @Override public void setVec3(String glslName, float x, float y, float z) { - throwIfReserved(glslName); - int uniform = getUniformLocation(glslName); if (uniform < 0) { @@ -103,10 +92,7 @@ public class GlProgram extends GlObject implements Shader { glUniform3f(uniform, x, y, z); } - @Override public void setVec4(String glslName, float x, float y, float z, float w) { - throwIfReserved(glslName); - int uniform = getUniformLocation(glslName); if (uniform < 0) { @@ -116,10 +102,7 @@ public class GlProgram extends GlObject implements Shader { glUniform4f(uniform, x, y, z, w); } - @Override public void setMat4(String glslName, Matrix4fc matrix) { - throwIfReserved(glslName); - int uniform = getUniformLocation(glslName); if (uniform < 0) { @@ -129,6 +112,16 @@ public class GlProgram extends GlObject implements Shader { glUniformMatrix4fv(uniform, false, matrix.get(new float[16])); } + public void setMat3(String glslName, Matrix3fc matrix) { + int uniform = getUniformLocation(glslName); + + if (uniform < 0) { + return; + } + + glUniformMatrix3fv(uniform, false, matrix.get(new float[9])); + } + /** * Retrieves the index of the uniform with the given name. * @@ -179,18 +172,4 @@ public class GlProgram extends GlObject implements Shader { protected void deleteInternal(int handle) { glDeleteProgram(handle); } - - public static void throwIfReserved(String glslName) { - if (glslName.startsWith("flw_")) { - throw new IllegalArgumentException("Uniform names starting with flw_are reserved"); - } - - if (glslName.startsWith("_flw_")) { - throw new IllegalArgumentException("Uniform names starting with _flw_ are reserved for internal use"); - } - - if (glslName.startsWith("gl_")) { - throw new IllegalArgumentException("Uniform names cannot start with gl_"); - } - } } 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 ad09ff409..7aa96716c 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/InstancerProviderImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/InstancerProviderImpl.java @@ -3,25 +3,63 @@ package com.jozufozu.flywheel.impl.visualization; import java.util.function.Supplier; import com.jozufozu.flywheel.api.backend.Engine; -import com.jozufozu.flywheel.api.context.Context; 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.instance.InstancerProvider; import com.jozufozu.flywheel.api.model.Model; +import com.jozufozu.flywheel.api.visualization.EmbeddedLevel; import com.jozufozu.flywheel.api.visualization.VisualizationContext; -public record InstancerProviderImpl(Engine engine, - Context context, - RenderStage renderStage) implements InstancerProvider, Supplier { +public class InstancerProviderImpl implements InstancerProvider, Supplier { + protected final Engine engine; + protected final RenderStage renderStage; + + public InstancerProviderImpl(Engine engine, RenderStage renderStage) { + this.engine = engine; + this.renderStage = renderStage; + } + @Override public Instancer instancer(InstanceType type, Model model) { - return engine.instancer(type, context, model, renderStage); + return engine.instancer(type, model, renderStage); } @Override public VisualizationContext get() { return new VisualizationContextImpl(this, engine.renderOrigin()); } + + public Embedded embed(EmbeddedLevel world) { + return new Embedded(engine, world, renderStage); + } + + @Override + public String toString() { + return "InstancerProviderImpl[" + "engine=" + engine + ", " + "renderStage=" + renderStage + ']'; + } + + public static final class Embedded extends InstancerProviderImpl { + private final EmbeddedLevel world; + + public Embedded(Engine engine, EmbeddedLevel world, RenderStage renderStage) { + super(engine, renderStage); + this.world = world; + } + + @Override + public Instancer instancer(InstanceType type, Model model) { + return engine.instancer(world, type, model, renderStage); + } + + public EmbeddedLevel world() { + return world; + } + + @Override + public String toString() { + return "InstancerProviderImpl.EmbeddedImpl[" + "world=" + world + ", " + "engine=" + engine + ", " + "renderStage=" + renderStage + ']'; + } + } } diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationContextImpl.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationContextImpl.java index 26809f528..4521a775d 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationContextImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationContextImpl.java @@ -1,7 +1,7 @@ package com.jozufozu.flywheel.impl.visualization; -import com.jozufozu.flywheel.api.context.Context; import com.jozufozu.flywheel.api.instance.InstancerProvider; +import com.jozufozu.flywheel.api.visualization.EmbeddedLevel; import com.jozufozu.flywheel.api.visualization.VisualizationContext; import net.minecraft.core.Vec3i; @@ -15,8 +15,7 @@ import net.minecraft.core.Vec3i; */ public record VisualizationContextImpl(InstancerProviderImpl instancerProvider, Vec3i renderOrigin) implements VisualizationContext { @Override - public VisualizationContext withContext(Context context, Vec3i renderOrigin) { - var provider = new InstancerProviderImpl(instancerProvider.engine(), context, instancerProvider.renderStage()); - return new VisualizationContextImpl(provider, renderOrigin); + public VisualizationContext embed(EmbeddedLevel world) { + return new VisualizationContextImpl(instancerProvider.embed(world), renderOrigin); } } diff --git a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java index 14949c236..e06ec0f8f 100644 --- a/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java +++ b/src/main/java/com/jozufozu/flywheel/impl/visualization/VisualizationManagerImpl.java @@ -36,7 +36,6 @@ import com.jozufozu.flywheel.impl.visualization.manager.VisualManagerImpl; import com.jozufozu.flywheel.impl.visualization.ratelimit.BandedPrimeLimiter; import com.jozufozu.flywheel.impl.visualization.ratelimit.DistanceUpdateLimiterImpl; import com.jozufozu.flywheel.impl.visualization.ratelimit.NonLimiter; -import com.jozufozu.flywheel.lib.context.Contexts; import com.jozufozu.flywheel.lib.task.Flag; import com.jozufozu.flywheel.lib.task.IfElsePlan; import com.jozufozu.flywheel.lib.task.MapContextPlan; @@ -85,9 +84,9 @@ public class VisualizationManagerImpl implements VisualizationManager { .createEngine(level); taskExecutor = FlwTaskExecutor.get(); - var blockEntitiesStorage = new BlockEntityStorage(new InstancerProviderImpl(engine, Contexts.DEFAULT, RenderStage.AFTER_BLOCK_ENTITIES)); - var entitiesStorage = new EntityStorage(new InstancerProviderImpl(engine, Contexts.DEFAULT, RenderStage.AFTER_ENTITIES)); - var effectsStorage = new EffectStorage(new InstancerProviderImpl(engine, Contexts.DEFAULT, RenderStage.AFTER_PARTICLES)); + var blockEntitiesStorage = new BlockEntityStorage(new InstancerProviderImpl(engine, RenderStage.AFTER_BLOCK_ENTITIES)); + var entitiesStorage = new EntityStorage(new InstancerProviderImpl(engine, RenderStage.AFTER_ENTITIES)); + var effectsStorage = new EffectStorage(new InstancerProviderImpl(engine, RenderStage.AFTER_PARTICLES)); blockEntities = new VisualManagerImpl<>(blockEntitiesStorage); entities = new VisualManagerImpl<>(entitiesStorage); diff --git a/src/main/java/com/jozufozu/flywheel/lib/context/ContextShaders.java b/src/main/java/com/jozufozu/flywheel/lib/context/ContextShaders.java deleted file mode 100644 index cd3f69120..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/context/ContextShaders.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jozufozu.flywheel.lib.context; - -import org.jetbrains.annotations.ApiStatus; - -import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.api.context.ContextShader; - -public class ContextShaders { - public static final SimpleContextShader DEFAULT = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("context/default.vert"), Flywheel.rl("context/default.frag"))); - public static final SimpleContextShader CRUMBLING = ContextShader.REGISTRY.registerAndGet(new SimpleContextShader(Flywheel.rl("context/crumbling.vert"), Flywheel.rl("context/crumbling.frag"))); - - private ContextShaders() { - } - - @ApiStatus.Internal - public static void init() { - } -} diff --git a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java b/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java deleted file mode 100644 index 60a227f59..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/context/Contexts.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.jozufozu.flywheel.lib.context; - -import com.jozufozu.flywheel.api.context.Context; - -import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; -import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; -import net.minecraft.client.resources.model.ModelBakery; - -public final class Contexts { - public static final Context DEFAULT = SimpleContext.builder(ContextShaders.DEFAULT) - .build(); - - public static final Int2ObjectMap CRUMBLING; - - static { - var map = new Int2ObjectArrayMap(); - - for (int i = 0; i < ModelBakery.BREAKING_LOCATIONS.size(); i++) { - var crumblingLocation = ModelBakery.BREAKING_LOCATIONS.get(i); - map.put(i, SimpleContext.builder(ContextShaders.CRUMBLING) - .preparation((material, shader, textureSource) -> { - shader.setTexture("crumblingTex", textureSource.byName(crumblingLocation)); - }) - .build()); - } - - CRUMBLING = Int2ObjectMaps.unmodifiable(map); - } - - private Contexts() { - } -} diff --git a/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl b/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl index 8b70b9b44..c8d9a275c 100644 --- a/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl +++ b/src/main/resources/assets/flywheel/flywheel/instance/cull/transformed.glsl @@ -1,14 +1,5 @@ +#include "flywheel:util/matrix.glsl" + void flw_transformBoundingSphere(in FlwInstance i, inout vec3 center, inout float radius) { - mat4 pose = i.pose; - center = (pose * vec4(center, 1.0)).xyz; - - vec3 c0 = pose[0].xyz; - vec3 c1 = pose[1].xyz; - vec3 c2 = pose[2].xyz; - - // Comute the squared maximum to avoid 2 unnecessary sqrts. - // I don't think this makes it any faster but why not /shrug - float scaleSqr = max(dot(c0, c0), max(dot(c1, c1), dot(c2, c2))); - float scale = sqrt(scaleSqr); - radius *= scale; + transformBoundingSphere(i.pose, center, radius); } diff --git a/src/main/resources/assets/flywheel/flywheel/context/crumbling.frag b/src/main/resources/assets/flywheel/flywheel/internal/context/crumbling.frag similarity index 100% rename from src/main/resources/assets/flywheel/flywheel/context/crumbling.frag rename to src/main/resources/assets/flywheel/flywheel/internal/context/crumbling.frag diff --git a/src/main/resources/assets/flywheel/flywheel/context/crumbling.vert b/src/main/resources/assets/flywheel/flywheel/internal/context/crumbling.vert similarity index 100% rename from src/main/resources/assets/flywheel/flywheel/context/crumbling.vert rename to src/main/resources/assets/flywheel/flywheel/internal/context/crumbling.vert diff --git a/src/main/resources/assets/flywheel/flywheel/context/default.frag b/src/main/resources/assets/flywheel/flywheel/internal/context/default.frag similarity index 100% rename from src/main/resources/assets/flywheel/flywheel/context/default.frag rename to src/main/resources/assets/flywheel/flywheel/internal/context/default.frag diff --git a/src/main/resources/assets/flywheel/flywheel/context/default.vert b/src/main/resources/assets/flywheel/flywheel/internal/context/default.vert similarity index 100% rename from src/main/resources/assets/flywheel/flywheel/context/default.vert rename to src/main/resources/assets/flywheel/flywheel/internal/context/default.vert diff --git a/src/main/resources/assets/flywheel/flywheel/internal/context/embedded.frag b/src/main/resources/assets/flywheel/flywheel/internal/context/embedded.frag new file mode 100644 index 000000000..901805508 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/context/embedded.frag @@ -0,0 +1,10 @@ +uniform sampler3D _flw_lightVolume; + +in vec3 _flw_lightVolumeCoord; + +void flw_beginFragment() { + flw_fragLight = max(flw_fragLight, texture(_flw_lightVolume, _flw_lightVolumeCoord).rg); +} + +void flw_endFragment() { +} diff --git a/src/main/resources/assets/flywheel/flywheel/internal/context/embedded.vert b/src/main/resources/assets/flywheel/flywheel/internal/context/embedded.vert new file mode 100644 index 000000000..6b51f4444 --- /dev/null +++ b/src/main/resources/assets/flywheel/flywheel/internal/context/embedded.vert @@ -0,0 +1,16 @@ +uniform vec3 _flw_oneOverLightBoxSize; +uniform vec3 _flw_lightVolumeMin; +uniform mat4 _flw_model; +uniform mat3 _flw_normal; + +out vec3 _flw_lightVolumeCoord; + +void flw_beginVertex() { +} + +void flw_endVertex() { + _flw_lightVolumeCoord = (flw_vertexPos.xyz - _flw_lightVolumeMin) * _flw_oneOverLightBoxSize; + + flw_vertexPos = _flw_model * flw_vertexPos; + flw_vertexNormal = _flw_normal * flw_vertexNormal; +} diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl b/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl index 8b4e46268..283ad293b 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/cull.glsl @@ -2,6 +2,7 @@ #include "flywheel:internal/indirect/model_descriptor.glsl" #include "flywheel:internal/indirect/object.glsl" #include "flywheel:internal/uniforms/frame.glsl" +#include "flywheel:util/matrix.glsl" layout(local_size_x = _FLW_SUBGROUP_SIZE) in; @@ -17,6 +18,9 @@ layout(std430, binding = _FLW_MODEL_BUFFER_BINDING) restrict buffer ModelBuffer ModelDescriptor models[]; }; +uniform mat4 _flw_embeddedModel; +uniform bool _flw_useEmbeddedModel = false; + // Disgustingly vectorized sphere frustum intersection taking advantage of ahead of time packing. // Only uses 6 fmas and some boolean ops. // See also: @@ -41,6 +45,10 @@ bool _flw_isVisible(uint objectIndex, uint modelIndex) { flw_transformBoundingSphere(instance, center, radius); + if (_flw_useEmbeddedModel) { + transformBoundingSphere(_flw_embeddedModel, center, radius); + } + return _flw_testSphere(center, radius); } diff --git a/src/main/resources/assets/flywheel/flywheel/util/matrix.glsl b/src/main/resources/assets/flywheel/flywheel/util/matrix.glsl index 0b5e8eabd..059e933f3 100644 --- a/src/main/resources/assets/flywheel/flywheel/util/matrix.glsl +++ b/src/main/resources/assets/flywheel/flywheel/util/matrix.glsl @@ -41,3 +41,17 @@ mat3 modelToNormal(mat4 mat) { m[2] = mat[2].xyz; return m; } + +void transformBoundingSphere(in mat4 mat, inout vec3 center, inout float radius) { + center = (mat * vec4(center, 1.)).xyz; + + vec3 c0 = mat[0].xyz; + vec3 c1 = mat[1].xyz; + vec3 c2 = mat[2].xyz; + + // Comute the squared maximum to avoid 2 unnecessary sqrts. + // I don't think this makes it any faster but why not /shrug + float scaleSqr = max(dot(c0, c0), max(dot(c1, c1), dot(c2, c2))); + float scale = sqrt(scaleSqr); + radius *= scale; +}