More granularity for shader context loading

This commit is contained in:
JozsefA 2021-05-04 16:22:27 -07:00
parent bc5630e593
commit b6f13aa7ff
7 changed files with 54 additions and 61 deletions

View file

@ -7,7 +7,7 @@ import java.util.concurrent.ConcurrentHashMap;
import com.jozufozu.flywheel.backend.core.BasicInstancedTileRenderer; import com.jozufozu.flywheel.backend.core.BasicInstancedTileRenderer;
import com.jozufozu.flywheel.backend.core.OrientedModel; import com.jozufozu.flywheel.backend.core.OrientedModel;
import com.jozufozu.flywheel.backend.core.TransformedModel; 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.CreateClient;
import com.simibubi.create.content.contraptions.KineticDebugger; import com.simibubi.create.content.contraptions.KineticDebugger;
import com.simibubi.create.content.contraptions.base.KineticRenderMaterials; import com.simibubi.create.content.contraptions.base.KineticRenderMaterials;
@ -28,7 +28,7 @@ import net.minecraft.world.World;
public class FastRenderDispatcher { public class FastRenderDispatcher {
public static Map<MaterialType<?>, MaterialFactory> materials = new HashMap<>(); public static Map<MaterialType<?>, MaterialSpec> materials = new HashMap<>();
static { static {
registerMaterials(); registerMaterials();
@ -37,12 +37,12 @@ public class FastRenderDispatcher {
public static WorldAttached<ConcurrentHashMap.KeySetView<TileEntity, Boolean>> queuedUpdates = new WorldAttached<>(ConcurrentHashMap::newKeySet); public static WorldAttached<ConcurrentHashMap.KeySetView<TileEntity, Boolean>> queuedUpdates = new WorldAttached<>(ConcurrentHashMap::newKeySet);
public static void registerMaterials() { public static void registerMaterials() {
materials.put(MaterialTypes.TRANSFORMED, new MaterialFactory(AllProgramSpecs.MODEL, TransformedModel::new)); materials.put(MaterialTypes.TRANSFORMED, new MaterialSpec(AllProgramSpecs.MODEL, TransformedModel::new));
materials.put(MaterialTypes.ORIENTED, new MaterialFactory(AllProgramSpecs.ORIENTED, OrientedModel::new)); materials.put(MaterialTypes.ORIENTED, new MaterialSpec(AllProgramSpecs.ORIENTED, OrientedModel::new));
materials.put(KineticRenderMaterials.BELTS, new MaterialFactory(AllProgramSpecs.BELT, BeltInstancedModel::new)); materials.put(KineticRenderMaterials.BELTS, new MaterialSpec(AllProgramSpecs.BELT, BeltInstancedModel::new));
materials.put(KineticRenderMaterials.ROTATING, new MaterialFactory(AllProgramSpecs.ROTATING, RotatingModel::new)); materials.put(KineticRenderMaterials.ROTATING, new MaterialSpec(AllProgramSpecs.ROTATING, RotatingModel::new));
materials.put(KineticRenderMaterials.FLAPS, new MaterialFactory(AllProgramSpecs.FLAPS, FlapModel::new)); materials.put(KineticRenderMaterials.FLAPS, new MaterialSpec(AllProgramSpecs.FLAPS, FlapModel::new));
materials.put(KineticRenderMaterials.ACTORS, new MaterialFactory(AllProgramSpecs.C_ACTOR, ActorModel::new)); materials.put(KineticRenderMaterials.ACTORS, new MaterialSpec(AllProgramSpecs.ACTOR, ActorModel::new));
} }
public static void enqueueUpdate(TileEntity te) { public static void enqueueUpdate(TileEntity te) {

View file

@ -23,14 +23,7 @@ public abstract class ShaderContext<P extends GlProgram> {
public abstract ShaderSpecLoader<P> getLoader(); public abstract ShaderSpecLoader<P> getLoader();
public void load(ShaderLoader loader) { public abstract void load(ShaderLoader loader);
programs.values().forEach(IMultiProgram::delete);
programs.clear();
for (ProgramSpec programSpec : Backend.specRegistry.values()) {
loadProgramFromSpec(loader, programSpec);
}
}
public void loadProgramFromSpec(ShaderLoader loader, ProgramSpec programSpec) { public void loadProgramFromSpec(ShaderLoader loader, ProgramSpec programSpec) {

View file

@ -1,7 +1,9 @@
package com.jozufozu.flywheel.backend.core; package com.jozufozu.flywheel.backend.core;
import com.jozufozu.flywheel.backend.ShaderLoader;
import com.jozufozu.flywheel.backend.gl.shader.FogSensitiveProgram; import com.jozufozu.flywheel.backend.gl.shader.FogSensitiveProgram;
import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionProgram; import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionProgram;
import com.simibubi.create.foundation.render.AllProgramSpecs;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -13,5 +15,10 @@ public class ContraptionContext extends WorldContext<ContraptionProgram> {
super(new ResourceLocation("create", "contraption"), new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new)); super(new ResourceLocation("create", "contraption"), new FogSensitiveProgram.SpecLoader<>(ContraptionProgram::new));
} }
@Override
public void load(ShaderLoader loader) {
super.load(loader);
loadProgramFromSpec(loader, AllProgramSpecs.STRUCTURE);
}
} }

View file

@ -1,16 +1,18 @@
package com.jozufozu.flywheel.backend.core; 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.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.ResourceUtil;
import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderContext;
import com.jozufozu.flywheel.backend.ShaderLoader; import com.jozufozu.flywheel.backend.ShaderLoader;
import com.jozufozu.flywheel.backend.gl.shader.FogSensitiveProgram; 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.ShaderSpecLoader;
import com.jozufozu.flywheel.backend.gl.shader.ShaderType; import com.jozufozu.flywheel.backend.gl.shader.ShaderType;
import com.jozufozu.flywheel.backend.instancing.MaterialSpec;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -22,51 +24,34 @@ public class WorldContext<P extends BasicProgram> extends ShaderContext<P> {
private final ShaderSpecLoader<P> loader; private final ShaderSpecLoader<P> loader;
public final ResourceLocation frag; final Map<ShaderType, ResourceLocation> builtins;
public final ResourceLocation vert;
public WorldContext(ResourceLocation root, ShaderSpecLoader<P> loader) { public WorldContext(ResourceLocation root, ShaderSpecLoader<P> loader) {
super(root); super(root);
this.frag = ResourceUtil.subPath(root, "/builtin.frag"); builtins = new EnumMap<>(ShaderType.class);
this.vert = ResourceUtil.subPath(root, "/builtin.vert"); builtins.put(ShaderType.FRAGMENT, ResourceUtil.subPath(root, "/builtin.frag"));
builtins.put(ShaderType.VERTEX, ResourceUtil.subPath(root, "/builtin.vert"));
this.loader = loader; 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 @Override
public String preProcess(ShaderLoader loader, String shaderSrc, ShaderType type) { public String preProcess(ShaderLoader loader, String shaderSrc, ShaderType type) {
return ShaderLoader.lines(shaderSrc).flatMap(line -> { String builtinSrc = loader.getShaderSource(builtins.get(type));
Matcher matcher = builtinPattern.matcher(line);
if (matcher.find()) { return builtinPattern.matcher(shaderSrc)
ResourceLocation builtins; .replaceFirst(builtinSrc);
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;
} }
@Override @Override

View file

@ -3,16 +3,24 @@ package com.jozufozu.flywheel.backend.instancing;
import com.jozufozu.flywheel.backend.core.BasicProgram; import com.jozufozu.flywheel.backend.core.BasicProgram;
import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec; import com.jozufozu.flywheel.backend.gl.shader.ProgramSpec;
public class MaterialFactory { public class MaterialSpec {
ProgramSpec programSpec; private final ProgramSpec programSpec;
ModelFactory<?> modelFactory; private final ModelFactory<?> modelFactory;
public MaterialFactory(ProgramSpec programSpec, ModelFactory<?> modelFactory) { public MaterialSpec(ProgramSpec programSpec, ModelFactory<?> modelFactory) {
this.programSpec = programSpec; this.programSpec = programSpec;
this.modelFactory = modelFactory; this.modelFactory = modelFactory;
} }
public ProgramSpec getProgramSpec() {
return programSpec;
}
public ModelFactory<?> getModelFactory() {
return modelFactory;
}
public <P extends BasicProgram> RenderMaterial<P, ?> create(InstancedTileRenderer<P> renderer) { public <P extends BasicProgram> RenderMaterial<P, ?> create(InstancedTileRenderer<P> renderer) {
return new RenderMaterial<>(renderer, programSpec, modelFactory); return new RenderMaterial<>(renderer, programSpec, modelFactory);
} }

View file

@ -82,7 +82,7 @@ public class ContraptionRenderDispatcher {
GL13.glActiveTexture(GL40.GL_TEXTURE4); // the shaders expect light volumes to be in texture 4 GL13.glActiveTexture(GL40.GL_TEXTURE4); // the shaders expect light volumes to be in texture 4
if (Backend.canUseVBOs()) { 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()); structureShader.bind(viewProjection, camX, camY, camZ, FastRenderDispatcher.getDebugMode());
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : renderers.values()) {
renderer.doRenderLayer(layer, structureShader); renderer.doRenderLayer(layer, structureShader);

View file

@ -71,13 +71,13 @@ public class AllProgramSpecs {
.setVert(Locations.FLAP) .setVert(Locations.FLAP)
.setFrag(Locations.BLOCK) .setFrag(Locations.BLOCK)
.createProgramSpec()); .createProgramSpec());
public static final ProgramSpec C_STRUCTURE = register(builder("contraption_structure") public static final ProgramSpec STRUCTURE = register(builder("contraption_structure")
.addAttributes(ContraptionAttributes.class) .addAttributes(ContraptionAttributes.class)
.setVert(Locations.CONTRAPTION_STRUCTURE) .setVert(Locations.CONTRAPTION_STRUCTURE)
.setFrag(Locations.BLOCK) .setFrag(Locations.BLOCK)
.setDefines(ShaderConstants.define("CONTRAPTION")) .setDefines(ShaderConstants.define("CONTRAPTION"))
.createProgramSpec()); .createProgramSpec());
public static final ProgramSpec C_ACTOR = register(builder("contraption_actor") public static final ProgramSpec ACTOR = register(builder("contraption_actor")
.addAttributes(ModelAttributes.class) .addAttributes(ModelAttributes.class)
.addAttributes(ActorVertexAttributes.class) .addAttributes(ActorVertexAttributes.class)
.setVert(Locations.CONTRAPTION_ACTOR) .setVert(Locations.CONTRAPTION_ACTOR)