From b6f13aa7ff1d25d23c06831cfe0281f5fea4d1ad Mon Sep 17 00:00:00 2001 From: JozsefA Date: Tue, 4 May 2021 16:22:27 -0700 Subject: [PATCH] More granularity for shader context loading --- .../backend/FastRenderDispatcher.java | 16 ++--- .../flywheel/backend/ShaderContext.java | 9 +-- .../backend/core/ContraptionContext.java | 7 +++ .../flywheel/backend/core/WorldContext.java | 61 +++++++------------ ...MaterialFactory.java => MaterialSpec.java} | 16 +++-- .../render/ContraptionRenderDispatcher.java | 2 +- .../foundation/render/AllProgramSpecs.java | 4 +- 7 files changed, 54 insertions(+), 61 deletions(-) rename src/main/java/com/jozufozu/flywheel/backend/instancing/{MaterialFactory.java => MaterialSpec.java} (55%) diff --git a/src/main/java/com/jozufozu/flywheel/backend/FastRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/FastRenderDispatcher.java index cc796f7f0..4f936d889 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/FastRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/FastRenderDispatcher.java @@ -7,7 +7,7 @@ import java.util.concurrent.ConcurrentHashMap; import com.jozufozu.flywheel.backend.core.BasicInstancedTileRenderer; import com.jozufozu.flywheel.backend.core.OrientedModel; import com.jozufozu.flywheel.backend.core.TransformedModel; -import com.jozufozu.flywheel.backend.instancing.MaterialFactory; +import com.jozufozu.flywheel.backend.instancing.MaterialSpec; import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.KineticDebugger; import com.simibubi.create.content.contraptions.base.KineticRenderMaterials; @@ -28,7 +28,7 @@ import net.minecraft.world.World; public class FastRenderDispatcher { - public static Map, MaterialFactory> materials = new HashMap<>(); + public static Map, MaterialSpec> materials = new HashMap<>(); static { registerMaterials(); @@ -37,12 +37,12 @@ public class FastRenderDispatcher { public static WorldAttached> queuedUpdates = new WorldAttached<>(ConcurrentHashMap::newKeySet); public static void registerMaterials() { - materials.put(MaterialTypes.TRANSFORMED, new MaterialFactory(AllProgramSpecs.MODEL, TransformedModel::new)); - materials.put(MaterialTypes.ORIENTED, new MaterialFactory(AllProgramSpecs.ORIENTED, OrientedModel::new)); - materials.put(KineticRenderMaterials.BELTS, new MaterialFactory(AllProgramSpecs.BELT, BeltInstancedModel::new)); - materials.put(KineticRenderMaterials.ROTATING, new MaterialFactory(AllProgramSpecs.ROTATING, RotatingModel::new)); - materials.put(KineticRenderMaterials.FLAPS, new MaterialFactory(AllProgramSpecs.FLAPS, FlapModel::new)); - materials.put(KineticRenderMaterials.ACTORS, new MaterialFactory(AllProgramSpecs.C_ACTOR, ActorModel::new)); + materials.put(MaterialTypes.TRANSFORMED, new MaterialSpec(AllProgramSpecs.MODEL, TransformedModel::new)); + materials.put(MaterialTypes.ORIENTED, new MaterialSpec(AllProgramSpecs.ORIENTED, OrientedModel::new)); + materials.put(KineticRenderMaterials.BELTS, new MaterialSpec(AllProgramSpecs.BELT, BeltInstancedModel::new)); + materials.put(KineticRenderMaterials.ROTATING, new MaterialSpec(AllProgramSpecs.ROTATING, RotatingModel::new)); + materials.put(KineticRenderMaterials.FLAPS, new MaterialSpec(AllProgramSpecs.FLAPS, FlapModel::new)); + materials.put(KineticRenderMaterials.ACTORS, new MaterialSpec(AllProgramSpecs.ACTOR, ActorModel::new)); } public static void enqueueUpdate(TileEntity te) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java index b416318a9..c72614242 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java @@ -23,14 +23,7 @@ public abstract class ShaderContext

{ public abstract ShaderSpecLoader

getLoader(); - public void load(ShaderLoader loader) { - programs.values().forEach(IMultiProgram::delete); - programs.clear(); - - for (ProgramSpec programSpec : Backend.specRegistry.values()) { - loadProgramFromSpec(loader, programSpec); - } - } + public abstract void load(ShaderLoader loader); public void loadProgramFromSpec(ShaderLoader loader, ProgramSpec programSpec) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/core/ContraptionContext.java b/src/main/java/com/jozufozu/flywheel/backend/core/ContraptionContext.java index e90310d62..c08f37516 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/core/ContraptionContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/ContraptionContext.java @@ -1,7 +1,9 @@ package com.jozufozu.flywheel.backend.core; +import com.jozufozu.flywheel.backend.ShaderLoader; import com.jozufozu.flywheel.backend.gl.shader.FogSensitiveProgram; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionProgram; +import com.simibubi.create.foundation.render.AllProgramSpecs; import net.minecraft.util.ResourceLocation; @@ -13,5 +15,10 @@ public class ContraptionContext extends WorldContext { super(new ResourceLocation("create", "contraption"), new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)); } + @Override + public void load(ShaderLoader loader) { + super.load(loader); + loadProgramFromSpec(loader, AllProgramSpecs.STRUCTURE); + } } 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 74d95aa7a..1d354bf00 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/core/WorldContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/core/WorldContext.java @@ -1,16 +1,18 @@ package com.jozufozu.flywheel.backend.core; -import java.util.regex.Matcher; +import java.util.EnumMap; +import java.util.Map; import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import com.jozufozu.flywheel.backend.FastRenderDispatcher; 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.gl.shader.IMultiProgram; import com.jozufozu.flywheel.backend.gl.shader.ShaderSpecLoader; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.backend.instancing.MaterialSpec; import net.minecraft.util.ResourceLocation; @@ -22,51 +24,34 @@ public class WorldContext

extends ShaderContext

{ private final ShaderSpecLoader

loader; - public final ResourceLocation frag; - public final ResourceLocation vert; + final Map builtins; public WorldContext(ResourceLocation root, ShaderSpecLoader

loader) { super(root); - this.frag = ResourceUtil.subPath(root, "/builtin.frag"); - this.vert = ResourceUtil.subPath(root, "/builtin.vert"); + builtins = new EnumMap<>(ShaderType.class); + builtins.put(ShaderType.FRAGMENT, ResourceUtil.subPath(root, "/builtin.frag")); + builtins.put(ShaderType.VERTEX, ResourceUtil.subPath(root, "/builtin.vert")); this.loader = loader; } + @Override + public void load(ShaderLoader loader) { + programs.values().forEach(IMultiProgram::delete); + programs.clear(); + + FastRenderDispatcher.materials.values() + .stream() + .map(MaterialSpec::getProgramSpec) + .forEach(spec -> loadProgramFromSpec(loader, spec)); + } + @Override public String preProcess(ShaderLoader loader, String shaderSrc, ShaderType type) { - return ShaderLoader.lines(shaderSrc).flatMap(line -> { - Matcher matcher = builtinPattern.matcher(line); + String builtinSrc = loader.getShaderSource(builtins.get(type)); - if (matcher.find()) { - ResourceLocation builtins; - - switch (type) { - case FRAGMENT: - builtins = frag; - break; - case VERTEX: - builtins = vert; - break; - default: - builtins = null; - } - - String includeSource = loader.getShaderSource(builtins); - - return ShaderLoader.lines(includeSource); - } - - return Stream.of(line); - }).collect(Collectors.joining("\n")); - } - - public ResourceLocation getFrag() { - return frag; - } - - public ResourceLocation getVert() { - return vert; + return builtinPattern.matcher(shaderSrc) + .replaceFirst(builtinSrc); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialFactory.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java similarity index 55% rename from src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialFactory.java rename to src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java index ab6e4cd47..17f357531 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialFactory.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialSpec.java @@ -3,16 +3,24 @@ package com.jozufozu.flywheel.backend.instancing; import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; -public class MaterialFactory { +public class MaterialSpec { - ProgramSpec programSpec; - ModelFactory modelFactory; + private final ProgramSpec programSpec; + private final ModelFactory modelFactory; - public MaterialFactory(ProgramSpec programSpec, ModelFactory modelFactory) { + public MaterialSpec(ProgramSpec programSpec, ModelFactory modelFactory) { this.programSpec = programSpec; this.modelFactory = modelFactory; } + public ProgramSpec getProgramSpec() { + return programSpec; + } + + public ModelFactory getModelFactory() { + return modelFactory; + } + public

RenderMaterial create(InstancedTileRenderer

renderer) { return new RenderMaterial<>(renderer, programSpec, modelFactory); } 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 e99f883cf..295bf70b9 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 @@ -82,7 +82,7 @@ public class ContraptionRenderDispatcher { GL13.glActiveTexture(GL40.GL_TEXTURE4); // the shaders expect light volumes to be in texture 4 if (Backend.canUseVBOs()) { - ContraptionProgram structureShader = ContraptionContext.INSTANCE.getProgram(AllProgramSpecs.C_STRUCTURE); + ContraptionProgram structureShader = ContraptionContext.INSTANCE.getProgram(AllProgramSpecs.STRUCTURE); structureShader.bind(viewProjection, camX, camY, camZ, FastRenderDispatcher.getDebugMode()); for (RenderedContraption renderer : renderers.values()) { renderer.doRenderLayer(layer, structureShader); 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 af8ab05b6..3756d453e 100644 --- a/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java +++ b/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java @@ -71,13 +71,13 @@ public class AllProgramSpecs { .setVert(Locations.FLAP) .setFrag(Locations.BLOCK) .createProgramSpec()); - public static final ProgramSpec C_STRUCTURE = register(builder("contraption_structure") + public static final ProgramSpec STRUCTURE = register(builder("contraption_structure") .addAttributes(ContraptionAttributes.class) .setVert(Locations.CONTRAPTION_STRUCTURE) .setFrag(Locations.BLOCK) .setDefines(ShaderConstants.define("CONTRAPTION")) .createProgramSpec()); - public static final ProgramSpec C_ACTOR = register(builder("contraption_actor") + public static final ProgramSpec ACTOR = register(builder("contraption_actor") .addAttributes(ModelAttributes.class) .addAttributes(ActorVertexAttributes.class) .setVert(Locations.CONTRAPTION_ACTOR)