diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index 7de306597..7d8e23579 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -11,18 +11,13 @@ import org.apache.logging.log4j.Logger; import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GLCapabilities; -import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import com.jozufozu.flywheel.backend.gl.versioned.GlCompat; import com.jozufozu.flywheel.backend.instancing.InstanceData; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.backend.instancing.MaterialSpec; -import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; import com.simibubi.create.foundation.config.AllConfigs; import net.minecraft.client.Minecraft; -import net.minecraft.resources.IReloadableResourceManager; -import net.minecraft.resources.IResourceManager; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.vector.Matrix4f; import net.minecraft.world.IWorld; @@ -31,33 +26,47 @@ import net.minecraft.world.World; public class Backend { public static final Logger log = LogManager.getLogger(Backend.class); - public static final ShaderSources SHADER_SOURCES = new ShaderSources(); + private static final Backend INSTANCE = new Backend(); - public static GLCapabilities capabilities; - public static GlCompat compat; - - private static Matrix4f projectionMatrix = new Matrix4f(); - private static boolean instancedArrays; - private static boolean enabled; - - static final Map> materialRegistry = new HashMap<>(); - static final List> contexts = new ArrayList<>(); - static final Map programSpecRegistry = new HashMap<>(); - - static { - register(WorldContext.INSTANCE); - register(InstancedRenderDispatcher.CRUMBLING); + public static Backend getInstance() { + return INSTANCE; } + public final Minecraft minecraft; + public ShaderSources sources; + + public GLCapabilities capabilities; + public GlCompat compat; + + private Matrix4f projectionMatrix = new Matrix4f(); + private boolean instancedArrays; + private boolean enabled; + + private final List> contexts = new ArrayList<>(); + private final Map> materialRegistry = new HashMap<>(); + private final Map programSpecRegistry = new HashMap<>(); + public Backend() { - throw new IllegalStateException(); + // Can be null when running datagenerators due to the unfortunate time we call this + minecraft = Minecraft.getInstance(); + if (minecraft == null) return; + + sources = new ShaderSources(this); + + OptifineHandler.init(); + } + + void clearContexts() { + SpecMetaRegistry.clear(); + contexts.forEach(IShaderContext::delete); + materialRegistry.clear(); } /** * Get a string describing the Flywheel backend. When there are eventually multiple backends * (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use. */ - public static String getBackendDescriptor() { + public String getBackendDescriptor() { if (canUseInstancing()) { return "GL33 Instanced Arrays"; } @@ -72,7 +81,7 @@ public class Backend { /** * Register a shader program. */ - public static ProgramSpec register(ProgramSpec spec) { + public ProgramSpec register(ProgramSpec spec) { ResourceLocation name = spec.name; if (programSpecRegistry.containsKey(name)) { throw new IllegalStateException("Program spec '" + name + "' already registered."); @@ -84,7 +93,7 @@ public class Backend { /** * Register a shader context. */ - public static

ShaderContext

register(ShaderContext

spec) { + public > C register(C spec) { contexts.add(spec); return spec; } @@ -92,7 +101,7 @@ public class Backend { /** * Register an instancing material. */ - public static MaterialSpec register(MaterialSpec spec) { + public MaterialSpec register(MaterialSpec spec) { ResourceLocation name = spec.name; if (materialRegistry.containsKey(name)) { throw new IllegalStateException("Material spec '" + name + "' already registered."); @@ -101,52 +110,32 @@ public class Backend { return spec; } - public static ProgramSpec getSpec(ResourceLocation name) { + public ProgramSpec getSpec(ResourceLocation name) { return programSpecRegistry.get(name); } - /** - * Used to avoid calling Flywheel functions on (fake) worlds that don't specifically support it. - */ - public static boolean isFlywheelWorld(IWorld world) { - return (world instanceof IFlywheelWorld && ((IFlywheelWorld) world).supportsFlywheel()) || world == Minecraft.getInstance().world; - } - - public static boolean available() { + public boolean available() { return canUseVBOs(); } - public static boolean canUseInstancing() { + public boolean canUseInstancing() { return enabled && instancedArrays; } - public static boolean canUseVBOs() { + public boolean canUseVBOs() { return enabled && gl20(); } - public static boolean gl33() { + public boolean gl33() { return capabilities.OpenGL33; } - public static boolean gl20() { + public boolean gl20() { return capabilities.OpenGL20; } - public static void init() { - // Can be null when running datagenerators due to the unfortunate time we call this - Minecraft mc = Minecraft.getInstance(); - if (mc == null) return; - - IResourceManager manager = mc.getResourceManager(); - - if (manager instanceof IReloadableResourceManager) { - ((IReloadableResourceManager) manager).addReloadListener(SHADER_SOURCES); - } - - OptifineHandler.init(); - } - - public static void refresh() { + public void refresh() { + OptifineHandler.refresh(); capabilities = GL.createCapabilities(); compat = new GlCompat(capabilities); @@ -158,27 +147,38 @@ public class Backend { enabled = AllConfigs.CLIENT.experimentalRendering.get() && !OptifineHandler.usingShaders(); } - public static void reloadWorldRenderers() { - RenderWork.enqueue(Minecraft.getInstance().worldRenderer::loadRenderers); - } - - public static boolean canUseInstancing(World world) { + public boolean canUseInstancing(World world) { return canUseInstancing() && isFlywheelWorld(world); } - public static Collection> allMaterials() { + public Collection> allMaterials() { return materialRegistry.values(); } - public static Collection allPrograms() { + public Collection allPrograms() { return programSpecRegistry.values(); } - public static Matrix4f getProjectionMatrix() { + public Collection> allContexts() { + return contexts; + } + + public Matrix4f getProjectionMatrix() { return projectionMatrix; } - public static void setProjectionMatrix(Matrix4f projectionMatrix) { - Backend.projectionMatrix = projectionMatrix; + public void setProjectionMatrix(Matrix4f projectionMatrix) { + this.projectionMatrix = projectionMatrix; + } + + /** + * Used to avoid calling Flywheel functions on (fake) worlds that don't specifically support it. + */ + public static boolean isFlywheelWorld(IWorld world) { + return (world instanceof IFlywheelWorld && ((IFlywheelWorld) world).supportsFlywheel()) || world == Minecraft.getInstance().world; + } + + public static void reloadWorldRenderers() { + RenderWork.enqueue(Minecraft.getInstance().worldRenderer::loadRenderers); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java new file mode 100644 index 000000000..dd880b92b --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/IShaderContext.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.backend; + +import com.jozufozu.flywheel.backend.gl.shader.GlProgram; + +import net.minecraft.util.ResourceLocation; + +public interface IShaderContext

{ + + P getProgram(ResourceLocation loc); + + /** + * Load all programs associated with this context. This might be just one, if the context is very specialized. + */ + void load(); + + void delete(); +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java index d3316764a..c899108b5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderContext.java @@ -1,14 +1,12 @@ package com.jozufozu.flywheel.backend; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Nullable; -import com.google.common.collect.Lists; import com.jozufozu.flywheel.backend.gl.GlObject; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; import com.jozufozu.flywheel.backend.gl.shader.GlShader; @@ -21,21 +19,20 @@ import com.jozufozu.flywheel.core.shader.spec.ProgramState; import net.minecraft.util.ResourceLocation; -public abstract class ShaderContext

{ +public abstract class ShaderContext

implements IShaderContext

{ protected final Map> programs = new HashMap<>(); - protected ShaderSources sourceRepo; + public final Backend backend; - /** - * Load all programs associated with this context. This might be just one, if the context is very specialized. - */ - public final void load(ShaderSources loader) { - this.sourceRepo = loader; - load(); + public ShaderContext(Backend backend) { + this.backend = backend; } - protected abstract void load(); + @Override + public P getProgram(ResourceLocation spec) { + return programs.get(spec).get(); + } public Program loadAndLink(ProgramSpec spec, @Nullable ProgramState state) { Shader vertexFile = getSource(ShaderType.VERTEX, spec.vert); @@ -46,23 +43,20 @@ public abstract class ShaderContext

{ fragmentFile.defineAll(state.getDefines()); } - return link(loadProgram(spec.name, vertexFile, fragmentFile)); + return link(buildProgram(spec.name, vertexFile, fragmentFile)); } protected Shader getSource(ShaderType type, ResourceLocation name) { - return sourceRepo.source(name, type); + return backend.sources.source(name, type); } protected Program link(Program program) { return program.link(); } - public P getProgram(ResourceLocation spec) { - return programs.get(spec).get(); - } - - protected Program loadProgram(ResourceLocation name, Shader... shaders) { - return loadProgram(name, Lists.newArrayList(shaders)); + @Override + public void delete() { + programs.values().forEach(IMultiProgram::delete); } /** @@ -72,8 +66,8 @@ public abstract class ShaderContext

{ * @param shaders What are the different shader stages that should be linked together? * @return A program with all provided shaders attached */ - protected Program loadProgram(ResourceLocation name, Collection shaders) { - List compiled = new ArrayList<>(shaders.size()); + protected static Program buildProgram(ResourceLocation name, Shader... shaders) { + List compiled = new ArrayList<>(shaders.length); try { Program builder = new Program(name); diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java index 92556e1f3..1aa918085 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java @@ -29,15 +29,17 @@ import com.jozufozu.flywheel.backend.gl.shader.ShaderType; import com.jozufozu.flywheel.backend.loading.Shader; import com.jozufozu.flywheel.backend.loading.ShaderLoadingException; import com.jozufozu.flywheel.core.shader.spec.ProgramSpec; -import com.jozufozu.flywheel.core.shader.spec.SpecMetaRegistry; +import com.jozufozu.flywheel.event.GatherContextEvent; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.datafixers.util.Pair; import com.mojang.serialization.DataResult; import com.mojang.serialization.JsonOps; +import net.minecraft.resources.IReloadableResourceManager; import net.minecraft.resources.IResource; import net.minecraft.resources.IResourceManager; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.ModLoader; import net.minecraftforge.resource.IResourceType; import net.minecraftforge.resource.ISelectiveResourceReloadListener; import net.minecraftforge.resource.VanillaResourceType; @@ -47,31 +49,39 @@ public class ShaderSources implements ISelectiveResourceReloadListener { public static final String SHADER_DIR = "flywheel/shaders/"; public static final String PROGRAM_DIR = "flywheel/programs/"; public static final ArrayList EXTENSIONS = Lists.newArrayList(".vert", ".vsh", ".frag", ".fsh", ".glsl"); + private static final Gson GSON = new GsonBuilder().create(); private final Map shaderSource = new HashMap<>(); private boolean shouldCrash; - private final Gson gson = new GsonBuilder().create(); + private final Backend backend; + + public ShaderSources(Backend backend) { + this.backend = backend; + IResourceManager manager = backend.minecraft.getResourceManager(); + if (manager instanceof IReloadableResourceManager) { + ((IReloadableResourceManager) manager).addReloadListener(this); + } + } @Override public void onResourceManagerReload(IResourceManager manager, Predicate predicate) { if (predicate.test(VanillaResourceType.SHADERS)) { - OptifineHandler.refresh(); - Backend.refresh(); + backend.refresh(); - if (Backend.gl20()) { + if (backend.gl20()) { shaderSource.clear(); shouldCrash = false; - SpecMetaRegistry.init(); + backend.clearContexts(); + ModLoader.get().postEvent(new GatherContextEvent(backend)); loadProgramSpecs(manager); - loadShaderSources(manager); - for (ShaderContext context : Backend.contexts) { - context.load(this); + for (IShaderContext context : backend.allContexts()) { + context.load(); } if (shouldCrash) { @@ -97,13 +107,13 @@ public class ShaderSources implements ISelectiveResourceReloadListener { ResourceLocation specName = ResourceUtil.trim(location, PROGRAM_DIR, ".json"); - DataResult> result = ProgramSpec.CODEC.decode(JsonOps.INSTANCE, gson.fromJson(s, JsonElement.class)); + DataResult> result = ProgramSpec.CODEC.decode(JsonOps.INSTANCE, GSON.fromJson(s, JsonElement.class)); ProgramSpec spec = result.get().orThrow().getFirst(); spec.setName(specName); - Backend.register(spec); + backend.register(spec); } catch (Exception e) { Backend.log.error(e); } diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/spec/SpecMetaRegistry.java b/src/main/java/com/jozufozu/flywheel/backend/SpecMetaRegistry.java similarity index 69% rename from src/main/java/com/jozufozu/flywheel/core/shader/spec/SpecMetaRegistry.java rename to src/main/java/com/jozufozu/flywheel/backend/SpecMetaRegistry.java index 9bd884cd8..a19ed77f7 100644 --- a/src/main/java/com/jozufozu/flywheel/core/shader/spec/SpecMetaRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/SpecMetaRegistry.java @@ -1,14 +1,10 @@ -package com.jozufozu.flywheel.core.shader.spec; +package com.jozufozu.flywheel.backend; import java.util.HashMap; import java.util.Map; -import com.jozufozu.flywheel.core.shader.WorldFog; import com.jozufozu.flywheel.core.shader.extension.IProgramExtension; -import com.jozufozu.flywheel.core.shader.gamestate.FogStateProvider; import com.jozufozu.flywheel.core.shader.gamestate.IGameStateProvider; -import com.jozufozu.flywheel.core.shader.gamestate.NormalDebugStateProvider; -import com.jozufozu.flywheel.core.shader.gamestate.RainbowDebugStateProvider; import net.minecraft.util.ResourceLocation; @@ -17,18 +13,9 @@ public class SpecMetaRegistry { private static final Map registeredExtensions = new HashMap<>(); private static final Map registeredStateProviders = new HashMap<>(); - // TODO: proper registration, don't call this from ShaderLoader - private static boolean initialized = false; - public static void init() { - if (initialized) return; - initialized = true; - - register(FogStateProvider.INSTANCE); - register(RainbowDebugStateProvider.INSTANCE); - register(NormalDebugStateProvider.INSTANCE); - - register(WorldFog.LINEAR); - register(WorldFog.EXP2); + static void clear() { + registeredExtensions.clear(); + registeredStateProviders.clear(); } public static IGameStateProvider getStateProvider(ResourceLocation location) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java index db7c45634..64544c54b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java @@ -4,18 +4,18 @@ import com.jozufozu.flywheel.backend.Backend; public class GlVertexArray extends GlObject { public GlVertexArray() { - setHandle(Backend.compat.vao.genVertexArrays()); + setHandle(Backend.getInstance().compat.vao.genVertexArrays()); } public void bind() { - Backend.compat.vao.bindVertexArray(handle()); + Backend.getInstance().compat.vao.bindVertexArray(handle()); } public void unbind() { - Backend.compat.vao.bindVertexArray(0); + Backend.getInstance().compat.vao.bindVertexArray(0); } protected void deleteInternal(int handle) { - Backend.compat.vao.deleteVertexArrays(handle); + Backend.getInstance().compat.vao.deleteVertexArrays(handle); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java index d970e85e8..d52804652 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java @@ -54,7 +54,7 @@ public class GlBuffer extends GlObject { } public MappedBuffer getBuffer(int offset, int length) { - if (Backend.compat.mapBufferRange != MapBufferRange.UNSUPPORTED) { + if (Backend.getInstance().compat.mapBufferRange != MapBufferRange.UNSUPPORTED) { return new MappedBufferRange(this, offset, length, GL30.GL_MAP_WRITE_BIT); } else { MappedFullBuffer fullBuffer = new MappedFullBuffer(this, MappedBufferUsage.WRITE_ONLY); diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java index 1ce8cdc6c..af0485c1e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/MappedBufferRange.java @@ -26,7 +26,7 @@ public class MappedBufferRange extends MappedBuffer { @Override protected void checkAndMap() { if (!mapped) { - setInternal(Backend.compat.mapBufferRange.mapBuffer(owner.type, offset, length, access)); + setInternal(Backend.getInstance().compat.mapBufferRange.mapBuffer(owner.type, offset, length, access)); mapped = true; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java index fdeaa93ff..438fbb734 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/versioned/GlCompat.java @@ -16,11 +16,10 @@ import com.jozufozu.flywheel.backend.gl.versioned.instancing.InstancedArrays; import com.jozufozu.flywheel.backend.gl.versioned.instancing.VertexArrayObject; /** - * An instance of this class stores information - * about what OpenGL features are available. - *

- * Each field stores an enum variant that provides access to the - * most appropriate version of a feature for the current system. + * An instance of this class stores information about what OpenGL features are available. + *
+ * Each field stores an enum variant that provides access to the most appropriate version of a feature for the current + * system. */ public class GlCompat { public final MapBufferRange mapBufferRange; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/IDynamicInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/IDynamicInstance.java index 6e825639c..fd17da753 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/IDynamicInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/IDynamicInstance.java @@ -1,5 +1,7 @@ package com.jozufozu.flywheel.backend.instancing; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; + /** * An interface giving {@link TileEntityInstance}s a hook to have a function called at * the start of a frame. By implementing {@link IDynamicInstance}, a {@link TileEntityInstance} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/ITickableInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/ITickableInstance.java index 846ad7d8e..4c3265d0c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/ITickableInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/ITickableInstance.java @@ -1,5 +1,7 @@ package com.jozufozu.flywheel.backend.instancing; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; + /** * An interface giving {@link TileEntityInstance}s a hook to have a function called at * the end of every tick. By implementing {@link ITickableInstance}, a {@link TileEntityInstance} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index 0433b8800..235814484 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -12,12 +12,9 @@ import java.util.Vector; import javax.annotation.Nonnull; -import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.core.CrumblingInstanceManager; -import com.jozufozu.flywheel.core.CrumblingProgram; -import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; @@ -38,7 +35,6 @@ import net.minecraft.entity.Entity; import net.minecraft.inventory.container.PlayerContainer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.LazyValue; -import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.vector.Matrix4f; import net.minecraft.world.IWorld; @@ -48,15 +44,9 @@ import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber public class InstancedRenderDispatcher { - public static final ResourceLocation CRUMBLING_CONTEXT = new ResourceLocation(Flywheel.ID, "context/crumbling"); - public static final WorldContext CRUMBLING = new WorldContext<>(CrumblingProgram::new) - .withName(CRUMBLING_CONTEXT) - .withBuiltin(ShaderType.FRAGMENT, CRUMBLING_CONTEXT, "/builtin.frag") - .withBuiltin(ShaderType.VERTEX, CRUMBLING_CONTEXT, "/builtin.vert"); - private static final RenderType crumblingLayer = ModelBakery.BLOCK_DESTRUCTION_RENDER_LAYERS.get(0); - private static final WorldAttached tileInstanceManager = new WorldAttached<>(world -> new TileInstanceManager(WorldContext.INSTANCE.getMaterialManager(world))); + private static final WorldAttached tileInstanceManager = new WorldAttached<>(world -> new TileInstanceManager(Contexts.WORLD.getMaterialManager(world))); private static final LazyValue> blockBreaking = new LazyValue<>(() -> { Vector renderers = new Vector<>(10); for (int i = 0; i < 10; i++) { @@ -86,7 +76,7 @@ public class InstancedRenderDispatcher { @SubscribeEvent public static void onBeginFrame(BeginFrameEvent event) { - WorldContext.INSTANCE.getMaterialManager(event.getWorld()) + Contexts.WORLD.getMaterialManager(event.getWorld()) .checkAndShiftOrigin(event.getInfo()); get(event.getWorld()) .beginFrame(event.getInfo()); @@ -95,8 +85,8 @@ public class InstancedRenderDispatcher { @SubscribeEvent public static void renderLayer(RenderLayerEvent event) { ClientWorld world = event.getWorld(); - if (!Backend.canUseInstancing(world)) return; - MaterialManager materialManager = WorldContext.INSTANCE.getMaterialManager(world); + if (!Backend.getInstance().canUseInstancing(world)) return; + MaterialManager materialManager = Contexts.WORLD.getMaterialManager(world); event.type.startDrawing(); @@ -108,8 +98,8 @@ public class InstancedRenderDispatcher { @SubscribeEvent public static void onReloadRenderers(ReloadRenderersEvent event) { ClientWorld world = event.getWorld(); - if (Backend.canUseInstancing() && world != null) { - WorldContext.INSTANCE.getMaterialManager(world).delete(); + if (Backend.getInstance().canUseInstancing() && world != null) { + Contexts.WORLD.getMaterialManager(world).delete(); TileInstanceManager tileRenderer = get(world); tileRenderer.invalidate(); @@ -118,7 +108,7 @@ public class InstancedRenderDispatcher { } public static void renderBreaking(ClientWorld world, Matrix4f viewProjection, double cameraX, double cameraY, double cameraZ) { - if (!Backend.canUseInstancing(world)) return; + if (!Backend.getInstance().canUseInstancing(world)) return; WorldRenderer worldRenderer = Minecraft.getInstance().worldRenderer; Long2ObjectMap> breakingProgressions = worldRenderer.blockBreakingProgressions; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java index d9412cf52..f88250fc9 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -5,6 +5,8 @@ import java.util.Map; import javax.annotation.Nullable; import com.google.common.collect.Maps; +import com.jozufozu.flywheel.backend.instancing.tile.ITileInstanceFactory; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityType; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java index b7b0b8054..1a35ba4ff 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/Instancer.java @@ -241,7 +241,7 @@ public class Instancer { instanceFormat.vertexAttribPointers(staticAttributes); for (int i = 0; i < instanceFormat.getAttributeCount(); i++) { - Backend.compat.instancedArrays.vertexAttribDivisor(i + staticAttributes, 1); + Backend.getInstance().compat.instancedArrays.vertexAttribDivisor(i + staticAttributes, 1); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java index 54e409a36..8f64b5e79 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java @@ -5,13 +5,13 @@ import java.util.HashMap; import java.util.Map; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.materials.ModelData; import com.jozufozu.flywheel.core.materials.OrientedData; import com.jozufozu.flywheel.core.shader.IProgramCallback; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.util.WeakHashSet; -import com.simibubi.create.foundation.render.AllMaterialSpecs; import net.minecraft.client.renderer.ActiveRenderInfo; import net.minecraft.client.renderer.RenderType; @@ -34,10 +34,10 @@ public class MaterialManager

{ public MaterialManager(WorldContext

context) { this.materials = new HashMap<>(); - this.renderers = new ArrayList<>(Backend.allMaterials().size()); + this.renderers = new ArrayList<>(Backend.getInstance().allMaterials().size()); this.listeners = new WeakHashSet<>(); - for (MaterialSpec spec : Backend.allMaterials()) { + for (MaterialSpec spec : Backend.getInstance().allMaterials()) { InstanceMaterial material = new InstanceMaterial<>(this::getOriginCoordinate, spec); materials.put(spec.name, material); MaterialRenderer

renderer = new MaterialRenderer<>(context.getProgram(spec.getProgramName()), material); @@ -89,11 +89,11 @@ public class MaterialManager

{ } public InstanceMaterial getTransformMaterial() { - return getMaterial(AllMaterialSpecs.TRANSFORMED); + return getMaterial(Materials.TRANSFORMED); } public InstanceMaterial getOrientedMaterial() { - return getMaterial(AllMaterialSpecs.ORIENTED); + return getMaterial(Materials.ORIENTED); } public Vector3i getOriginCoordinate() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/TileInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/TileInstanceManager.java index 114d1a857..510b83dcf 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/TileInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/TileInstanceManager.java @@ -8,6 +8,7 @@ import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import net.minecraft.client.renderer.ActiveRenderInfo; @@ -109,7 +110,7 @@ public class TileInstanceManager implements MaterialManager.OriginShiftListener @SuppressWarnings("unchecked") @Nullable public TileEntityInstance getInstance(T tile, boolean create) { - if (!Backend.canUseInstancing()) return null; + if (!Backend.getInstance().canUseInstancing()) return null; TileEntityInstance instance = instances.get(tile); @@ -123,7 +124,7 @@ public class TileInstanceManager implements MaterialManager.OriginShiftListener } public void onLightUpdate(T tile) { - if (!Backend.canUseInstancing()) return; + if (!Backend.getInstance().canUseInstancing()) return; if (tile instanceof IInstanceRendered) { TileEntityInstance instance = getInstance(tile, false); @@ -134,7 +135,7 @@ public class TileInstanceManager implements MaterialManager.OriginShiftListener } public void add(T tile) { - if (!Backend.canUseInstancing()) return; + if (!Backend.getInstance().canUseInstancing()) return; if (tile instanceof IInstanceRendered) { addInternal(tile); @@ -142,7 +143,7 @@ public class TileInstanceManager implements MaterialManager.OriginShiftListener } public void update(T tile) { - if (!Backend.canUseInstancing()) return; + if (!Backend.getInstance().canUseInstancing()) return; if (tile instanceof IInstanceRendered) { TileEntityInstance instance = getInstance(tile, false); @@ -161,7 +162,7 @@ public class TileInstanceManager implements MaterialManager.OriginShiftListener } public void remove(T tile) { - if (!Backend.canUseInstancing()) return; + if (!Backend.getInstance().canUseInstancing()) return; if (tile instanceof IInstanceRendered) { removeInternal(tile); @@ -169,13 +170,13 @@ public class TileInstanceManager implements MaterialManager.OriginShiftListener } public synchronized void queueAdd(T tile) { - if (!Backend.canUseInstancing()) return; + if (!Backend.getInstance().canUseInstancing()) return; queuedAdditions.add(tile); } public synchronized void queueUpdate(T tile) { - if (!Backend.canUseInstancing()) return; + if (!Backend.getInstance().canUseInstancing()) return; queuedUpdates.add(tile); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/ITileInstanceFactory.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/ITileInstanceFactory.java similarity index 62% rename from src/main/java/com/jozufozu/flywheel/backend/instancing/ITileInstanceFactory.java rename to src/main/java/com/jozufozu/flywheel/backend/instancing/tile/ITileInstanceFactory.java index 8c4936203..b46187ab6 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/ITileInstanceFactory.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/ITileInstanceFactory.java @@ -1,4 +1,6 @@ -package com.jozufozu.flywheel.backend.instancing; +package com.jozufozu.flywheel.backend.instancing.tile; + +import com.jozufozu.flywheel.backend.instancing.MaterialManager; import net.minecraft.tileentity.TileEntity; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/TileEntityInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileEntityInstance.java similarity index 90% rename from src/main/java/com/jozufozu/flywheel/backend/instancing/TileEntityInstance.java rename to src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileEntityInstance.java index 48948e9ce..7ad2d8bfd 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/TileEntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileEntityInstance.java @@ -1,8 +1,14 @@ -package com.jozufozu.flywheel.backend.instancing; +package com.jozufozu.flywheel.backend.instancing.tile; import java.util.Arrays; import java.util.stream.Stream; +import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; +import com.jozufozu.flywheel.backend.instancing.IInstance; +import com.jozufozu.flywheel.backend.instancing.ITickableInstance; +import com.jozufozu.flywheel.backend.instancing.InstanceMaterial; +import com.jozufozu.flywheel.backend.instancing.MaterialManager; +import com.jozufozu.flywheel.backend.instancing.TileInstanceManager; import com.jozufozu.flywheel.core.materials.IFlatLight; import com.jozufozu.flywheel.core.materials.ModelData; import com.jozufozu.flywheel.core.materials.OrientedData; diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java b/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java index 7ed50b701..3c775cfbb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/BufferedModel.java @@ -75,7 +75,7 @@ public class BufferedModel { public void drawInstances(int instanceCount) { if (!valid()) return; - Backend.compat.drawInstanced.drawArraysInstanced(primitiveMode, 0, vertexCount, instanceCount); + Backend.getInstance().compat.drawInstanced.drawArraysInstanced(primitiveMode, 0, vertexCount, instanceCount); } public void delete() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java b/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java index 021641ba1..7c70e984c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/IndexedModel.java @@ -49,7 +49,7 @@ public class IndexedModel extends BufferedModel { public void drawInstances(int instanceCount) { if (vertexCount <= 0 || deleted) return; - Backend.compat.drawInstanced.drawElementsInstanced(primitiveMode, ebo.elementCount, ebo.eboIndexType, 0, instanceCount); + Backend.getInstance().compat.drawInstanced.drawElementsInstanced(primitiveMode, ebo.elementCount, ebo.eboIndexType, 0, instanceCount); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/Contexts.java b/src/main/java/com/jozufozu/flywheel/core/Contexts.java new file mode 100644 index 000000000..b14917d0f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/Contexts.java @@ -0,0 +1,48 @@ +package com.jozufozu.flywheel.core; + +import com.jozufozu.flywheel.Flywheel; +import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.SpecMetaRegistry; +import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.core.shader.WorldFog; +import com.jozufozu.flywheel.core.shader.WorldProgram; +import com.jozufozu.flywheel.core.shader.gamestate.FogStateProvider; +import com.jozufozu.flywheel.core.shader.gamestate.NormalDebugStateProvider; +import com.jozufozu.flywheel.event.GatherContextEvent; + +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) +public class Contexts { + + public static WorldContext WORLD; + public static WorldContext CRUMBLING; + + @SubscribeEvent + public static void flwInit(GatherContextEvent event) { + Backend backend = event.getBackend(); + + SpecMetaRegistry.register(FogStateProvider.INSTANCE); + SpecMetaRegistry.register(NormalDebugStateProvider.INSTANCE); + + SpecMetaRegistry.register(WorldFog.LINEAR); + SpecMetaRegistry.register(WorldFog.EXP2); + + CRUMBLING = backend.register(new WorldContext<>(backend, CrumblingProgram::new) + .withName(Names.CRUMBLING) + .withBuiltin(ShaderType.FRAGMENT, Names.CRUMBLING, "/builtin.frag") + .withBuiltin(ShaderType.VERTEX, Names.CRUMBLING, "/builtin.vert")); + + WORLD = backend.register(new WorldContext<>(backend, WorldProgram::new) + .withName(Names.WORLD) + .withBuiltin(ShaderType.FRAGMENT, Names.WORLD, "/builtin.frag") + .withBuiltin(ShaderType.VERTEX, Names.WORLD, "/builtin.vert")); + } + + public static class Names { + public static final ResourceLocation CRUMBLING = new ResourceLocation(Flywheel.ID, "context/crumbling"); + public static final ResourceLocation WORLD = new ResourceLocation(Flywheel.ID, "context/world"); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java b/src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java index d2da9dcd7..7a50f1671 100644 --- a/src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java @@ -1,6 +1,5 @@ package com.jozufozu.flywheel.core; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.TileInstanceManager; @@ -8,7 +7,7 @@ import net.minecraft.util.math.BlockPos; public class CrumblingInstanceManager extends TileInstanceManager { public CrumblingInstanceManager() { - super(new MaterialManager<>(InstancedRenderDispatcher.CRUMBLING)); + super(new MaterialManager<>(Contexts.CRUMBLING)); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/Formats.java b/src/main/java/com/jozufozu/flywheel/core/Formats.java new file mode 100644 index 000000000..060cae933 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/Formats.java @@ -0,0 +1,24 @@ +package com.jozufozu.flywheel.core; + +import com.jozufozu.flywheel.backend.gl.attrib.CommonAttributes; +import com.jozufozu.flywheel.backend.gl.attrib.MatrixAttributes; +import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; + +public class Formats { + public static final VertexFormat UNLIT_MODEL = VertexFormat.builder() + .addAttributes(CommonAttributes.VEC3, CommonAttributes.NORMAL, CommonAttributes.UV) + .build(); + + public static final VertexFormat TRANSFORMED = litInstance() + .addAttributes(MatrixAttributes.MAT4, + MatrixAttributes.MAT3) + .build(); + public static final VertexFormat ORIENTED = litInstance() + .addAttributes(CommonAttributes.VEC3, CommonAttributes.VEC3, CommonAttributes.QUATERNION) + .build(); + + public static VertexFormat.Builder litInstance() { + return VertexFormat.builder() + .addAttributes(CommonAttributes.LIGHT, CommonAttributes.RGBA); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/Materials.java b/src/main/java/com/jozufozu/flywheel/core/Materials.java new file mode 100644 index 000000000..0d7ee40e2 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/Materials.java @@ -0,0 +1,34 @@ +package com.jozufozu.flywheel.core; + +import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.instancing.InstanceData; +import com.jozufozu.flywheel.backend.instancing.MaterialSpec; +import com.jozufozu.flywheel.core.materials.ModelData; +import com.jozufozu.flywheel.core.materials.OrientedData; +import com.jozufozu.flywheel.event.GatherContextEvent; +import com.simibubi.create.foundation.render.AllMaterialSpecs; + +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) +public class Materials { + public static final MaterialSpec ORIENTED = AllMaterialSpecs.register(new MaterialSpec<>(Locations.ORIENTED, Programs.ORIENTED, Formats.UNLIT_MODEL, Formats.ORIENTED, OrientedData::new)); + public static final MaterialSpec TRANSFORMED = AllMaterialSpecs.register(new MaterialSpec<>(Locations.MODEL, Programs.TRANSFORMED, Formats.UNLIT_MODEL, Formats.TRANSFORMED, ModelData::new)); + + public static MaterialSpec register(MaterialSpec spec) { + return Backend.getInstance().register(spec); + } + + @SubscribeEvent + public static void flwInit(GatherContextEvent event) { + register(ORIENTED); + register(TRANSFORMED); + } + + public static class Locations { + public static final ResourceLocation MODEL = new ResourceLocation("create", "model"); + public static final ResourceLocation ORIENTED = new ResourceLocation("create", "oriented"); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/Programs.java b/src/main/java/com/jozufozu/flywheel/core/Programs.java new file mode 100644 index 000000000..a6553208f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/Programs.java @@ -0,0 +1,10 @@ +package com.jozufozu.flywheel.core; + +import com.jozufozu.flywheel.Flywheel; + +import net.minecraft.util.ResourceLocation; + +public class Programs { + public static final ResourceLocation TRANSFORMED = new ResourceLocation(Flywheel.ID, "model"); + public static final ResourceLocation ORIENTED = new ResourceLocation(Flywheel.ID, "oriented"); +} diff --git a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java index 6be1bde5b..1aff5eee7 100644 --- a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java +++ b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java @@ -7,7 +7,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; -import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.ResourceUtil; import com.jozufozu.flywheel.backend.ShaderContext; @@ -35,13 +34,6 @@ public class WorldContext

extends ShaderContext

{ private static final String declaration = "#flwbuiltins"; private static final Pattern builtinPattern = Pattern.compile(declaration); - public static final ResourceLocation WORLD_CONTEXT = new ResourceLocation(Flywheel.ID, "context/world"); - - public static final WorldContext INSTANCE = new WorldContext<>(WorldProgram::new) - .withName(WORLD_CONTEXT) - .withBuiltin(ShaderType.FRAGMENT, WORLD_CONTEXT, "/builtin.frag") - .withBuiltin(ShaderType.VERTEX, WORLD_CONTEXT, "/builtin.vert"); - protected ResourceLocation name; protected Supplier> specStream; protected TemplateFactory templateFactory; @@ -53,10 +45,11 @@ public class WorldContext

extends ShaderContext

{ private final ExtensibleGlProgram.Factory

factory; - public WorldContext(ExtensibleGlProgram.Factory

factory) { + public WorldContext(Backend backend, ExtensibleGlProgram.Factory

factory) { + super(backend); this.factory = factory; - specStream = () -> Backend.allMaterials() + specStream = () -> backend.allMaterials() .stream() .map(MaterialSpec::getProgramName); @@ -102,16 +95,16 @@ public class WorldContext

extends ShaderContext

{ Backend.log.info("Loading context '{}'", name); try { - builtins.forEach((type, resourceLocation) -> builtinSources.put(type, sourceRepo.getShaderSource(resourceLocation))); + builtins.forEach((type, resourceLocation) -> builtinSources.put(type, backend.sources.getShaderSource(resourceLocation))); } catch (ShaderLoadingException e) { - sourceRepo.notifyError(); + backend.sources.notifyError(); Backend.log.error(String.format("Could not find builtin: %s", e.getMessage())); return; } - template = templateFactory.create(sourceRepo); + template = templateFactory.create(backend.sources); transformer = new ShaderTransformer() .pushStage(this::injectBuiltins) .pushStage(Shader::processIncludes) @@ -119,7 +112,7 @@ public class WorldContext

extends ShaderContext

{ .pushStage(Shader::processIncludes); specStream.get() - .map(Backend::getSpec) + .map(backend::getSpec) .forEach(spec -> { try { @@ -128,11 +121,18 @@ public class WorldContext

extends ShaderContext

{ Backend.log.debug("Loaded program {}", spec.name); } catch (Exception e) { Backend.log.error("Program '{}': {}", spec.name, e); - sourceRepo.notifyError(); + backend.sources.notifyError(); } }); } + @Override + public void delete() { + super.delete(); + + materialManager.forEach(MaterialManager::delete); + } + @Override protected Shader getSource(ShaderType type, ResourceLocation name) { Shader source = super.getSource(type, name); diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/extension/IProgramExtension.java b/src/main/java/com/jozufozu/flywheel/core/shader/extension/IProgramExtension.java index 74da45a1d..bdc3701ec 100644 --- a/src/main/java/com/jozufozu/flywheel/core/shader/extension/IProgramExtension.java +++ b/src/main/java/com/jozufozu/flywheel/core/shader/extension/IProgramExtension.java @@ -1,7 +1,7 @@ package com.jozufozu.flywheel.core.shader.extension; +import com.jozufozu.flywheel.backend.SpecMetaRegistry; import com.jozufozu.flywheel.backend.gl.shader.GlProgram; -import com.jozufozu.flywheel.core.shader.spec.SpecMetaRegistry; import com.mojang.serialization.Codec; import net.minecraft.util.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/core/shader/gamestate/IGameStateProvider.java b/src/main/java/com/jozufozu/flywheel/core/shader/gamestate/IGameStateProvider.java index e5a06e57c..17ece39ec 100644 --- a/src/main/java/com/jozufozu/flywheel/core/shader/gamestate/IGameStateProvider.java +++ b/src/main/java/com/jozufozu/flywheel/core/shader/gamestate/IGameStateProvider.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.core.shader.gamestate; -import com.jozufozu.flywheel.core.shader.spec.SpecMetaRegistry; +import com.jozufozu.flywheel.backend.SpecMetaRegistry; import com.mojang.serialization.Codec; import net.minecraft.util.ResourceLocation; diff --git a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java index 744ad8ea2..759524d38 100644 --- a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java @@ -24,7 +24,7 @@ public class ForgeEvents { ArrayList right = event.getRight(); - String text = "Flywheel: " + Backend.getBackendDescriptor(); + String text = "Flywheel: " + Backend.getInstance().getBackendDescriptor(); if (right.size() < 10) { right.add(""); right.add(text); diff --git a/src/main/java/com/jozufozu/flywheel/event/GatherContextEvent.java b/src/main/java/com/jozufozu/flywheel/event/GatherContextEvent.java new file mode 100644 index 000000000..a409d4363 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/event/GatherContextEvent.java @@ -0,0 +1,19 @@ +package com.jozufozu.flywheel.event; + +import com.jozufozu.flywheel.backend.Backend; + +import net.minecraftforge.eventbus.api.Event; +import net.minecraftforge.fml.event.lifecycle.IModBusEvent; + +public class GatherContextEvent extends Event implements IModBusEvent { + + private final Backend backend; + + public GatherContextEvent(Backend backend) { + this.backend = backend; + } + + public Backend getBackend() { + return backend; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java b/src/main/java/com/jozufozu/flywheel/light/LightVolume.java index c27ad19f3..736b8e06d 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/light/LightVolume.java @@ -51,7 +51,7 @@ public class LightVolume { public LightVolume(GridAlignedBB sampleVolume) { setSampleVolume(sampleVolume); - pixelFormat = Backend.compat.pixelFormat; + pixelFormat = Backend.getInstance().compat.pixelFormat; this.glTexture = new GlTexture(GL_TEXTURE_3D); this.lightData = MemoryUtil.memAlloc(this.textureVolume.volume() * pixelFormat.byteCount()); diff --git a/src/main/java/com/simibubi/create/CreateClient.java b/src/main/java/com/simibubi/create/CreateClient.java index da0fab384..95771076c 100644 --- a/src/main/java/com/simibubi/create/CreateClient.java +++ b/src/main/java/com/simibubi/create/CreateClient.java @@ -24,8 +24,6 @@ import com.simibubi.create.foundation.item.CustomItemModels; import com.simibubi.create.foundation.item.CustomRenderedItems; import com.simibubi.create.foundation.ponder.content.PonderIndex; import com.simibubi.create.foundation.ponder.elements.WorldSectionElement; -import com.simibubi.create.foundation.render.AllMaterialSpecs; -import com.simibubi.create.foundation.render.CreateFlywheelHandler; import com.simibubi.create.foundation.render.SuperByteBufferCache; import com.simibubi.create.foundation.utility.ghost.GhostBlocks; import com.simibubi.create.foundation.utility.outliner.Outliner; @@ -79,13 +77,10 @@ public class CreateClient { modEventBus.addListener(AllParticleTypes::registerFactories); modEventBus.addListener(ClientEvents::loadCompleted); - Backend.init(); - CreateFlywheelHandler.init(); + Backend.getInstance(); } public static void clientInit(FMLClientSetupEvent event) { - AllMaterialSpecs.init(); - BUFFER_CACHE.registerCompartment(KineticTileEntityRenderer.KINETIC_TILE); BUFFER_CACHE.registerCompartment(ContraptionRenderDispatcher.CONTRAPTION, 20); BUFFER_CACHE.registerCompartment(WorldSectionElement.DOC_WORLD_SECTION, 20); diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java index 1df76d71b..bea28ca27 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntityRenderer.java @@ -39,7 +39,7 @@ public class KineticTileEntityRenderer extends SafeTileEntityRenderer renderItem(te, partialTicks, ms, buffer, light, overlay); FilteringRenderer.renderOnTileEntity(te, partialTicks, ms, buffer, light, overlay); - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; renderComponents(te, partialTicks, ms, buffer, light, overlay); } @@ -112,7 +112,7 @@ public class DeployerRenderer extends SafeTileEntityRenderer protected void renderComponents(DeployerTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { IVertexBuilder vb = buffer.getBuffer(RenderType.getSolid()); - if (!Backend.canUseInstancing(te.getWorld())) { + if (!Backend.getInstance().canUseInstancing(te.getWorld())) { KineticTileEntityRenderer.renderRotatingKineticBlock(te, getRenderedBlockState(te), ms, vb, light); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java index 86bc2274c..7424cdf81 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/fan/EncasedFanRenderer.java @@ -28,7 +28,7 @@ public class EncasedFanRenderer extends KineticTileEntityRenderer { @Override protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; Direction direction = te.getBlockState() .get(FACING); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java index b4b0370b6..fc67fbd98 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/FlywheelRenderer.java @@ -36,7 +36,7 @@ public class FlywheelRenderer extends KineticTileEntityRenderer { int light, int overlay) { super.renderSafe(te, partialTicks, ms, buffer, light, overlay); - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; BlockState blockState = te.getBlockState(); FlywheelTileEntity wte = (FlywheelTileEntity) te; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java index 1bda53f70..4ce123e96 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineInstance.java @@ -1,7 +1,7 @@ package com.simibubi.create.content.contraptions.components.flywheel.engine; import com.jozufozu.flywheel.backend.instancing.MaterialManager; -import com.jozufozu.flywheel.backend.instancing.TileEntityInstance; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.jozufozu.flywheel.core.PartialModel; import com.jozufozu.flywheel.core.materials.ModelData; import com.mojang.blaze3d.matrix.MatrixStack; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineRenderer.java index 5be5fe8c3..ce77b42ef 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/EngineRenderer.java @@ -24,7 +24,7 @@ public class EngineRenderer extends SafeTileEntityRe protected void renderSafe(T te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; Block block = te.getBlockState() .getBlock(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MechanicalMixerRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MechanicalMixerRenderer.java index e0ab0cbae..0f84de945 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MechanicalMixerRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/mixer/MechanicalMixerRenderer.java @@ -33,7 +33,7 @@ public class MechanicalMixerRenderer extends KineticTileEntityRenderer { protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; BlockState blockState = te.getBlockState(); MechanicalMixerTileEntity mixer = (MechanicalMixerTileEntity) te; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/press/MechanicalPressRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/press/MechanicalPressRenderer.java index 04a61fecf..2b31d4afc 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/press/MechanicalPressRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/press/MechanicalPressRenderer.java @@ -33,7 +33,7 @@ public class MechanicalPressRenderer extends KineticTileEntityRenderer { int light, int overlay) { super.renderSafe(te, partialTicks, ms, buffer, light, overlay); - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; BlockPos pos = te.getPos(); BlockState blockState = te.getBlockState(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java index dba81ee8b..688796acd 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/saw/SawRenderer.java @@ -49,7 +49,7 @@ public class SawRenderer extends SafeTileEntityRenderer { renderItems(te, partialTicks, ms, buffer, light, overlay); FilteringRenderer.renderOnTileEntity(te, partialTicks, ms, buffer, light, overlay); - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; renderShaft(te, ms, buffer, light, overlay); } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java index acafdb8f9..0d23f7523 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/BearingRenderer.java @@ -26,7 +26,7 @@ public class BearingRenderer extends KineticTileEntityRenderer { protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; super.renderSafe(te, partialTicks, ms, buffer, light, overlay); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java index a57df0205..3ca4d5d17 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/StabilizedBearingMovementBehaviour.java @@ -34,7 +34,7 @@ public class StabilizedBearingMovementBehaviour extends MovementBehaviour { @OnlyIn(Dist.CLIENT) public void renderInContraption(MovementContext context, PlacementSimulationWorld renderWorld, ContraptionMatrices matrices, IRenderTypeBuffer buffer) { - if (Backend.canUseInstancing()) return; + if (Backend.getInstance().canUseInstancing()) return; Direction facing = context.state.get(BlockStateProperties.FACING); PartialModel top = AllBlockPartials.BEARING_TOP; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java index 0653d4524..f57cebc70 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerInstance.java @@ -2,7 +2,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement.ch import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.MaterialManager; -import com.jozufozu.flywheel.backend.instancing.TileEntityInstance; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.jozufozu.flywheel.core.materials.ModelData; import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlockPartials; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerRenderer.java index d15365d66..a63c27957 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/chassis/StickerRenderer.java @@ -26,7 +26,7 @@ public class StickerRenderer extends SafeTileEntityRenderer { protected void renderSafe(StickerTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; BlockState state = te.getBlockState(); SuperByteBuffer head = PartialBufferer.get(AllBlockPartials.STICKER_HEAD, state); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageRenderer.java index a081357f3..5ae13bc6d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/gantry/GantryCarriageRenderer.java @@ -32,7 +32,7 @@ public class GantryCarriageRenderer extends KineticTileEntityRenderer { int light, int overlay) { super.renderSafe(te, partialTicks, ms, buffer, light, overlay); - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; BlockState state = te.getBlockState(); Direction facing = state.get(GantryCarriageBlock.FACING); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java index 4d55ac710..5b9f755df 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/glue/GlueInstance.java @@ -8,10 +8,11 @@ import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstance; import com.jozufozu.flywheel.backend.model.BufferedModel; import com.jozufozu.flywheel.backend.model.IndexedModel; +import com.jozufozu.flywheel.core.Formats; +import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.instancing.ConditionalInstance; import com.jozufozu.flywheel.core.materials.OrientedData; import com.simibubi.create.AllItems; -import com.simibubi.create.foundation.render.AllMaterialSpecs; import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.VecHelper; @@ -29,7 +30,7 @@ public class GlueInstance extends EntityInstance implements ITi public GlueInstance(MaterialManager renderer, SuperGlueEntity entity) { super(renderer, entity); - Instancer instancer = renderer.getMaterial(AllMaterialSpecs.ORIENTED) + Instancer instancer = renderer.getMaterial(Materials.ORIENTED) .get(entity.getType(), GlueInstance::supplyModel); model = new ConditionalInstance<>(instancer) .withCondition(this::shouldShow) @@ -109,6 +110,6 @@ public class GlueInstance extends EntityInstance implements ITi ByteBuffer buffer = ByteBuffer.allocate(quads.length * 4); buffer.asFloatBuffer().put(quads); - return IndexedModel.fromSequentialQuads(AllMaterialSpecs.UNLIT_MODEL, buffer, 8); + return IndexedModel.fromSequentialQuads(Formats.UNLIT_MODEL, buffer, 8); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/AbstractPulleyRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/AbstractPulleyRenderer.java index c5439f47d..f8994d9e7 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/AbstractPulleyRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/pulley/AbstractPulleyRenderer.java @@ -44,7 +44,7 @@ public abstract class AbstractPulleyRenderer extends KineticTileEntityRenderer { protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; super.renderSafe(te, partialTicks, ms, buffer, light, overlay); float offset = getOffset(te, partialTicks); 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 5c6e36c97..7038cd6f3 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 @@ -11,14 +11,10 @@ import static org.lwjgl.opengl.GL13.glEnable; import java.util.List; import java.util.Random; -import java.util.stream.Stream; import org.apache.commons.lang3.tuple.Pair; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.gl.shader.ShaderType; -import com.jozufozu.flywheel.backend.loading.ModelTemplate; -import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; @@ -31,6 +27,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; import com.simibubi.create.foundation.render.AllProgramSpecs; import com.simibubi.create.foundation.render.Compartment; +import com.simibubi.create.foundation.render.CreateContexts; import com.simibubi.create.foundation.render.SuperByteBuffer; import com.simibubi.create.foundation.render.SuperByteBufferCache; import com.simibubi.create.foundation.render.TileEntityRenderHelper; @@ -53,7 +50,6 @@ import net.minecraft.client.renderer.RenderTypeLookup; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.LightType; import net.minecraft.world.World; @@ -74,12 +70,6 @@ public class ContraptionRenderDispatcher { public static final Int2ObjectMap WORLD_HOLDERS = new Int2ObjectOpenHashMap<>(); public static final Compartment> CONTRAPTION = new Compartment<>(); - private static final ResourceLocation ctxRoot = new ResourceLocation("create", "context/contraption"); - public static final WorldContext TILES = contraptionContext(); - public static final WorldContext STRUCTURE = contraptionContext() - .withSpecStream(() -> Stream.of(AllProgramSpecs.STRUCTURE)) - .withTemplateFactory(ModelTemplate::new); - public static void tick() { if (Minecraft.getInstance().isGamePaused()) return; @@ -118,8 +108,8 @@ public class ContraptionRenderDispatcher { glEnable(GL_TEXTURE_3D); glActiveTexture(GL_TEXTURE4); // the shaders expect light volumes to be in texture 4 - if (Backend.canUseVBOs()) { - ContraptionProgram structureShader = STRUCTURE.getProgram(AllProgramSpecs.STRUCTURE); + if (Backend.getInstance().canUseVBOs()) { + ContraptionProgram structureShader = CreateContexts.STRUCTURE.getProgram(AllProgramSpecs.STRUCTURE); structureShader.bind(); structureShader.uploadViewProjection(event.viewProjection); @@ -130,7 +120,7 @@ public class ContraptionRenderDispatcher { } } - if (Backend.canUseInstancing()) { + if (Backend.getInstance().canUseInstancing()) { for (RenderedContraption renderer : RENDERERS.values()) { renderer.materialManager.render(layer, event.viewProjection, event.camX, event.camY, event.camZ, renderer::setup); } @@ -150,7 +140,7 @@ public class ContraptionRenderDispatcher { public static void render(AbstractContraptionEntity entity, Contraption contraption, ContraptionMatrices matrices, IRenderTypeBuffer buffers) { World world = entity.world; - if (Backend.canUseVBOs() && Backend.isFlywheelWorld(world)) { + if (Backend.getInstance().canUseVBOs() && Backend.isFlywheelWorld(world)) { RenderedContraption renderer = getRenderer(world, contraption); PlacementSimulationWorld renderWorld = renderer.renderWorld; @@ -343,10 +333,4 @@ public class ContraptionRenderDispatcher { WORLD_HOLDERS.values().removeIf(ContraptionWorldHolder::isDead); } - private static WorldContext contraptionContext() { - return new WorldContext<>(ContraptionProgram::new) - .withName(ctxRoot) - .withBuiltin(ShaderType.FRAGMENT, ctxRoot, "/builtin.frag") - .withBuiltin(ShaderType.VERTEX, ctxRoot, "/builtin.vert"); - } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java index e9c7c069e..6ee05def0 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/render/RenderedContraption.java @@ -24,6 +24,7 @@ import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionLighter; +import com.simibubi.create.foundation.render.CreateContexts; import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.worldWrappers.PlacementSimulationWorld; @@ -59,11 +60,11 @@ public class RenderedContraption extends ContraptionWorldHolder { public RenderedContraption(PlacementSimulationWorld renderWorld, Contraption contraption) { super(contraption, renderWorld); this.lighter = contraption.makeLighter(); - this.materialManager = new ContraptionMaterialManager(ContraptionRenderDispatcher.TILES); + this.materialManager = new ContraptionMaterialManager(CreateContexts.CWORLD); this.kinetics = new ContraptionInstanceManager(this, materialManager); buildLayers(); - if (Backend.canUseInstancing()) { + if (Backend.getInstance().canUseInstancing()) { buildInstancedTiles(); buildActors(); } @@ -134,7 +135,7 @@ public class RenderedContraption extends ContraptionWorldHolder { BufferedModel layerModel = buildStructureModel(renderWorld, contraption, layer); if (layerModel != null) { - if (Backend.compat.vertexArrayObjectsSupported()) + if (Backend.getInstance().compat.vertexArrayObjectsSupported()) renderLayers.put(layer, new ArrayModelRenderer(layerModel)); else renderLayers.put(layer, new ModelRenderer(layerModel)); diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveRenderer.java index af24a5362..f2f6306cd 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/FluidValveRenderer.java @@ -28,7 +28,7 @@ public class FluidValveRenderer extends KineticTileEntityRenderer { protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; super.renderSafe(te, partialTicks, ms, buffer, light, overlay); BlockState blockState = te.getBlockState(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java index 51438ca7d..cb6c22d09 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerRenderer.java @@ -32,7 +32,7 @@ public class SpeedControllerRenderer extends SmartTileEntityRenderer { protected void renderSafe(BeltTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (!Backend.canUseInstancing(te.getWorld())) { + if (!Backend.getInstance().canUseInstancing(te.getWorld())) { BlockState blockState = te.getBlockState(); if (!AllBlocks.BELT.has(blockState)) return; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java index 3997ce182..03d7026da 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/encased/SplitShaftRenderer.java @@ -28,7 +28,7 @@ public class SplitShaftRenderer extends KineticTileEntityRenderer { @Override protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; Block block = te.getBlockState().getBlock(); final Axis boxAxis = ((IRotate) block).getRotationAxis(te.getBlockState()); diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeRenderer.java index 4649333e4..442cb9bdf 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeRenderer.java @@ -40,7 +40,7 @@ public class GaugeRenderer extends KineticTileEntityRenderer { @Override protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; super.renderSafe(te, partialTicks, ms, buffer, light, overlay); BlockState gaugeState = te.getBlockState(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java index fec1c431f..0c9313b2f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gearbox/GearboxRenderer.java @@ -27,7 +27,7 @@ public class GearboxRenderer extends KineticTileEntityRenderer { @Override protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; final Axis boxAxis = te.getBlockState().get(BlockStateProperties.AXIS); final BlockPos pos = te.getPos(); diff --git a/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorInstance.java b/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorInstance.java index bccafee0a..5977a827c 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorInstance.java +++ b/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorInstance.java @@ -2,7 +2,7 @@ package com.simibubi.create.content.curiosities.projector; import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.MaterialManager; -import com.jozufozu.flywheel.backend.instancing.TileEntityInstance; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.simibubi.create.foundation.render.effects.EffectsHandler; public class ChromaticProjectorInstance extends TileEntityInstance implements IDynamicInstance { diff --git a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java index f4ddab828..e40dfdff0 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelInstance.java @@ -9,7 +9,7 @@ import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.InstanceData; import com.jozufozu.flywheel.backend.instancing.Instancer; import com.jozufozu.flywheel.backend.instancing.MaterialManager; -import com.jozufozu.flywheel.backend.instancing.TileEntityInstance; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.logistics.block.FlapData; import com.simibubi.create.foundation.gui.widgets.InterpolatedValue; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelRenderer.java b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelRenderer.java index 95c00bd70..920d64259 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/belts/tunnel/BeltTunnelRenderer.java @@ -31,7 +31,7 @@ public class BeltTunnelRenderer extends SmartTileEntityRenderer { int light, int overlay) { super.renderSafe(te, partialTicks, ms, buffer, light, overlay); - if (!te.hasFlap() || Backend.canUseInstancing(te.getWorld())) + if (!te.hasFlap() || Backend.getInstance().canUseInstancing(te.getWorld())) return; BlockState blockState = te.getBlockState(); diff --git a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java index 711565bbe..b588893ac 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/mechanicalArm/ArmRenderer.java @@ -42,7 +42,7 @@ public class ArmRenderer extends KineticTileEntityRenderer { super.renderSafe(te, pt, ms, buffer, light, overlay); ArmTileEntity arm = (ArmTileEntity) te; - boolean usingFlywheel = Backend.canUseInstancing(te.getWorld()); + boolean usingFlywheel = Backend.getInstance().canUseInstancing(te.getWorld()); ItemStack item = arm.heldItem; boolean hasItem = !item.isEmpty(); diff --git a/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java b/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java index c07d2c612..db89e4cb4 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverInstance.java @@ -3,7 +3,7 @@ package com.simibubi.create.content.logistics.block.redstone; import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.InstanceMaterial; import com.jozufozu.flywheel.backend.instancing.MaterialManager; -import com.jozufozu.flywheel.backend.instancing.TileEntityInstance; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.jozufozu.flywheel.core.materials.ModelData; import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlockPartials; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverRenderer.java b/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverRenderer.java index 50d13a86d..cfbbe0bfd 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverRenderer.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/redstone/AnalogLeverRenderer.java @@ -28,7 +28,7 @@ public class AnalogLeverRenderer extends SafeTileEntityRenderer> cir) { - if (Backend.canUseInstancing()) { + if (Backend.getInstance().canUseInstancing()) { Iterable entities = cir.getReturnValue(); ArrayList list = Lists.newArrayList(entities); diff --git a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/CancelTileEntityRenderMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/CancelTileEntityRenderMixin.java index b27b2e31d..318492720 100644 --- a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/CancelTileEntityRenderMixin.java +++ b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/CancelTileEntityRenderMixin.java @@ -27,7 +27,7 @@ public class CancelTileEntityRenderMixin { */ @Inject(at = @At("RETURN"), method = "getTileEntities", cancellable = true) private void noRenderInstancedTiles(CallbackInfoReturnable> cir) { - if (Backend.canUseInstancing()) { + if (Backend.getInstance().canUseInstancing()) { List tiles = cir.getReturnValue(); tiles.removeIf(tile -> tile instanceof IInstanceRendered && !((IInstanceRendered) tile).shouldRenderNormally()); diff --git a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/RenderHooksMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/RenderHooksMixin.java index fa244b629..0c345df3f 100644 --- a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/RenderHooksMixin.java +++ b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/RenderHooksMixin.java @@ -8,7 +8,6 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.OptifineHandler; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; @@ -51,13 +50,13 @@ public class RenderHooksMixin { @Inject(at = @At("TAIL"), method = "renderLayer") private void renderLayer(RenderType type, MatrixStack stack, double camX, double camY, double camZ, CallbackInfo ci) { - if (!Backend.available()) + if (!Backend.getInstance().available()) return; Matrix4f view = stack.peek() .getModel(); Matrix4f viewProjection = view.copy(); - viewProjection.multiplyBackward(Backend.getProjectionMatrix()); + viewProjection.multiplyBackward(Backend.getInstance().getProjectionMatrix()); MinecraftForge.EVENT_BUS.post(new RenderLayerEvent(world, type, viewProjection, camX, camY, camZ)); GL20.glUseProgram(0); @@ -65,8 +64,7 @@ public class RenderHooksMixin { @Inject(at = @At("TAIL"), method = "loadRenderers") private void refresh(CallbackInfo ci) { - OptifineHandler.refresh(); - Backend.refresh(); + Backend.getInstance().refresh(); MinecraftForge.EVENT_BUS.post(new ReloadRenderersEvent(world)); } @@ -82,13 +80,13 @@ public class RenderHooksMixin { private void renderBlockBreaking(MatrixStack stack, float p_228426_2_, long p_228426_3_, boolean p_228426_5_, ActiveRenderInfo info, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f p_228426_9_, CallbackInfo ci) { - if (!Backend.available()) + if (!Backend.getInstance().available()) return; Matrix4f view = stack.peek() .getModel(); Matrix4f viewProjection = view.copy(); - viewProjection.multiplyBackward(Backend.getProjectionMatrix()); + viewProjection.multiplyBackward(Backend.getInstance().getProjectionMatrix()); Vector3d cameraPos = info.getProjectedView(); InstancedRenderDispatcher.renderBreaking(world, viewProjection, cameraPos.x, cameraPos.y, cameraPos.z); diff --git a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/StoreProjectionMatrixMixin.java b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/StoreProjectionMatrixMixin.java index cbe203a74..361a32dd8 100644 --- a/src/main/java/com/simibubi/create/foundation/mixin/flywheel/StoreProjectionMatrixMixin.java +++ b/src/main/java/com/simibubi/create/foundation/mixin/flywheel/StoreProjectionMatrixMixin.java @@ -30,7 +30,7 @@ public abstract class StoreProjectionMatrixMixin { @Inject(method = "loadProjectionMatrix", at = @At("TAIL")) private void onProjectionMatrixLoad(Matrix4f projection, CallbackInfo ci) { if (shouldCopy) { - Backend.setProjectionMatrix(projection.copy()); + Backend.getInstance().setProjectionMatrix(projection.copy()); shouldCopy = false; } } diff --git a/src/main/java/com/simibubi/create/foundation/render/AllInstanceFormats.java b/src/main/java/com/simibubi/create/foundation/render/AllInstanceFormats.java index 4fb31d1b8..2ae42930c 100644 --- a/src/main/java/com/simibubi/create/foundation/render/AllInstanceFormats.java +++ b/src/main/java/com/simibubi/create/foundation/render/AllInstanceFormats.java @@ -1,20 +1,11 @@ package com.simibubi.create.foundation.render; import com.jozufozu.flywheel.backend.gl.attrib.CommonAttributes; -import com.jozufozu.flywheel.backend.gl.attrib.MatrixAttributes; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; +import com.jozufozu.flywheel.core.Formats; public class AllInstanceFormats { - public static final VertexFormat MODEL = litInstance() - .addAttributes(MatrixAttributes.MAT4, - MatrixAttributes.MAT3) - .build(); - - public static final VertexFormat ORIENTED = litInstance() - .addAttributes(CommonAttributes.VEC3, CommonAttributes.VEC3, CommonAttributes.QUATERNION) - .build(); - public static VertexFormat ROTATING = kineticInstance() .addAttributes(CommonAttributes.NORMAL) .build(); @@ -35,13 +26,8 @@ public class AllInstanceFormats { CommonAttributes.FLOAT, CommonAttributes.FLOAT, CommonAttributes.FLOAT, CommonAttributes.FLOAT) .build(); - private static VertexFormat.Builder litInstance() { - return VertexFormat.builder() - .addAttributes(CommonAttributes.LIGHT, CommonAttributes.RGBA); - } - private static VertexFormat.Builder kineticInstance() { - return litInstance() + return Formats.litInstance() .addAttributes(CommonAttributes.VEC3, CommonAttributes.FLOAT, CommonAttributes.FLOAT); } } diff --git a/src/main/java/com/simibubi/create/foundation/render/AllMaterialSpecs.java b/src/main/java/com/simibubi/create/foundation/render/AllMaterialSpecs.java index 562ac7c26..3a0c1dbcf 100644 --- a/src/main/java/com/simibubi/create/foundation/render/AllMaterialSpecs.java +++ b/src/main/java/com/simibubi/create/foundation/render/AllMaterialSpecs.java @@ -1,12 +1,10 @@ package com.simibubi.create.foundation.render; -import static com.jozufozu.flywheel.backend.Backend.register; - -import com.jozufozu.flywheel.backend.gl.attrib.CommonAttributes; -import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; +import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.instancing.InstanceData; import com.jozufozu.flywheel.backend.instancing.MaterialSpec; -import com.jozufozu.flywheel.core.materials.ModelData; -import com.jozufozu.flywheel.core.materials.OrientedData; +import com.jozufozu.flywheel.core.Formats; +import com.jozufozu.flywheel.event.GatherContextEvent; import com.simibubi.create.Create; import com.simibubi.create.content.contraptions.base.RotatingData; import com.simibubi.create.content.contraptions.components.actors.ActorData; @@ -14,27 +12,33 @@ import com.simibubi.create.content.contraptions.relays.belt.BeltData; import com.simibubi.create.content.logistics.block.FlapData; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public class AllMaterialSpecs { public static void init() { // noop, make sure the static field are loaded. } - public static final VertexFormat UNLIT_MODEL = VertexFormat.builder() - .addAttributes(CommonAttributes.VEC3, CommonAttributes.NORMAL, CommonAttributes.UV) - .build(); + public static final MaterialSpec ROTATING = new MaterialSpec<>(Locations.ROTATING, AllProgramSpecs.ROTATING, Formats.UNLIT_MODEL, AllInstanceFormats.ROTATING, RotatingData::new); + public static final MaterialSpec BELTS = new MaterialSpec<>(Locations.BELTS, AllProgramSpecs.BELT, Formats.UNLIT_MODEL, AllInstanceFormats.BELT, BeltData::new); + public static final MaterialSpec ACTORS = new MaterialSpec<>(Locations.ACTORS, AllProgramSpecs.ACTOR, Formats.UNLIT_MODEL, AllInstanceFormats.ACTOR, ActorData::new); + public static final MaterialSpec FLAPS = new MaterialSpec<>(Locations.FLAPS, AllProgramSpecs.FLAPS, Formats.UNLIT_MODEL, AllInstanceFormats.FLAP, FlapData::new); - public static final MaterialSpec ROTATING = register(new MaterialSpec<>(Locations.ROTATING, AllProgramSpecs.ROTATING, UNLIT_MODEL, AllInstanceFormats.ROTATING, RotatingData::new)); - public static final MaterialSpec TRANSFORMED = register(new MaterialSpec<>(Locations.MODEL, AllProgramSpecs.MODEL, UNLIT_MODEL, AllInstanceFormats.MODEL, ModelData::new)); - public static final MaterialSpec ORIENTED = register(new MaterialSpec<>(Locations.ORIENTED, AllProgramSpecs.ORIENTED, UNLIT_MODEL, AllInstanceFormats.ORIENTED, OrientedData::new)); + public static MaterialSpec register(MaterialSpec spec) { + return Backend.getInstance().register(spec); + } - public static final MaterialSpec BELTS = register(new MaterialSpec<>(Locations.BELTS, AllProgramSpecs.BELT, UNLIT_MODEL, AllInstanceFormats.BELT, BeltData::new)); - public static final MaterialSpec ACTORS = register(new MaterialSpec<>(Locations.ACTORS, AllProgramSpecs.ACTOR, UNLIT_MODEL, AllInstanceFormats.ACTOR, ActorData::new)); - public static final MaterialSpec FLAPS = register(new MaterialSpec<>(Locations.FLAPS, AllProgramSpecs.FLAPS, UNLIT_MODEL, AllInstanceFormats.FLAP, FlapData::new)); + @SubscribeEvent + public static void flwInit(GatherContextEvent event) { + register(ROTATING); + register(BELTS); + register(ACTORS); + register(FLAPS); + } public static class Locations { - public static final ResourceLocation MODEL = new ResourceLocation("create", "model"); - public static final ResourceLocation ORIENTED = new ResourceLocation("create", "oriented"); public static final ResourceLocation ROTATING = new ResourceLocation(Create.ID, "rotating"); public static final ResourceLocation BELTS = new ResourceLocation(Create.ID, "belts"); public static final ResourceLocation ACTORS = new ResourceLocation(Create.ID, "actors"); 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 94acc2e71..6df22f622 100644 --- a/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java +++ b/src/main/java/com/simibubi/create/foundation/render/AllProgramSpecs.java @@ -1,15 +1,11 @@ package com.simibubi.create.foundation.render; -import com.jozufozu.flywheel.Flywheel; import com.simibubi.create.Create; import net.minecraft.util.ResourceLocation; public class AllProgramSpecs { - public static final ResourceLocation MODEL = new ResourceLocation(Flywheel.ID, "model");//, Locations.MODEL_VERT, Locations.BLOCK); - public static final ResourceLocation ORIENTED = new ResourceLocation(Flywheel.ID, "oriented");//, Locations.ORIENTED, Locations.BLOCK); - public static final ResourceLocation ROTATING = loc("rotating"); public static final ResourceLocation CHROMATIC = loc("chromatic"); public static final ResourceLocation BELT = loc("belt"); diff --git a/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java b/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java new file mode 100644 index 000000000..5b537ab7f --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/render/CreateContexts.java @@ -0,0 +1,46 @@ +package com.simibubi.create.foundation.render; + +import java.util.stream.Stream; + +import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.SpecMetaRegistry; +import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.backend.loading.ModelTemplate; +import com.jozufozu.flywheel.core.WorldContext; +import com.jozufozu.flywheel.core.shader.gamestate.RainbowDebugStateProvider; +import com.jozufozu.flywheel.event.GatherContextEvent; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionProgram; +import com.simibubi.create.foundation.render.effects.EffectsContext; + +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) +public class CreateContexts { + private static final ResourceLocation CONTRAPTION = new ResourceLocation("create", "context/contraption"); + + public static EffectsContext EFFECTS; + public static WorldContext CWORLD; + public static WorldContext STRUCTURE; + + @SubscribeEvent + public static void flwInit(GatherContextEvent event) { + Backend backend = event.getBackend(); + + SpecMetaRegistry.register(RainbowDebugStateProvider.INSTANCE); + + EFFECTS = backend.register(new EffectsContext(backend)); + CWORLD = backend.register(contraptionContext(backend)); + STRUCTURE = backend.register(contraptionContext(backend) + .withSpecStream(() -> Stream.of(AllProgramSpecs.STRUCTURE)) + .withTemplateFactory(ModelTemplate::new)); + } + + private static WorldContext contraptionContext(Backend backend) { + return new WorldContext<>(backend, ContraptionProgram::new) + .withName(CONTRAPTION) + .withBuiltin(ShaderType.FRAGMENT, CONTRAPTION, "/builtin.frag") + .withBuiltin(ShaderType.VERTEX, CONTRAPTION, "/builtin.vert"); + } +} diff --git a/src/main/java/com/simibubi/create/foundation/render/CreateFlywheelHandler.java b/src/main/java/com/simibubi/create/foundation/render/CreateFlywheelHandler.java deleted file mode 100644 index 0a7a950f0..000000000 --- a/src/main/java/com/simibubi/create/foundation/render/CreateFlywheelHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.simibubi.create.foundation.render; - -import com.jozufozu.flywheel.backend.Backend; -import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; -import com.simibubi.create.foundation.render.effects.EffectsContext; - -public class CreateFlywheelHandler { - public static void init() { - Backend.register(ContraptionRenderDispatcher.TILES); - Backend.register(ContraptionRenderDispatcher.STRUCTURE); - Backend.register(EffectsContext.INSTANCE); - } -} 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 3579f85f1..1967b49d5 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 @@ -11,15 +11,13 @@ import net.minecraft.util.ResourceLocation; public class EffectsContext extends ShaderContext { - public static final EffectsContext INSTANCE = new EffectsContext(); - - public EffectsContext() { - super(); + public EffectsContext(Backend backend) { + super(backend); } @Override public void load() { - ProgramSpec programSpec = Backend.getSpec(AllProgramSpecs.CHROMATIC); + ProgramSpec programSpec = Backend.getInstance().getSpec(AllProgramSpecs.CHROMATIC); try { programs.put(programSpec.name, new SphereFilterProgram(loadAndLink(programSpec, null))); @@ -27,7 +25,7 @@ public class EffectsContext extends ShaderContext { Backend.log.debug("Loaded program {}", programSpec.name); } catch (Exception e) { Backend.log.error("Program '{}': {}", programSpec.name, e); - sourceRepo.notifyError(); + backend.sources.notifyError(); } } diff --git a/src/main/java/com/simibubi/create/foundation/render/effects/EffectsHandler.java b/src/main/java/com/simibubi/create/foundation/render/effects/EffectsHandler.java index c8095eff5..41ce02740 100644 --- a/src/main/java/com/simibubi/create/foundation/render/effects/EffectsHandler.java +++ b/src/main/java/com/simibubi/create/foundation/render/effects/EffectsHandler.java @@ -12,6 +12,7 @@ import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.core.FullscreenQuad; import com.jozufozu.flywheel.util.RenderUtil; import com.simibubi.create.foundation.render.AllProgramSpecs; +import com.simibubi.create.foundation.render.CreateContexts; import com.simibubi.create.foundation.utility.AnimationTickHolder; import net.minecraft.client.MainWindow; @@ -29,11 +30,11 @@ public class EffectsHandler { @Nullable public static EffectsHandler getInstance() { - if (Backend.available() && instance == null) { - instance = new EffectsHandler(); + if (Backend.getInstance().available() && instance == null) { + instance = new EffectsHandler(Backend.getInstance()); } - if (!Backend.available() && instance != null) { + if (!Backend.getInstance().available() && instance != null) { instance.delete(); instance = null; } @@ -49,12 +50,12 @@ public class EffectsHandler { return Minecraft.getInstance().gameRenderer.getFarPlaneDistance() * 4; } - + private final Backend backend; private final Framebuffer framebuffer; - private final ArrayList spheres; - public EffectsHandler() { + public EffectsHandler(Backend backend) { + this.backend = backend; spheres = new ArrayList<>(); Framebuffer render = Minecraft.getInstance().getFramebuffer(); @@ -79,10 +80,10 @@ public class EffectsHandler { Framebuffer mainBuffer = Minecraft.getInstance().getFramebuffer(); - Backend.compat.fbo.bindFramebuffer(FramebufferConstants.FRAME_BUFFER, framebuffer.framebufferObject); + backend.compat.fbo.bindFramebuffer(FramebufferConstants.FRAME_BUFFER, framebuffer.framebufferObject); GL11.glClear(GL30.GL_COLOR_BUFFER_BIT); - SphereFilterProgram program = EffectsContext.INSTANCE.getProgram(AllProgramSpecs.CHROMATIC); + SphereFilterProgram program = CreateContexts.EFFECTS.getProgram(AllProgramSpecs.CHROMATIC); program.bind(); program.bindColorTexture(mainBuffer.getColorAttachment()); @@ -129,10 +130,10 @@ public class EffectsHandler { program.unbind(); spheres.clear(); - Backend.compat.fbo.bindFramebuffer(GL30.GL_READ_FRAMEBUFFER, framebuffer.framebufferObject); - Backend.compat.fbo.bindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, mainBuffer.framebufferObject); - Backend.compat.blit.blitFramebuffer(0, 0, mainBuffer.framebufferWidth, mainBuffer.framebufferHeight, 0, 0, mainBuffer.framebufferWidth, mainBuffer.framebufferHeight, GL30.GL_COLOR_BUFFER_BIT, GL20.GL_LINEAR); - Backend.compat.fbo.bindFramebuffer(FramebufferConstants.FRAME_BUFFER, mainBuffer.framebufferObject); + backend.compat.fbo.bindFramebuffer(GL30.GL_READ_FRAMEBUFFER, framebuffer.framebufferObject); + backend.compat.fbo.bindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, mainBuffer.framebufferObject); + backend.compat.blit.blitFramebuffer(0, 0, mainBuffer.framebufferWidth, mainBuffer.framebufferHeight, 0, 0, mainBuffer.framebufferWidth, mainBuffer.framebufferHeight, GL30.GL_COLOR_BUFFER_BIT, GL20.GL_LINEAR); + backend.compat.fbo.bindFramebuffer(FramebufferConstants.FRAME_BUFFER, mainBuffer.framebufferObject); } public void delete() { diff --git a/src/main/java/com/simibubi/create/foundation/tileEntity/renderer/ColoredOverlayTileEntityRenderer.java b/src/main/java/com/simibubi/create/foundation/tileEntity/renderer/ColoredOverlayTileEntityRenderer.java index e0b204bc8..2b47c21fb 100644 --- a/src/main/java/com/simibubi/create/foundation/tileEntity/renderer/ColoredOverlayTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/tileEntity/renderer/ColoredOverlayTileEntityRenderer.java @@ -19,7 +19,7 @@ public abstract class ColoredOverlayTileEntityRenderer ext protected void renderSafe(T te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { - if (Backend.canUseInstancing(te.getWorld())) return; + if (Backend.getInstance().canUseInstancing(te.getWorld())) return; SuperByteBuffer render = render(getOverlayBuffer(te), getColor(te, partialTicks), light); render.renderInto(ms, buffer.getBuffer(RenderType.getSolid())); diff --git a/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag b/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag index 43197a6c0..919e64ceb 100644 --- a/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag +++ b/src/main/resources/assets/create/flywheel/shaders/context/contraption/builtin.frag @@ -2,6 +2,7 @@ #flwinclude <"flywheel:core/lightutil.glsl"> varying vec3 BoxCoord; +varying vec2 BoxLight; uniform sampler3D uLightVolume; uniform sampler2D uBlockAtlas;