Skip to content

Commit

Permalink
Add initial "shader bypass" functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 23, 2025
1 parent ded7d69 commit 6541195
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {
}

val MINECRAFT_VERSION by extra { "1.21.1" }
val NEOFORGE_VERSION by extra { "21.1.58" }
val NEOFORGE_VERSION by extra { "21.1.91" }
val FABRIC_LOADER_VERSION by extra { "0.16.10" }
val FABRIC_API_VERSION by extra { "0.103.0+1.21.1" }

val SODIUM_DEPENDENCY_FABRIC by extra { files(rootDir.resolve("custom_sodium").resolve("sodium-fabric-0.6.7-snapshot+mc1.21.1-local.jar"))}
val SODIUM_DEPENDENCY_NEO by extra { "maven.modrinth:sodium:mc1.21.1-0.6.1-neoforge" }
val SODIUM_DEPENDENCY_NEO by extra { files(rootDir.resolve("custom_sodium").resolve("sodium-neoforge-0.6.7-snapshot+mc1.21.1-local.jar"))}

// This value can be set to null to disable Parchment.
// TODO: Re-add Parchment
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/net/irisshaders/iris/Iris.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ public void onEarlyInitialize() {
logger.warn("", e);
}

irisConfig = new IrisConfig(IrisPlatformHelpers.getInstance().getConfigDir().resolve("iris.properties"));
irisConfig = new IrisConfig(IrisPlatformHelpers.getInstance().getConfigDir().resolve("iris.properties"), IrisPlatformHelpers.getInstance().getConfigDir().resolve("iris-excluded.json"));

