From 9a178ef9d82b8ecfbcbb5031fd4997b60608d0f0 Mon Sep 17 00:00:00 2001 From: JozsefA Date: Wed, 19 May 2021 19:11:06 -0700 Subject: [PATCH] Abstract fog - WorldPrograms no longer accept an explicit fog mode. - They now inherit from a class that takes a list of arbitrary shader extensions --- .../jozufozu/flywheel/backend/Backend.java | 12 +-- .../flywheel/backend/ShaderContext.java | 2 +- .../backend/core/CrumblingProgram.java | 9 ++- .../core/FirstNonnullMultiProgram.java | 6 ++ .../flywheel/backend/core/WorldContext.java | 10 +-- .../WorldMultiProgram.java} | 31 +++---- .../backend/core/WorldTileRenderer.java | 3 +- .../core/shader/ExtensibleGlProgram.java | 62 ++++++++++++++ .../flywheel/backend/core/shader/FogMode.java | 81 +++++++++++++++++++ .../backend/{gl => core/shader}/GlFog.java | 2 +- .../backend/core/shader/GlFogMode.java | 38 +++++++++ .../core/shader/IProgramExtension.java | 17 ++++ .../backend/core/shader/ProgramExtender.java | 18 +++++ .../{gl => core}/shader/ProgramSpec.java | 2 +- .../{gl => core}/shader/SingleProgram.java | 5 +- .../WorldProgram.java} | 18 ++--- .../flywheel/backend/gl/GlFogMode.java | 37 --------- .../backend/gl/shader/ProgramFogMode.java | 58 ------------- .../backend/gl/shader/ShaderSpecLoader.java | 1 + .../instancing/InstancedTileRenderer.java | 4 +- .../backend/instancing/MaterialSpec.java | 2 +- .../backend/instancing/RenderMaterial.java | 4 +- .../render/ContraptionProgram.java | 10 ++- .../render/ContraptionRenderDispatcher.java | 6 +- .../simibubi/create/events/ClientEvents.java | 4 +- .../mixin/flywheel/FogColorTrackerMixin.java | 2 +- .../mixin/flywheel/TileWorldHookMixin.java | 4 +- .../foundation/render/AllProgramSpecs.java | 2 +- .../render/effects/EffectsContext.java | 2 +- 29 files changed, 292 insertions(+), 160 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/core/FirstNonnullMultiProgram.java rename src/main/java/com/jozufozu/flywheel/backend/{gl/shader/FogSensitiveProgram.java => core/WorldMultiProgram.java} (56%) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/core/shader/ExtensibleGlProgram.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/core/shader/FogMode.java rename src/main/java/com/jozufozu/flywheel/backend/{gl => core/shader}/GlFog.java (95%) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFogMode.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/core/shader/IProgramExtension.java create mode 100644 src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramExtender.java rename src/main/java/com/jozufozu/flywheel/backend/{gl => core}/shader/ProgramSpec.java (90%) rename src/main/java/com/jozufozu/flywheel/backend/{gl => core}/shader/SingleProgram.java (80%) rename src/main/java/com/jozufozu/flywheel/backend/core/{BasicProgram.java => shader/WorldProgram.java} (75%) delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/gl/GlFogMode.java delete mode 100644 src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramFogMode.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index 62a8c0cd7..a704b8273 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -20,12 +20,12 @@ import org.apache.logging.log4j.Logger; import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GLCapabilities; -import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.core.CrumblingRenderer; import com.jozufozu.flywheel.backend.core.WorldContext; import com.jozufozu.flywheel.backend.core.WorldTileRenderer; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; import com.jozufozu.flywheel.backend.instancing.IFlywheelWorld; import com.jozufozu.flywheel.backend.instancing.InstanceData; @@ -64,7 +64,7 @@ public class Backend { public static GLCapabilities capabilities; public static GlCompat compat; - public static WorldAttached> tileRenderer = new WorldAttached<>(() -> new WorldTileRenderer<>(WorldContext.INSTANCE)); + public static WorldAttached> tileRenderer = new WorldAttached<>(() -> new WorldTileRenderer<>(WorldContext.INSTANCE)); public static LazyValue> blockBreaking = new LazyValue<>(() -> { Vector renderers = new Vector<>(10); for (int i = 0; i < 10; i++) { @@ -87,7 +87,7 @@ public class Backend { listeners.refreshListener(world -> { if (canUseInstancing() && world != null) { - WorldTileRenderer tileRenderer = Backend.tileRenderer.get(world); + WorldTileRenderer tileRenderer = Backend.tileRenderer.get(world); tileRenderer.invalidate(); world.loadedTileEntityList.forEach(tileRenderer::add); } @@ -190,7 +190,7 @@ public class Backend { Minecraft mc = Minecraft.getInstance(); ClientWorld world = mc.world; - WorldTileRenderer instancer = tileRenderer.get(world); + WorldTileRenderer instancer = tileRenderer.get(world); Entity renderViewEntity = mc.renderViewEntity; instancer.tick(renderViewEntity.getX(), renderViewEntity.getY(), renderViewEntity.getZ()); @@ -198,7 +198,7 @@ public class Backend { public static void renderLayer(ClientWorld world, RenderType layer, Matrix4f viewProjection, double cameraX, double cameraY, double cameraZ) { if (!canUseInstancing(world)) return; - WorldTileRenderer renderer = tileRenderer.get(world); + WorldTileRenderer renderer = tileRenderer.get(world); layer.startDrawing(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java index 70f774155..89ab4e48f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java @@ -4,9 +4,9 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import com.jozufozu.flywheel.backend.gl.shader.IMultiProgram; -import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; import com.jozufozu.flywheel.backend.gl.shader.ShaderSpecLoader; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; import com.jozufozu.flywheel.backend.loading.Program; diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/CrumblingProgram.java b/src/main/java/com/jozufozu/flywheel/backend/core/CrumblingProgram.java index 417ae135c..2c1606813 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/core/CrumblingProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/CrumblingProgram.java @@ -2,14 +2,17 @@ package com.jozufozu.flywheel.backend.core; import static org.lwjgl.opengl.GL20.glUniform2f; -import com.jozufozu.flywheel.backend.gl.shader.ProgramFogMode; +import java.util.List; + +import com.jozufozu.flywheel.backend.core.shader.ProgramExtender; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.loading.Program; -public class CrumblingProgram extends BasicProgram { +public class CrumblingProgram extends WorldProgram { protected final int uTextureScale; protected int uCrumbling; - public CrumblingProgram(Program program, ProgramFogMode.Factory fogFactory) { + public CrumblingProgram(Program program, List fogFactory) { super(program, fogFactory); uTextureScale = getUniformLocation("uTextureScale"); diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/FirstNonnullMultiProgram.java b/src/main/java/com/jozufozu/flywheel/backend/core/FirstNonnullMultiProgram.java new file mode 100644 index 000000000..1e7b441dd --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/FirstNonnullMultiProgram.java @@ -0,0 +1,6 @@ +package com.jozufozu.flywheel.backend.core; + +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; + +public class FirstNonnullMultiProgram

{ +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/WorldContext.java b/src/main/java/com/jozufozu/flywheel/backend/core/WorldContext.java index f3b1dada0..a68515382 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/core/WorldContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/WorldContext.java @@ -12,9 +12,9 @@ import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.ResourceUtil; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderLoader; -import com.jozufozu.flywheel.backend.gl.shader.FogSensitiveProgram; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.gl.shader.IMultiProgram; -import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; import com.jozufozu.flywheel.backend.gl.shader.ShaderSpecLoader; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; import com.jozufozu.flywheel.backend.instancing.MaterialSpec; @@ -27,13 +27,13 @@ import com.jozufozu.flywheel.backend.loading.ShaderTransformer; import net.minecraft.util.ResourceLocation; -public class WorldContext

extends ShaderContext

{ +public class WorldContext

extends ShaderContext

{ private static final String declaration = "#flwbuiltins"; private static final Pattern builtinPattern = Pattern.compile(declaration); - public static final WorldContext INSTANCE = new WorldContext<>(new ResourceLocation(Flywheel.ID, "context/world"), new FogSensitiveProgram.SpecLoader<>(BasicProgram::new)); - public static final WorldContext CRUMBLING = new WorldContext<>(new ResourceLocation(Flywheel.ID, "context/crumbling"), new FogSensitiveProgram.SpecLoader<>(CrumblingProgram::new)); + public static final WorldContext INSTANCE = new WorldContext<>(new ResourceLocation(Flywheel.ID, "context/world"), new WorldMultiProgram.SpecLoader<>(WorldProgram::new)); + public static final WorldContext CRUMBLING = new WorldContext<>(new ResourceLocation(Flywheel.ID, "context/crumbling"), new WorldMultiProgram.SpecLoader<>(CrumblingProgram::new)); protected ProgramTemplate template; protected final ResourceLocation name; diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/FogSensitiveProgram.java b/src/main/java/com/jozufozu/flywheel/backend/core/WorldMultiProgram.java similarity index 56% rename from src/main/java/com/jozufozu/flywheel/backend/gl/shader/FogSensitiveProgram.java rename to src/main/java/com/jozufozu/flywheel/backend/core/WorldMultiProgram.java index f54e699cc..dfc822a9a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/FogSensitiveProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/WorldMultiProgram.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.gl.shader; +package com.jozufozu.flywheel.backend.core; import java.util.ArrayList; import java.util.Collections; @@ -8,16 +8,21 @@ import java.util.Map; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderLoader; -import com.jozufozu.flywheel.backend.gl.GlFog; -import com.jozufozu.flywheel.backend.gl.GlFogMode; +import com.jozufozu.flywheel.backend.core.shader.ExtensibleGlProgram; +import com.jozufozu.flywheel.backend.core.shader.GlFog; +import com.jozufozu.flywheel.backend.core.shader.GlFogMode; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; +import com.jozufozu.flywheel.backend.gl.shader.IMultiProgram; +import com.jozufozu.flywheel.backend.gl.shader.ShaderSpecLoader; import com.jozufozu.flywheel.backend.loading.Program; -public class FogSensitiveProgram

implements IMultiProgram

{ +public class WorldMultiProgram

implements IMultiProgram

{ private final Map programs; private final List

debugPrograms; - public FogSensitiveProgram(Map programs, List

debugPrograms) { + public WorldMultiProgram(Map programs, List

debugPrograms) { this.programs = programs; this.debugPrograms = debugPrograms; } @@ -34,10 +39,10 @@ public class FogSensitiveProgram

implements IMultiProgram

implements ShaderSpecLoader

{ - private final FogProgramLoader

fogProgramLoader; + private final ExtensibleGlProgram.Factory

factory; - public SpecLoader(FogProgramLoader

fogProgramLoader) { - this.fogProgramLoader = fogProgramLoader; + public SpecLoader(ExtensibleGlProgram.Factory

factory) { + this.factory = factory; } @Override @@ -49,7 +54,7 @@ public class FogSensitiveProgram

implements IMultiProgram

programs = new EnumMap<>(GlFogMode.class); @@ -57,16 +62,12 @@ public class FogSensitiveProgram

implements IMultiProgram

(programs, debugModes); + return new WorldMultiProgram<>(programs, debugModes); } } - public interface FogProgramLoader

{ - - P create(Program program, ProgramFogMode.Factory fogFactory); - } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/WorldTileRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/core/WorldTileRenderer.java index 3ad0efec9..1e04b9edc 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/core/WorldTileRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/WorldTileRenderer.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.core; import java.util.ArrayList; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.gl.shader.ShaderCallback; import com.jozufozu.flywheel.backend.instancing.InstancedTileRenderer; @@ -12,7 +13,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.vector.Matrix4f; -public class WorldTileRenderer

extends InstancedTileRenderer

{ +public class WorldTileRenderer

extends InstancedTileRenderer

{ public static int MAX_ORIGIN_DISTANCE = 100; public BlockPos originCoordinate = BlockPos.ZERO; diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/shader/ExtensibleGlProgram.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/ExtensibleGlProgram.java new file mode 100644 index 000000000..f2e6423a0 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/ExtensibleGlProgram.java @@ -0,0 +1,62 @@ +package com.jozufozu.flywheel.backend.core.shader; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; +import com.jozufozu.flywheel.backend.loading.Program; + +/** + * A shader program that be arbitrarily "extended". This class can take in any number of program extensions, and + * will initialize them and then call their {@link IProgramExtension#bind() bind} function every subsequent time this + * program is bound. An "extension" is something that interacts with the shader program in a way that is invisible to + * the caller using the program. This is used by some programs to implement the different fog modes. Other uses might + * include binding extra textures to allow for blocks to have normal maps, for example. As the extensions are + * per-program, this also allows for same extra specialization within a + * {@link com.jozufozu.flywheel.backend.ShaderContext ShaderContext}. + */ +public class ExtensibleGlProgram extends GlProgram { + + protected final List extensions; + + public ExtensibleGlProgram(Program program, @Nullable List extensions) { + super(program); + + if (extensions != null) { + this.extensions = extensions.stream() + .map(e -> e.create(this)) + .collect(Collectors.toList()); + } else { + this.extensions = Collections.emptyList(); + } + } + + @Override + public void bind() { + super.bind(); + + extensions.forEach(IProgramExtension::bind); + } + + @Override + public String toString() { + return "ExtensibleGlProgram{" + + "name=" + name + + ", extensions=" + extensions + + '}'; + } + + /** + * A factory interface to create {@link GlProgram}s parameterized by a list of extensions. This doesn't necessarily + * have to return an {@link ExtensibleGlProgram} if implementors want more flexibility for whatever reason. + */ + public interface Factory

{ + + @Nonnull + P create(Program program, @Nullable List fogFactory); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/shader/FogMode.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/FogMode.java new file mode 100644 index 000000000..5e88019d2 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/FogMode.java @@ -0,0 +1,81 @@ +package com.jozufozu.flywheel.backend.core.shader; + +import org.lwjgl.opengl.GL20; + +import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; + +import net.minecraft.util.ResourceLocation; + +public abstract class FogMode { + + public static class None implements IProgramExtension { + + public static final ResourceLocation NAME = new ResourceLocation(Flywheel.ID, "fog_none"); + + public None(GlProgram program) { + + } + + @Override + public void bind() { + + } + + @Override + public ResourceLocation name() { + return NAME; + } + } + + public static class Linear implements IProgramExtension { + + public static final ResourceLocation NAME = new ResourceLocation(Flywheel.ID, "fog_linear"); + + private final int uFogColor; + private final int uFogRange; + + public Linear(GlProgram program) { + this.uFogColor = program.getUniformLocation("uFogColor"); + this.uFogRange = program.getUniformLocation("uFogRange"); + } + + @Override + public void bind() { + GL20.glUniform2f(uFogRange, GlFog.getFogStart(), GlFog.getFogEnd()); + GL20.glUniform4fv(uFogColor, GlFog.FOG_COLOR); + } + + @Override + public ResourceLocation name() { + return NAME; + } + } + + public static class Exp2 implements IProgramExtension { + + public static final ResourceLocation NAME = new ResourceLocation(Flywheel.ID, "fog_exp2"); + + private final int uFogColor; + private final int uFogDensity; + + public Exp2(GlProgram program) { + this.uFogColor = program.getUniformLocation("uFogColor"); + this.uFogDensity = program.getUniformLocation("uFogDensity"); + } + + @Override + public void bind() { + GL20.glUniform1f(uFogDensity, GlFog.getFogDensity()); + GL20.glUniform4fv(uFogColor, GlFog.FOG_COLOR); + } + + @Override + public ResourceLocation name() { + return NAME; + } + } + + public interface Factory extends IProgramExtension { + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlFog.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFog.java similarity index 95% rename from src/main/java/com/jozufozu/flywheel/backend/gl/GlFog.java rename to src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFog.java index 2ad61fcf2..100c1a71d 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlFog.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFog.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.gl; +package com.jozufozu.flywheel.backend.core.shader; import org.lwjgl.opengl.GL11; diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFogMode.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFogMode.java new file mode 100644 index 000000000..57f1acea6 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/GlFogMode.java @@ -0,0 +1,38 @@ +package com.jozufozu.flywheel.backend.core.shader; + +import java.util.Collections; +import java.util.List; + +import com.google.common.collect.Lists; +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; + +public enum GlFogMode implements ProgramExtender { + NONE(FogMode.None::new), + LINEAR(FogMode.Linear::new, "USE_FOG_LINEAR"), + EXP2(FogMode.Exp2::new, "USE_FOG_EXP2"), + ; + + public static final String USE_FOG = "USE_FOG"; + + private final ProgramExtender fogFactory; + private final List defines; + + GlFogMode(ProgramExtender fogFactory) { + this.fogFactory = fogFactory; + this.defines = Collections.emptyList(); + } + + GlFogMode(ProgramExtender fogFactory, String name) { + this.fogFactory = fogFactory; + this.defines = Lists.newArrayList(USE_FOG, name); + } + + public List getDefines() { + return defines; + } + + @Override + public IProgramExtension create(GlProgram program) { + return fogFactory.create(program); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/shader/IProgramExtension.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/IProgramExtension.java new file mode 100644 index 000000000..4e825fa5c --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/IProgramExtension.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.backend.core.shader; + +import net.minecraft.util.ResourceLocation; + +/** + * A program extension to be passed to + */ +public interface IProgramExtension { + + /** + * Bind the extra program state. It is recommended to grab the state information from global variables, + * or local variables passed through a {@link ProgramExtender} closure. + */ + void bind(); + + ResourceLocation name(); +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramExtender.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramExtender.java new file mode 100644 index 000000000..b187fa7d2 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramExtender.java @@ -0,0 +1,18 @@ +package com.jozufozu.flywheel.backend.core.shader; + +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; + +/** + * A factory interface for creating {@link IProgramExtension}s. These are what end up being passed in + * during shader program construction. + */ +public interface ProgramExtender { + + /** + * Construct the extension, binding any necessary information using the provided {@link GlProgram}. + * + * @param program The program being extended. + * @return An extension object, possibly initialized using the program. + */ + IProgramExtension create(GlProgram program); +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramSpec.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramSpec.java similarity index 90% rename from src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramSpec.java rename to src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramSpec.java index a7f686fff..6474c008c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramSpec.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/ProgramSpec.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.backend.gl.shader; +package com.jozufozu.flywheel.backend.core.shader; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/SingleProgram.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/SingleProgram.java similarity index 80% rename from src/main/java/com/jozufozu/flywheel/backend/gl/shader/SingleProgram.java rename to src/main/java/com/jozufozu/flywheel/backend/core/shader/SingleProgram.java index d66f10bb6..f4da285f3 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/SingleProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/SingleProgram.java @@ -1,7 +1,10 @@ -package com.jozufozu.flywheel.backend.gl.shader; +package com.jozufozu.flywheel.backend.core.shader; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderLoader; +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; +import com.jozufozu.flywheel.backend.gl.shader.IMultiProgram; +import com.jozufozu.flywheel.backend.gl.shader.ShaderSpecLoader; import com.jozufozu.flywheel.backend.loading.Program; public class SingleProgram

implements IMultiProgram

{ diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/BasicProgram.java b/src/main/java/com/jozufozu/flywheel/backend/core/shader/WorldProgram.java similarity index 75% rename from src/main/java/com/jozufozu/flywheel/backend/core/BasicProgram.java rename to src/main/java/com/jozufozu/flywheel/backend/core/shader/WorldProgram.java index cae17d565..749be410b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/core/BasicProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/shader/WorldProgram.java @@ -1,33 +1,29 @@ -package com.jozufozu.flywheel.backend.core; +package com.jozufozu.flywheel.backend.core.shader; import static org.lwjgl.opengl.GL20.glUniform1f; import static org.lwjgl.opengl.GL20.glUniform3f; -import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.jozufozu.flywheel.backend.gl.shader.ProgramFogMode; +import java.util.List; + import com.jozufozu.flywheel.backend.loading.Program; import com.simibubi.create.foundation.utility.AnimationTickHolder; import net.minecraft.util.math.vector.Matrix4f; -public class BasicProgram extends GlProgram { +public class WorldProgram extends ExtensibleGlProgram { protected final int uTime; protected final int uViewProjection; protected final int uCameraPos; - protected final ProgramFogMode fogMode; - protected int uBlockAtlas; protected int uLightMap; - public BasicProgram(Program program, ProgramFogMode.Factory fogFactory) { - super(program); + public WorldProgram(Program program, List fogFactory) { + super(program, fogFactory); uTime = getUniformLocation("uTime"); uViewProjection = getUniformLocation("uViewProjection"); uCameraPos = getUniformLocation("uCameraPos"); - fogMode = fogFactory.create(this); - bind(); registerSamplers(); unbind(); @@ -55,7 +51,5 @@ public class BasicProgram extends GlProgram { super.bind(); uploadTime(AnimationTickHolder.getRenderTime()); - - fogMode.bind(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlFogMode.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlFogMode.java deleted file mode 100644 index ebb2354bb..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlFogMode.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.jozufozu.flywheel.backend.gl; - -import java.util.Collections; -import java.util.List; - -import com.google.common.collect.Lists; -import com.jozufozu.flywheel.backend.gl.shader.ProgramFogMode; - -public enum GlFogMode { - NONE(ProgramFogMode.None::new), - LINEAR(ProgramFogMode.Linear::new, "USE_FOG_LINEAR"), - EXP2(ProgramFogMode.Exp2::new, "USE_FOG_EXP2"), - ; - - public static final String USE_FOG = "USE_FOG"; - - private final ProgramFogMode.Factory fogFactory; - private final List defines; - - GlFogMode(ProgramFogMode.Factory fogFactory) { - this.fogFactory = fogFactory; - this.defines = Collections.emptyList(); - } - - GlFogMode(ProgramFogMode.Factory fogFactory, String name) { - this.fogFactory = fogFactory; - this.defines = Lists.newArrayList(USE_FOG, name); - } - - public List getDefines() { - return defines; - } - - public ProgramFogMode.Factory getFogFactory() { - return fogFactory; - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramFogMode.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramFogMode.java deleted file mode 100644 index ec1048f9b..000000000 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ProgramFogMode.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.jozufozu.flywheel.backend.gl.shader; - -import org.lwjgl.opengl.GL20; - -import com.jozufozu.flywheel.backend.gl.GlFog; - -public abstract class ProgramFogMode { - - public abstract void bind(); - - public static class None extends ProgramFogMode { - - public None(GlProgram program) { - - } - - @Override - public void bind() { - - } - } - - public static class Linear extends ProgramFogMode { - private final int uFogColor; - private final int uFogRange; - - public Linear(GlProgram program) { - this.uFogColor = program.getUniformLocation("uFogColor"); - this.uFogRange = program.getUniformLocation("uFogRange"); - } - - @Override - public void bind() { - GL20.glUniform2f(uFogRange, GlFog.getFogStart(), GlFog.getFogEnd()); - GL20.glUniform4fv(uFogColor, GlFog.FOG_COLOR); - } - } - - public static class Exp2 extends ProgramFogMode { - private final int uFogColor; - private final int uFogDensity; - - public Exp2(GlProgram program) { - this.uFogColor = program.getUniformLocation("uFogColor"); - this.uFogDensity = program.getUniformLocation("uFogDensity"); - } - - @Override - public void bind() { - GL20.glUniform1f(uFogDensity, GlFog.getFogDensity()); - GL20.glUniform4fv(uFogColor, GlFog.FOG_COLOR); - } - } - - public interface Factory { - ProgramFogMode create(GlProgram program); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderSpecLoader.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderSpecLoader.java index f050ac981..1fafd1346 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderSpecLoader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/ShaderSpecLoader.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.gl.shader; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderLoader; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; public interface ShaderSpecLoader

{ diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedTileRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedTileRenderer.java index 04a3646fc..69f81f7a1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedTileRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedTileRenderer.java @@ -8,10 +8,10 @@ import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.core.WorldContext; import com.jozufozu.flywheel.backend.core.materials.ModelData; import com.jozufozu.flywheel.backend.core.materials.OrientedData; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.gl.shader.ShaderCallback; import com.simibubi.create.foundation.render.AllMaterialSpecs; @@ -25,7 +25,7 @@ import net.minecraft.util.math.vector.Vector3f; import net.minecraft.world.IBlockReader; import net.minecraft.world.World; -public abstract class InstancedTileRenderer

{ +public abstract class InstancedTileRenderer

{ public final WorldContext

context; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java index 2cfcfa435..fc30978b2 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java @@ -1,7 +1,7 @@ package com.jozufozu.flywheel.backend.instancing; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; -import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; import net.minecraft.util.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/RenderMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/RenderMaterial.java index c5a1b4e11..209516f56 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/RenderMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/RenderMaterial.java @@ -12,8 +12,8 @@ import org.lwjgl.opengl.GL11; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.core.PartialModel; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.shader.ShaderCallback; import com.jozufozu.flywheel.util.BufferBuilderReader; @@ -34,7 +34,7 @@ import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.vector.Matrix4f; -public class RenderMaterial

{ +public class RenderMaterial

{ protected final InstancedTileRenderer

renderer; protected final Cache> models; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java index b5952789a..203b5c0fa 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionProgram.java @@ -1,22 +1,24 @@ package com.simibubi.create.content.contraptions.components.structureMovement.render; +import java.util.List; + import org.lwjgl.opengl.GL20; -import com.jozufozu.flywheel.backend.core.BasicProgram; -import com.jozufozu.flywheel.backend.gl.shader.ProgramFogMode; +import com.jozufozu.flywheel.backend.core.shader.ProgramExtender; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.jozufozu.flywheel.backend.loading.Program; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.vector.Matrix4f; -public class ContraptionProgram extends BasicProgram { +public class ContraptionProgram extends WorldProgram { protected final int uLightBoxSize; protected final int uLightBoxMin; protected final int uModel; protected int uLightVolume; - public ContraptionProgram(Program program, ProgramFogMode.Factory fogFactory) { + public ContraptionProgram(Program program, List fogFactory) { super(program, fogFactory); uLightBoxSize = getUniformLocation("uLightBoxSize"); uLightBoxMin = getUniformLocation("uLightBoxMin"); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java index f8a2e942e..8add8c3b1 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/ContraptionRenderDispatcher.java @@ -16,7 +16,7 @@ import org.apache.commons.lang3.tuple.Pair; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.core.WorldContext; -import com.jozufozu.flywheel.backend.gl.shader.FogSensitiveProgram; +import com.jozufozu.flywheel.backend.core.WorldMultiProgram; import com.jozufozu.flywheel.backend.loading.ModelTemplate; import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllMovementBehaviours; @@ -71,8 +71,8 @@ public class ContraptionRenderDispatcher { public static final Compartment> CONTRAPTION = new Compartment<>(); private static final ResourceLocation ctxRoot = new ResourceLocation("create", "context/contraption"); - public static final WorldContext STRUCTURE = new WorldContext<>(ctxRoot, new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new), () -> Stream.of(AllProgramSpecs.STRUCTURE), ModelTemplate::new); - public static final WorldContext TILES = new WorldContext<>(ctxRoot, new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)); + public static final WorldContext STRUCTURE = new WorldContext<>(ctxRoot, new WorldMultiProgram.SpecLoader<>(ContraptionProgram::new), () -> Stream.of(AllProgramSpecs.STRUCTURE), ModelTemplate::new); + public static final WorldContext TILES = new WorldContext<>(ctxRoot, new WorldMultiProgram.SpecLoader<>(ContraptionProgram::new)); public static void tick() { if (Minecraft.getInstance().isGamePaused()) return; diff --git a/src/main/java/com/simibubi/create/events/ClientEvents.java b/src/main/java/com/simibubi/create/events/ClientEvents.java index f42d9431e..4465a05e8 100644 --- a/src/main/java/com/simibubi/create/events/ClientEvents.java +++ b/src/main/java/com/simibubi/create/events/ClientEvents.java @@ -5,8 +5,8 @@ import java.util.List; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.RenderWork; -import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.core.WorldTileRenderer; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.systems.RenderSystem; import com.simibubi.create.AllFluids; @@ -145,7 +145,7 @@ public class ClientEvents { if (world.isRemote() && world instanceof ClientWorld && !(world instanceof WrappedClientWorld)) { CreateClient.invalidateRenderers(world); AnimationTickHolder.reset(); - WorldTileRenderer renderer = Backend.tileRenderer.get(world); + WorldTileRenderer renderer = Backend.tileRenderer.get(world); renderer.invalidate(); ((ClientWorld) world).loadedTileEntityList.forEach(renderer::add); } diff --git a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/FogColorTrackerMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/FogColorTrackerMixin.java index 87682d8f4..508caedc2 100644 --- a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/FogColorTrackerMixin.java +++ b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/FogColorTrackerMixin.java @@ -6,7 +6,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.backend.gl.GlFog; +import com.jozufozu.flywheel.backend.core.shader.GlFog; import com.mojang.blaze3d.platform.GlStateManager; @Mixin(GlStateManager.class) diff --git a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/TileWorldHookMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/TileWorldHookMixin.java index fdf4a4705..246127c0a 100644 --- a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/TileWorldHookMixin.java +++ b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/TileWorldHookMixin.java @@ -11,8 +11,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.core.WorldTileRenderer; +import com.jozufozu.flywheel.backend.core.shader.WorldProgram; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; @@ -47,7 +47,7 @@ public class TileWorldHookMixin { @Inject(at = @At(value = "INVOKE", target = "Ljava/util/Set;clear()V", ordinal = 0), method = "tickBlockEntities") private void onChunkUnload(CallbackInfo ci) { if (isRemote) { - WorldTileRenderer kineticRenderer = Backend.tileRenderer.get(self); + WorldTileRenderer kineticRenderer = Backend.tileRenderer.get(self); for (TileEntity tile : tileEntitiesToBeRemoved) { kineticRenderer.remove(tile); } diff --git a/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java b/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java index a08553039..c6d50eebd 100644 --- a/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java +++ b/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java @@ -3,7 +3,7 @@ package com.simibubi.create.foundation.render; import static com.jozufozu.flywheel.backend.Backend.register; import com.jozufozu.flywheel.Flywheel; -import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; +import com.jozufozu.flywheel.backend.core.shader.ProgramSpec; import com.simibubi.create.Create; import net.minecraft.util.ResourceLocation; diff --git a/src/main/java/com/simibubi/create/foundation/render/effects/EffectsContext.java b/src/main/java/com/simibubi/create/foundation/render/effects/EffectsContext.java index 5771e712e..0f13aa9e8 100644 --- a/src/main/java/com/simibubi/create/foundation/render/effects/EffectsContext.java +++ b/src/main/java/com/simibubi/create/foundation/render/effects/EffectsContext.java @@ -2,7 +2,7 @@ package com.simibubi.create.foundation.render.effects; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderLoader; -import com.jozufozu.flywheel.backend.gl.shader.SingleProgram; +import com.jozufozu.flywheel.backend.core.shader.SingleProgram; import com.jozufozu.flywheel.backend.loading.ShaderTransformer; import com.simibubi.create.foundation.render.AllProgramSpecs;