Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.7.+'
id 'fabric-loom' version '1.10.1'
id 'io.github.juuxel.loom-quiltflower' version '1.8.0'
id 'maven-publish'
}
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# https://modmuss50.me/fabric.html
minecraft_version=1.21
yarn_mappings=1.21+build.7
loader_version=0.15.11
fabric_version=0.100.4+1.21
minecraft_version=1.21.8
yarn_mappings=1.21.8+build.1
loader_version=0.17.2
fabric_version=0.133.4+1.21.8

# Mod Properties
mod_version=1.12
mod_version=1.13
maven_group=suso
archives_base_name=shader-reload
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
45 changes: 45 additions & 0 deletions src/main/java/suso/shaderreload/CustomShaderLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package suso.shaderreload;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.*;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.resource.DefaultResourcePack;
import net.minecraft.util.Identifier;

import java.io.FileNotFoundException;
import java.util.Set;

public class CustomShaderLoader extends ShaderLoader {
private final DefaultResourcePack defaultPack = MinecraftClient.getInstance().getDefaultResourcePack();

public CustomShaderLoader(TextureManager textureManager) {
super(textureManager, CustomShaderLoader::onShaderError);
}

@Override
public PostEffectProcessor loadPostEffect(Identifier id, Set<Identifier> availableExternalTargets) {
PostEffectProcessor result = super.loadPostEffect(id, availableExternalTargets);
if(result != null) return result;

try {
return loadDefaultPostEffect(id, availableExternalTargets);
} catch (Exception e) {
ShaderReload.printShaderException(e, true);
}

return null;
}

static void onShaderError(Exception e) {
ShaderReload.tripError();
ShaderReload.printShaderException(e, false);
}

private ShaderProgram loadDefaultProgram(ShaderProgram key) throws FileNotFoundException {
return null;
}

private PostEffectProcessor loadDefaultPostEffect(Identifier id, Set<Identifier> availableExternalTargets) {
return null;
}
}
128 changes: 42 additions & 86 deletions src/main/java/suso/shaderreload/ShaderReload.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package suso.shaderreload;

import com.google.gson.JsonSyntaxException;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gl.PostEffectProcessor;
import net.minecraft.client.gl.ShaderProgram;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.resource.*;
import net.minecraft.text.Text;
import net.minecraft.util.*;
Expand All @@ -18,21 +12,17 @@
import org.lwjgl.glfw.GLFW;
import suso.shaderreload.mixin.KeyboardInvoker;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import static net.minecraft.resource.ResourceType.CLIENT_RESOURCES;