try {
irisConfig.initialize();
Expand Down
52 changes: 51 additions & 1 deletion common/src/main/java/net/irisshaders/iris/config/IrisConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package net.irisshaders.iris.config;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.gui.option.IrisVideoSettings;
import net.irisshaders.iris.pathways.colorspace.ColorSpace;
import net.minecraft.resources.ResourceLocation;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;

Expand All @@ -19,6 +27,7 @@ public class IrisConfig {
private static final String COMMENT =
"This file stores configuration options for Iris, such as the currently active shaderpack";
private final Path propertiesPath;
private final Path excludedPath;
/**
* The path to the current shaderpack. Null if the internal shaderpack is being used.
*/
Expand All @@ -27,21 +36,31 @@ public class IrisConfig {
* Whether or not shaders are used for rendering. False to disable all shader-based rendering, true to enable it.
*/
private boolean enableShaders;
/**
* Whether or not to allow core shaders to draw to the main color texture.
*/
private boolean allowUnknownShaders;
/**
* If debug features should be enabled. Gives much more detailed OpenGL error outputs at the cost of performance.
*/
private boolean enableDebugOptions;
/**
* What shaders should be nuked.
*/
private List<ResourceLocation> shadersToSkip = new ArrayList<>();
/**
* If the update notification should be disabled or not.
*/
private boolean disableUpdateMessage;

public IrisConfig(Path propertiesPath) {
public IrisConfig(Path propertiesPath, Path excluded) {
shaderPackName = null;
enableShaders = true;
allowUnknownShaders = false;
enableDebugOptions = false;
disableUpdateMessage = false;
this.propertiesPath = propertiesPath;
this.excludedPath = excluded;
}

/**
Expand Down Expand Up @@ -113,13 +132,33 @@ public void setShadersEnabled(boolean enabled) {
this.enableShaders = enabled;
}

private static Gson GSON = new Gson();

/**
* loads the config file and then populates the string, int, and boolean entries with the parsed entries
*
* @throws IOException if the file cannot be loaded
*/

public void load() throws IOException {
if (Files.exists(excludedPath)) {
JsonArray json = JsonParser.parseString(Files.readString(excludedPath)).getAsJsonObject().getAsJsonArray("excluded");
for (int i = 0; i < json.size(); i++) {
ResourceLocation resource = ResourceLocation.tryParse(json.get(i).getAsString());
if (resource == null) {
Iris.logger.warn("Unknown shader " + json.get(i).getAsString());
}

shadersToSkip.add(resource);
}
} else {
JsonObject defaultV = new JsonObject();
JsonArray array = new JsonArray();
array.add("put:valuesHere");
defaultV.add("excluded", array);
Files.writeString(excludedPath, GSON.toJson(defaultV));
}

if (!Files.exists(propertiesPath)) {
return;
}
Expand All @@ -129,8 +168,10 @@ public void load() throws IOException {
try (InputStream is = Files.newInputStream(propertiesPath)) {
properties.load(is);
}

shaderPackName = properties.getProperty("shaderPack");
enableShaders = !"false".equals(properties.getProperty("enableShaders"));
allowUnknownShaders = "true".equals(properties.getProperty("allowUnknownShaders"));
enableDebugOptions = "true".equals(properties.getProperty("enableDebugOptions"));
disableUpdateMessage = "true".equals(properties.getProperty("disableUpdateMessage"));
try {
Expand Down Expand Up @@ -159,6 +200,7 @@ public void save() throws IOException {
Properties properties = new Properties();
properties.setProperty("shaderPack", getShaderPackName().orElse(""));
properties.setProperty("enableShaders", enableShaders ? "true" : "false");
properties.setProperty("allowUnknownShaders", allowUnknownShaders ? "true" : "false");
properties.setProperty("enableDebugOptions", enableDebugOptions ? "true" : "false");
properties.setProperty("disableUpdateMessage", disableUpdateMessage ? "true" : "false");
properties.setProperty("maxShadowRenderDistance", String.valueOf(IrisVideoSettings.shadowDistance));
Expand All @@ -168,4 +210,12 @@ public void save() throws IOException {
properties.store(os, COMMENT);
}
}

public boolean shouldAllowUnknownShaders() {
return allowUnknownShaders;
}

public boolean shouldSkip(ResourceLocation value) {
return shadersToSkip.contains(value); // TODO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
import com.mojang.blaze3d.shaders.Program;
import com.mojang.blaze3d.shaders.Uniform;
import com.mojang.blaze3d.vertex.VertexFormat;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.gl.GLDebug;
import net.irisshaders.iris.gl.blending.DepthColorStorage;
import net.irisshaders.iris.mixinterface.ShaderInstanceInterface;
import net.irisshaders.iris.pipeline.IrisRenderingPipeline;
import net.irisshaders.iris.pipeline.ShaderRenderingPipeline;
import net.irisshaders.iris.pipeline.WorldRenderingPipeline;
import net.irisshaders.iris.pipeline.programs.ExtendedShader;
import net.irisshaders.iris.pipeline.programs.FallbackShader;
import net.irisshaders.iris.shadows.ShadowRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceProvider;
import org.lwjgl.opengl.KHRDebug;
import org.slf4j.Logger;
Expand All @@ -25,6 +30,11 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Map;

@Mixin(ShaderInstance.class)
public abstract class MixinShaderInstance implements ShaderInstanceInterface {
@Unique
Expand All @@ -41,6 +51,57 @@ public abstract class MixinShaderInstance implements ShaderInstanceInterface {
@Final
private Program fragmentProgram;

@Unique
private static final MethodHandle NONE = MethodHandles.constant(Integer.class, 2);

@Unique
private static final MethodHandle ALWAYS = MethodHandles.constant(Integer.class, 1);

@Unique
private MethodHandle shouldSkip;

private static Map<Class<?>, MethodHandle> shouldSkipList = new Object2ObjectOpenHashMap<>();

static {
shouldSkipList.put(ExtendedShader.class, NONE);
shouldSkipList.put(FallbackShader.class, NONE);
}

@Inject(method = "<init>(Lnet/minecraft/server/packs/resources/ResourceProvider;Lnet/minecraft/resources/ResourceLocation;Lcom/mojang/blaze3d/vertex/VertexFormat;)V", at = @At("TAIL"), require = 0)
private void iriss$storeSkip(ResourceProvider resourceProvider, ResourceLocation string, VertexFormat vertexFormat, CallbackInfo ci) {
shouldSkip = shouldSkipList.computeIfAbsent(getClass(), x -> {
try {
MethodHandle iris$skipDraw = MethodHandles.lookup().findVirtual(x, "iris$skipDraw", MethodType.methodType(boolean.class));
Iris.logger.warn("Class " + x.getName() + " has opted out of being rendered with shaders.");
return iris$skipDraw;
} catch (NoSuchMethodException | IllegalAccessException e) {
return NONE;
}
});


if (Iris.getIrisConfig().shouldSkip(string)) {
shouldSkip = ALWAYS;
}
}

public boolean iris$shouldSkipThis() {
if (Iris.getIrisConfig().shouldAllowUnknownShaders()) {
if (!shouldOverrideShaders()) return false;

if (shouldSkip == NONE) return false;
if (shouldSkip == ALWAYS) return true;

try {
return (boolean) shouldSkip.invoke(((ShaderInstance) (Object) this));
} catch (Throwable e) {
throw new RuntimeException(e);
}
} else {
return !(((Object) this) instanceof ExtendedShader || ((Object) this) instanceof FallbackShader || !shouldOverrideShaders());
}
}

@Unique
private static boolean shouldOverrideShaders() {
WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable();
Expand Down Expand Up @@ -91,17 +152,40 @@ private void name(ResourceProvider resourceProvider, String string, VertexFormat

@Inject(method = "apply", at = @At("TAIL"))
private void onTail(CallbackInfo ci) {
if (((Object) this) instanceof ExtendedShader || ((Object) this) instanceof FallbackShader || !shouldOverrideShaders()) {
DepthColorStorage.unlockDepthColor();
if (!iris$shouldSkipThis()) {
if (!isKnownShader() && shouldOverrideShaders()) {
WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable();

if (pipeline instanceof IrisRenderingPipeline) {
if (ShadowRenderer.ACTIVE) {
((IrisRenderingPipeline) pipeline).bindDefaultShadow();
} else {
((IrisRenderingPipeline) pipeline).bindDefault();
}
}
}

return;
}

DepthColorStorage.disableDepthColor();
}

private boolean isKnownShader() {
return ((Object) this) instanceof ExtendedShader || ((Object) this) instanceof FallbackShader;
}

@Inject(method = "clear", at = @At("HEAD"))
private void iris$unlockDepthColorState(CallbackInfo ci) {
if (((Object) this) instanceof ExtendedShader || ((Object) this) instanceof FallbackShader || !shouldOverrideShaders()) {
if (!iris$shouldSkipThis()) {
if (!isKnownShader() && shouldOverrideShaders()) {
WorldRenderingPipeline pipeline = Iris.getPipelineManager().getPipelineNullable();

if (pipeline instanceof IrisRenderingPipeline) {
Minecraft.getInstance().getMainRenderTarget().bindWrite(false);
}
}

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import net.irisshaders.iris.compat.dh.DHCompat;
import net.irisshaders.iris.compat.dh.DhFrameBufferWrapper;
import net.irisshaders.iris.features.FeatureFlags;
import net.irisshaders.iris.gl.GLDebug;
import net.irisshaders.iris.gl.IrisRenderSystem;
Expand Down Expand Up @@ -190,6 +191,9 @@ public class IrisRenderingPipeline implements WorldRenderingPipeline, ShaderRend
private int currentNormalTexture;
private int currentSpecularTexture;
private ColorSpace currentColorSpace;
private GlFramebuffer defaultFB;
private GlFramebuffer defaultFBAlt;
private GlFramebuffer defaultFBShadow;

public IrisRenderingPipeline(ProgramSet programSet) {
ShaderPrinter.resetPrintState();
Expand Down Expand Up @@ -439,6 +443,7 @@ public IrisRenderingPipeline(ProgramSet programSet) {
shadowRenderer = null;
}

defaultFBShadow = shadowRenderTargets.createFramebufferWritingToMain(new int[] {0});
} else {
this.shadowClearPasses = ImmutableList.of();
this.shadowClearPassesFull = ImmutableList.of();
Expand Down Expand Up @@ -505,6 +510,10 @@ public void process(int target) {
}

currentColorSpace = IrisVideoSettings.colorSpace;
int defaultTex = packDirectives.getFallbackTex();

defaultFB = flippedAfterPrepare.contains(defaultTex) ? renderTargets.createFramebufferWritingToAlt(new int[] { defaultTex }) : renderTargets.createFramebufferWritingToMain(new int[] { defaultTex });
defaultFBAlt = flippedAfterTranslucent.contains(defaultTex) ? renderTargets.createFramebufferWritingToAlt(new int[] { defaultTex }) : renderTargets.createFramebufferWritingToMain(new int[] { defaultTex });
}

private ComputeProgram[] createShadowComputes(ComputeSource[] compute, ProgramSet programSet) {
Expand Down Expand Up @@ -1337,4 +1346,16 @@ public boolean skipAllRendering() {
public CloudSetting getDHCloudSetting() {
return dhCloudSetting;
}

public void bindDefault() {
if (isBeforeTranslucent) {
defaultFB.bind();
} else {
defaultFBAlt.bind();
}
}

public void bindDefaultShadow() {
defaultFBShadow.bind();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PackDirectives {
private final PackRenderTargetDirectives renderTargetDirectives;
private final PackShadowDirectives shadowDirectives;
private final float drynessHalfLife;
private int fallbackTex;
private boolean supportsColorCorrection;
private int noiseTextureResolution;
private float sunPathRotation;
Expand Down Expand Up @@ -85,6 +86,7 @@ public PackDirectives(Set<Integer> supportedRenderTargets, ShaderProperties prop
frustumCulling = properties.getFrustumCulling().orElse(true);
occlusionCulling = properties.getOcclusionCulling().orElse(true);
oldLighting = properties.getOldLighting().orElse(false);
fallbackTex = properties.getFallbackTex();
supportsColorCorrection = properties.supportsColorCorrection().orElse(false);
concurrentCompute = properties.getConcurrentCompute().orElse(false);
oldHandLight = properties.getOldHandLight().orElse(true);
Expand All @@ -108,6 +110,7 @@ public PackDirectives(Set<Integer> supportedRenderTargets, ShaderProperties prop
explicitFlips = directives.explicitFlips;
scaleOverrides = directives.scaleOverrides;
prepareBeforeShadow = directives.prepareBeforeShadow;
fallbackTex = directives.fallbackTex;
particleRenderingSettings = directives.particleRenderingSettings;
textureMap = directives.textureMap;
}
Expand Down Expand Up @@ -248,6 +251,10 @@ public boolean supportsColorCorrection() {
return supportsColorCorrection;
}

public int getFallbackTex() {
return fallbackTex;
}

public void acceptDirectivesFrom(DirectiveHolder directives) {
renderTargetDirectives.acceptDirectives(directives);
shadowDirectives.acceptDirectives(directives);
Expand Down
Loading

0 comments on commit 6541195

Please sign in to comment.