From d38e2421c97319bbd209866c65336721f1ab3f89 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 30 Dec 2021 15:15:25 -0800 Subject: [PATCH] Fix crash on resource reload with flywheel disabled - Closes #83 - Move engine selection to function - Rename Backend.log to Backend.LOGGER --- .../jozufozu/flywheel/backend/Backend.java | 32 +++++++++++-------- .../com/jozufozu/flywheel/backend/Loader.java | 6 ++-- .../flywheel/backend/OptifineHandler.java | 6 ++-- .../flywheel/backend/gl/shader/GlProgram.java | 2 +- .../flywheel/backend/gl/shader/GlShader.java | 4 +-- .../backend/pipeline/ProtoProgram.java | 2 +- .../backend/source/FileResolution.java | 2 +- .../backend/source/error/ErrorReporter.java | 8 ++--- .../jozufozu/flywheel/core/WorldContext.java | 8 ++--- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index b8ec0ff04..bffbddc46 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -27,7 +27,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; public class Backend { - public static final Logger log = LogManager.getLogger(Backend.class); + public static final Logger LOGGER = LogManager.getLogger(Backend.class); protected static final Backend INSTANCE = new Backend(); public static Backend getInstance() { @@ -39,8 +39,6 @@ public class Backend { public GLCapabilities capabilities; public GlCompat compat; - private boolean enabled; - public final Loader loader; private final List> contexts = new ArrayList<>(); private final Map> materialRegistry = new HashMap<>(); @@ -93,7 +91,7 @@ public class Backend { } materialRegistry.put(name, spec); - log.debug("registered material '" + name + "' with instance size " + spec.getLayout().getStride()); + LOGGER.debug("registered material '" + name + "' with instance size " + spec.getLayout().getStride()); return spec; } @@ -104,20 +102,12 @@ public class Backend { public void refresh() { OptifineHandler.refresh(); - boolean usingShaders = OptifineHandler.usingShaders(); capabilities = GL.createCapabilities(); compat = new GlCompat(capabilities); - engine = FlwConfig.get() - .getEngine(); - - enabled = switch (engine) { - case OFF -> false; - case BATCHING -> !usingShaders; - case INSTANCING -> !usingShaders && compat.instancedArraysSupported(); - }; + engine = chooseEngine(compat); } public Collection> allMaterials() { @@ -133,7 +123,7 @@ public class Backend { } public static boolean isOn() { - return getInstance().enabled; + return getInstance().engine != FlwEngine.OFF; } public static boolean canUseInstancing(@Nullable Level world) { @@ -174,4 +164,18 @@ public class Backend { public static void init() { } + + private static FlwEngine chooseEngine(GlCompat compat) { + FlwEngine preferredChoice = FlwConfig.get() + .getEngine(); + + boolean usingShaders = OptifineHandler.usingShaders(); + boolean canUseEngine = switch (preferredChoice) { + case OFF -> true; + case BATCHING -> !usingShaders; + case INSTANCING -> !usingShaders && compat.instancedArraysSupported(); + }; + + return canUseEngine ? preferredChoice : FlwEngine.OFF; + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/Loader.java b/src/main/java/com/jozufozu/flywheel/backend/Loader.java index 9bf8d3b80..188900381 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Loader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Loader.java @@ -85,10 +85,10 @@ public class Loader implements ResourceManagerReloadListener { throw new ShaderLoadingException("Could not load all shaders, see log for details"); } - Backend.log.info("Loaded all shader programs."); + Backend.LOGGER.info("Loaded all shader programs."); ClientLevel world = Minecraft.getInstance().level; - if (Backend.isFlywheelWorld(world)) { + if (Backend.canUseInstancing(world)) { // TODO: looks like it might be good to have another event here InstancedRenderDispatcher.resetInstanceWorld(world); CrumblingRenderer.reset(); @@ -118,7 +118,7 @@ public class Loader implements ResourceManagerReloadListener { backend.register(spec); } catch (Exception e) { - Backend.log.error(e); + Backend.LOGGER.error(e); } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java b/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java index 1fd8fbd2f..f003208fe 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java +++ b/src/main/java/com/jozufozu/flywheel/backend/OptifineHandler.java @@ -44,9 +44,9 @@ public class OptifineHandler { optifine = Package.getPackage(OPTIFINE_ROOT_PACKAGE); if (optifine == null) { - Backend.log.info("Optifine not detected."); + Backend.LOGGER.info("Optifine not detected."); } else { - Backend.log.info("Optifine detected."); + Backend.LOGGER.info("Optifine detected."); refresh(); } @@ -79,7 +79,7 @@ public class OptifineHandler { return false; }); } catch (IOException e) { - Backend.log.info("No shader config found."); + Backend.LOGGER.info("No shader config found."); } return shadersOff; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java index 8fbac95cd..432575f5e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlProgram.java @@ -49,7 +49,7 @@ public abstract class GlProgram extends GlObject { int index = glGetUniformLocation(this.handle(), uniform); if (index < 0) { - Backend.log.debug("No active uniform '{}' exists in program '{}'. Could be unused.", uniform, this.name); + Backend.LOGGER.debug("No active uniform '{}' exists in program '{}'. Could be unused.", uniform, this.name); } return index; diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java index c386294ba..9aab8e5f0 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/shader/GlShader.java @@ -24,8 +24,8 @@ public class GlShader extends GlObject { String log = GL20.glGetShaderInfoLog(handle); if (!log.isEmpty()) { - Backend.log.error("Shader compilation log for " + name + ": " + log); - Backend.log.error(source); + Backend.LOGGER.error("Shader compilation log for " + name + ": " + log); + Backend.LOGGER.error(source); } //Backend.log.debug(shader.printSource()); diff --git a/src/main/java/com/jozufozu/flywheel/backend/pipeline/ProtoProgram.java b/src/main/java/com/jozufozu/flywheel/backend/pipeline/ProtoProgram.java index caa331d79..db4f9353e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/pipeline/ProtoProgram.java +++ b/src/main/java/com/jozufozu/flywheel/backend/pipeline/ProtoProgram.java @@ -50,7 +50,7 @@ public class ProtoProgram { String log = glGetProgramInfoLog(this.program); if (!log.isEmpty()) { - Backend.log.debug("Program link log for " + parent.name + ": " + log); + Backend.LOGGER.debug("Program link log for " + parent.name + ": " + log); } int result = glGetProgrami(this.program, GL_LINK_STATUS); diff --git a/src/main/java/com/jozufozu/flywheel/backend/source/FileResolution.java b/src/main/java/com/jozufozu/flywheel/backend/source/FileResolution.java index ba4e5f343..9799f00ca 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/source/FileResolution.java +++ b/src/main/java/com/jozufozu/flywheel/backend/source/FileResolution.java @@ -73,7 +73,7 @@ public class FileResolution { builder.pointAtFile(span.getSourceFile()) .pointAt(span, 2); } - Backend.log.error(builder.build()); + Backend.LOGGER.error(builder.build()); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/source/error/ErrorReporter.java b/src/main/java/com/jozufozu/flywheel/backend/source/error/ErrorReporter.java index 97232a6c4..20e70d2eb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/source/error/ErrorReporter.java +++ b/src/main/java/com/jozufozu/flywheel/backend/source/error/ErrorReporter.java @@ -18,7 +18,7 @@ public class ErrorReporter { .pointAt(span, 2) .build(); - Backend.log.error(error); + Backend.LOGGER.error(error); } public static void generateFileError(SourceFile file, String message) { @@ -27,7 +27,7 @@ public class ErrorReporter { .pointAtFile(file) .build(); - Backend.log.error(error); + Backend.LOGGER.error(error); } public static void generateMissingStruct(SourceFile file, Span vertexName, CharSequence msg) { @@ -45,7 +45,7 @@ public class ErrorReporter { .pointAt(vertexName, 2) .hintIncludeFor(span.orElse(null), hint); - Backend.log.error(error.build()); + Backend.LOGGER.error(error.build()); } public static void generateMissingFunction(SourceFile file, CharSequence functionName, CharSequence msg) { @@ -62,6 +62,6 @@ public class ErrorReporter { .pointAtFile(file) .hintIncludeFor(span.orElse(null), hint); - Backend.log.error(error.build()); + Backend.LOGGER.error(error.build()); } } diff --git a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java index 5306d605c..92320a0f9 100644 --- a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java +++ b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java @@ -34,7 +34,7 @@ public class WorldContext

implements ShaderContext

{ @Override public void load() { - Backend.log.info("Loading context '{}'", name); + Backend.LOGGER.info("Loading context '{}'", name); specStream.get() .map(backend::getSpec) @@ -46,10 +46,10 @@ public class WorldContext

implements ShaderContext

{ try { programs.put(spec.name, pipeline.compile(spec)); - Backend.log.debug("Loaded program {}", spec.name); + Backend.LOGGER.debug("Loaded program {}", spec.name); } catch (Exception e) { - Backend.log.error("Error loading program {}", spec.name); - Backend.log.error("", e); + Backend.LOGGER.error("Error loading program {}", spec.name); + Backend.LOGGER.error("", e); backend.loader.notifyError(); } }