@Environment(EnvType.CLIENT)
public class ShaderReload implements ClientModInitializer {
public static final int GLFW_KEY = GLFW.GLFW_KEY_R;
public static final Logger LOGGER = LogManager.getLogger("Shader Reload");

private static final StopException STOP = new StopException();
private static boolean reloading = false;
private static boolean stopReloading = false;
private static boolean expectError = false;
private static ResourceReloader shaderLoader;

@Override
Expand All @@ -41,95 +31,61 @@ public void onInitializeClient() {
}

public static void reloadShaders() {
if (reloading) return;
var client = MinecraftClient.getInstance();
if(reloading) {
return;
}

reloading = true;
stopReloading = false;
SimpleResourceReload.start(client.getResourceManager(), List.of(shaderLoader, client.worldRenderer),
Util.getMainWorkerExecutor(), client, CompletableFuture.completedFuture(Unit.INSTANCE), false)
.whenComplete()
.whenComplete((result, throwable) -> {
reloading = false;
if (throwable == null) {
((KeyboardInvoker) client.keyboard).invokeDebugLog("debug.reload_shaders.message");
return;
}
if (throwable instanceof CompletionException ex && ex.getCause() != null) {
throwable = ex.getCause();
}
if (!(throwable instanceof StopException)) {
((KeyboardInvoker) client.keyboard).invokeDebugError("debug.reload_shaders.unknown_error");
throwable.printStackTrace();
}
});
}
expectError = false;
MinecraftClient client = MinecraftClient.getInstance();

// Print a shader exception in chat.
private static void printShaderException(Exception exception, boolean builtin) {
var client = MinecraftClient.getInstance();
var throwable = (Throwable) exception;
while (!(throwable instanceof InvalidHierarchicalFileException)) {
var cause = throwable.getCause();
if (cause != null) throwable = cause;
else {
var translationKey = "debug.reload_shaders.unknown_error" + (builtin ? ".builtin" : "");
((KeyboardInvoker) client.keyboard).invokeDebugError(translationKey);
throwable.printStackTrace();
SimpleResourceReload.start(client.getResourceManager(), List.of(shaderLoader, client.worldRenderer), Util.getMainWorkerExecutor(), client, CompletableFuture.completedFuture(Unit.INSTANCE), false).whenComplete().whenComplete((result, throwable) -> {
reloading = false;
if(throwable == null) {
((KeyboardInvoker) client.keyboard).debugLog(Text.translatable("debug.reload_shaders.message"));
expectError = false;
return;
}
}
var translationKey = "debug.reload_shaders.error" + (builtin ? ".builtin" : "");
((KeyboardInvoker) client.keyboard).invokeDebugError(translationKey);
client.inGameHud.getChatHud().addMessage(Text.literal(throwable.getMessage()).formatted(Formatting.GRAY));
}

// Try loading a core shader; if it fails, stop shader reloading or try loading a built-in core shader.
public static ShaderProgram onLoadShaders$new(ResourceFactory factory, String name, VertexFormat format) throws IOException {
try {
return new ShaderProgram(factory, name, format);
} catch (IOException e) {
printShaderException(e, false);
if (reloading) throw STOP;
}
try {
var defaultPack = MinecraftClient.getInstance().getDefaultResourcePack();
return new ShaderProgram(defaultPack.getFactory(), name, format);
} catch (IOException e) {
printShaderException(e, true);
throw e;
}
if(throwable instanceof CompletionException ex && ex.getCause() != null) {
throwable = ex.getCause();
}

if(!expectError) {
((KeyboardInvoker) client.keyboard).debugLog(Text.translatable("debug.reload_shaders.unknown_error"));
LOGGER.error(throwable);
}

expectError = false;
});
}

// Try loading a shader effect; if it fails, request stopping and try loading a built-in shader effect.
@SuppressWarnings("resource")
public static PostEffectProcessor onLoadShader$new(TextureManager textureManager, ResourceFactory resourceFactory,
Framebuffer framebuffer, Identifier location) throws IOException {
try {
return new PostEffectProcessor(textureManager, resourceFactory, framebuffer, location);
} catch (IOException | JsonSyntaxException e) {
printShaderException(e, false);
stopReloading = true;
}
try {
var defaultPack = MinecraftClient.getInstance().getDefaultResourcePack();
resourceFactory = new LifecycledResourceManagerImpl(CLIENT_RESOURCES, List.of(defaultPack));
return new PostEffectProcessor(textureManager, resourceFactory, framebuffer, location);
} catch (IOException | JsonSyntaxException e) {
printShaderException(e, true);
throw e;
// Print a shader exception in chat.
public static void printShaderException(Exception exception, boolean builtin) {
MinecraftClient client = MinecraftClient.getInstance();
Throwable throwable = exception;
while (!(throwable instanceof InvalidHierarchicalFileException)) {
Throwable cause = throwable.getCause();
if(cause != null) {
throwable = cause;
} else {
String translationKey = "debug.reload_shaders.unknown_error" + (builtin ? ".builtin" : "");
((KeyboardInvoker) client.keyboard).debugLog(Text.translatable(translationKey));
LOGGER.error(throwable);
return;
}
}
}

// Stop shader reloading if it's requested.
public static void onLoadShader$end() {
if (reloading && stopReloading) throw STOP;
String translationKey = "debug.reload_shaders.error" + (builtin ? ".builtin" : "");
((KeyboardInvoker) client.keyboard).debugLog(Text.translatable(translationKey));
client.inGameHud.getChatHud().addMessage(Text.literal(throwable.getMessage()).formatted(Formatting.GRAY));
}

public static void setShaderLoader(ResourceReloader value) {
shaderLoader = value;
}

private static class StopException extends RuntimeException {
private StopException() {}
public static void tripError() {
expectError = true;
}
}
57 changes: 0 additions & 57 deletions src/main/java/suso/shaderreload/mixin/GameRendererMixin.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/suso/shaderreload/mixin/GlProgramManagerMixin.java

This file was deleted.

12 changes: 6 additions & 6 deletions src/main/java/suso/shaderreload/mixin/KeyboardInvoker.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package suso.shaderreload.mixin;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Keyboard;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(Keyboard.class)
@Mixin(Keyboard.class) @Environment(EnvType.CLIENT)
public interface KeyboardInvoker {
@Invoker
void invokeDebugLog(String key, Object ... args);

@Invoker
void invokeDebugError(String key, Object ... args);
@Invoker("debugLog")
void debugLog(Text text);
}
Loading