Skip to content

Commit

Permalink
Better ANTLR error handling for GLSL transformations (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cleptomania authored Nov 2, 2024
1 parent d180023 commit ffa1193
Showing 1 changed file with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import net.coderbot.iris.gl.shader.ShaderType;
import net.coderbot.iris.pipeline.transform.parameter.Parameters;
import net.coderbot.iris.pipeline.transform.parameter.AttributeParameters;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ConsoleErrorListener;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.taumc.glsl.Util;
Expand Down Expand Up @@ -130,6 +135,18 @@ public static <P extends Parameters> Map<PatchShaderType, String> transform(Stri
}
}

private static void configureNoError(Parser parser) {
parser.setErrorHandler(new BailErrorStrategy());
parser.removeErrorListeners();
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
}

private static void configureError(Parser parser) {
parser.setErrorHandler(new DefaultErrorStrategy());
parser.addErrorListener(ConsoleErrorListener.INSTANCE);
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
}

private static <P extends Parameters> Map<PatchShaderType, String> transformInternal(EnumMap<PatchShaderType, String> inputs, Patch patchType, P parameters) {
EnumMap<PatchShaderType, String> result = new EnumMap<>(PatchShaderType.class);
EnumMap<PatchShaderType, GLSLParser.Translation_unitContext> types = new EnumMap<>(PatchShaderType.class);
Expand Down Expand Up @@ -186,28 +203,22 @@ private static <P extends Parameters> Map<PatchShaderType, String> transformInte
GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(input));
GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer));
parser.setBuildParseTree(true);
var translationUnit = parser.translation_unit();

switch(patchType) {
case SODIUM_TERRAIN:
SodiumTransformer.transform(translationUnit, parameters);
break;
case COMPOSITE:
CompositeDepthTransformer.transform(translationUnit);
break;
case ATTRIBUTES:
AttributeTransformer.transform(translationUnit, (AttributeParameters) parameters, profile, versionInt);
break;
default:
throw new IllegalStateException("Unknown patch type: " + patchType.name());
configureNoError(parser);

GLSLParser.Translation_unitContext translationUnit;
try {
translationUnit = doTransform(parser, patchType, parameters, profile, versionInt);
} catch (Exception e) {
lexer.reset();
parser.reset();
configureError(parser);
translationUnit = doTransform(parser, patchType, parameters, profile, versionInt);
}

// Check if we need to patch in texture LOD extension enabling
if (versionInt <= 120 && (Util.containsCall(translationUnit, "texture2DLod") || Util.containsCall(translationUnit, "texture3DLod"))) {
textureLodExtensionPatches.add(translationUnit);
}

CompatibilityTransformer.transformEach(translationUnit, parameters);
types.put(type, translationUnit);
prepatched.put(type, profileString);
}
Expand All @@ -230,6 +241,25 @@ private static <P extends Parameters> Map<PatchShaderType, String> transformInte
return result;
}

private static GLSLParser.Translation_unitContext doTransform(GLSLParser parser, Patch patchType, Parameters parameters, String profile, int versionInt) {
GLSLParser.Translation_unitContext translationUnit = parser.translation_unit();
switch (patchType) {
case SODIUM_TERRAIN:
SodiumTransformer.transform(translationUnit, parameters);
break;
case COMPOSITE:
CompositeDepthTransformer.transform(translationUnit);
break;
case ATTRIBUTES:
AttributeTransformer.transform(translationUnit, (AttributeParameters) parameters, profile, versionInt);
break;
default:
throw new IllegalStateException("Unknown patch type: " + patchType.name());
}
CompatibilityTransformer.transformEach(translationUnit, parameters);
return translationUnit;
}

public static void applyIntelHd4000Workaround(GLSLParser.Translation_unitContext translationUnit) {
Util.renameFunctionCall(translationUnit, "ftransform", "iris_ftransform");
}
Expand Down

0 comments on commit ffa1193

Please sign in to comment.