From 1b8344d0a30c9d810c2782c94fdf7bc7ca62bedd Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 5 Mar 2025 19:01:32 +0000 Subject: [PATCH] Ignore shader loading errors Another go at fixing #2127. In a892739f8eb87fb078dedae07d8c8614b9d6da4b we set the precision on the Tbo uniform. However, this is stripped in the shader pre-processing Pojav/gl4es does, and so has no effect. As a (terrible) workaround, we now just ignore shader loading errors. This probably does leak memory (we'll never clean up the program), but there's not much we can do about that. --- .../computercraft/client/ClientRegistry.java | 17 ++++++++++++++++- .../client/render/RenderTypes.java | 11 +++++++---- .../shaders/core/computercraft/monitor_tbo.fsh | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java b/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java index 9fe909ee9..7f54840cf 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java +++ b/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java @@ -47,6 +47,8 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.ItemLike; import org.jspecify.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; @@ -63,6 +65,8 @@ import java.util.function.Supplier; * @see ModRegistry The common registry for actual game objects. */ public final class ClientRegistry { + private static final Logger LOG = LoggerFactory.getLogger(ClientRegistry.class); + private ClientRegistry() { } @@ -182,7 +186,18 @@ public final class ClientRegistry { } public static void registerShaders(ResourceProvider resources, BiConsumer> load) throws IOException { - RenderTypes.registerShaders(resources, load); + RenderTypes.registerShaders(resources, (name, create, onLoaded) -> { + ShaderInstance shader; + try { + shader = create.get(); + } catch (Exception e) { + LOG.error("Failed to load {}", name, e); + onLoaded.accept(null); + return; + } + + load.accept(shader, onLoaded); + }); } private record UnclampedPropertyFunction( diff --git a/projects/common/src/client/java/dan200/computercraft/client/render/RenderTypes.java b/projects/common/src/client/java/dan200/computercraft/client/render/RenderTypes.java index 327038690..01f4cf151 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/render/RenderTypes.java +++ b/projects/common/src/client/java/dan200/computercraft/client/render/RenderTypes.java @@ -16,11 +16,11 @@ import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.ShaderInstance; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceProvider; +import org.apache.commons.io.function.IOSupplier; import org.jspecify.annotations.Nullable; import java.io.IOException; import java.util.Objects; -import java.util.function.BiConsumer; import java.util.function.Consumer; /** @@ -68,9 +68,12 @@ public class RenderTypes { return Objects.requireNonNull(GameRenderer.getRendertypeTextShader(), "Text shader has not been registered"); } - public static void registerShaders(ResourceProvider resources, BiConsumer> load) throws IOException { - load.accept( - new MonitorTextureBufferShader( + public interface ShaderLoader { + void tryLoad(String name, IOSupplier create, Consumer<@Nullable ShaderInstance> accept) throws IOException; + } + + public static void registerShaders(ResourceProvider resources, ShaderLoader load) throws IOException { + load.tryLoad("monitor shader", () -> new MonitorTextureBufferShader( resources, ComputerCraftAPI.MOD_ID + "/monitor_tbo", MONITOR_TBO.format() diff --git a/projects/common/src/main/resources/assets/minecraft/shaders/core/computercraft/monitor_tbo.fsh b/projects/common/src/main/resources/assets/minecraft/shaders/core/computercraft/monitor_tbo.fsh index d65cb78d6..85e91499b 100644 --- a/projects/common/src/main/resources/assets/minecraft/shaders/core/computercraft/monitor_tbo.fsh +++ b/projects/common/src/main/resources/assets/minecraft/shaders/core/computercraft/monitor_tbo.fsh @@ -10,7 +10,7 @@ #define FONT_HEIGHT 9.0 uniform sampler2D Sampler0; // Font -uniform mediump usamplerBuffer Tbo; +uniform usamplerBuffer Tbo; layout(std140) uniform MonitorData { vec3 Palette[16];