Skip to content

Commit

Permalink
Merge pull request #2 from TauMC/caching
Browse files Browse the repository at this point in the history
Add caching
  • Loading branch information
ferriarnus authored Nov 3, 2024
2 parents f84d6b9 + 77854d0 commit e3cb096
Show file tree
Hide file tree
Showing 6 changed files with 804 additions and 91 deletions.
40 changes: 40 additions & 0 deletions src/main/java/org/taumc/glsl/InjectorPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.taumc.glsl;

import org.taumc.glsl.grammar.GLSLParser;
import org.taumc.glsl.grammar.GLSLParserBaseListener;

import java.util.concurrent.atomic.AtomicReference;

public class InjectorPoint extends GLSLParserBaseListener {

private final Transformer transformer;
private final boolean function;

public InjectorPoint(Transformer transformer, boolean function) {
this.transformer = transformer;
this.function = function;
}

@Override
public void enterFunction_definition(GLSLParser.Function_definitionContext ctx) {
if (ctx.getParent() instanceof GLSLParser.External_declarationContext list) {
transformer.function = list;
transformer.variable = list;
}
}

@Override
public void enterStorage_qualifier(GLSLParser.Storage_qualifierContext ctx) {
if (function) {
return;
}
var parent = ctx.getParent();
while (!(parent instanceof GLSLParser.External_declarationContext list)) {
if (parent.getParent() == null) {
return;
}
parent = parent.getParent();
}
transformer.variable = list;
}
}
28 changes: 15 additions & 13 deletions src/main/java/org/taumc/glsl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ public static void main(String[] args) throws Exception {
parser.setBuildParseTree(true);
var translationUnit = parser.translation_unit();

Util.injectVariable(translationUnit, "in ivec2 a_LightCoord2;");
Util.rename(translationUnit, "a_Color", "rewritten_Color");
Transformer transformer = new Transformer(translationUnit);

transformer.injectVariable("in ivec2 a_LightCoord2;");
transformer.rename("a_Color", "rewritten_Color");
Set<Integer> found = new HashSet<>();
Util.renameArray(translationUnit, "test", "replaced", found);
transformer.renameArray("test", "replaced", found);
//Util.removeVariable(translationUnit, "_vert_tex_diffuse_coord");
Util.removeVariable(translationUnit, "testing");
Util.removeConstAssignment(translationUnit);
Util.renameFunctionCall(translationUnit, "_vert_init", "newCall");
Util.renameAndWrapShadow(translationUnit, "function", "wrapped");
Util.prependMain(translationUnit, "injected = 5;");
Util.replaceExpression(translationUnit, "vartest", "unint(5)");
Util.removeUnusedFunctions(translationUnit);
Util.rewriteStructArrays(translationUnit);
Util.renameFunctionCall(translationUnit, "texture2D", "texture");
Util.replaceExpression(translationUnit, "gl_TextureMatrix[0]", "mat4(1.0f)");
transformer.removeVariable("testing");
transformer.removeConstAssignment();
transformer.renameFunctionCall("_vert_init", "newCall");
transformer.renameAndWrapShadow("function", "wrapped");
transformer.prependMain("injected = 5;");
transformer.replaceExpression("vartest", "unint(5)");
transformer.removeUnusedFunctions();
transformer.rewriteStructArrays();
transformer.renameFunctionCall("texture2D", "texture");
transformer.replaceExpression("gl_TextureMatrix[0]", "mat4(1.0f)");

System.out.println(getFormattedShader(translationUnit));

Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/taumc/glsl/ShaderParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.taumc.glsl;

import com.github.bsideup.jabel.Desugar;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.BufferedTokenStream;
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.taumc.glsl.grammar.GLSLLexer;
import org.taumc.glsl.grammar.GLSLParser;
import org.taumc.glsl.grammar.GLSLPreParser;

import java.util.function.Function;

public class ShaderParser {
@Desugar
public record ParsedShader(GLSLPreParser.Translation_unitContext pre, GLSLParser.Translation_unitContext full) {}

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 ParsedShader tryParse(GLSLLexer lexer, GLSLPreParser preParser, GLSLParser parser) {
var pre = preParser.translation_unit();
lexer.reset();
parser.reset();
var full = parser.translation_unit();
return new ParsedShader(pre, full);
}

public static ParsedShader parseShader(String shader) {
GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(shader));
GLSLPreParser preParser = new GLSLPreParser(new BufferedTokenStream(lexer));
GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer));
preParser.setBuildParseTree(true);
parser.setBuildParseTree(true);
configureNoError(parser);
configureNoError(preParser);
try {
return tryParse(lexer, preParser, parser);
} catch (Exception e) {
lexer.reset();
parser.reset();
preParser.reset();
configureError(parser);
configureError(preParser);
return tryParse(lexer, preParser, parser);
}
}

public static <T> T parseSnippet(String codeSnippet, Function<GLSLParser, T> parseLogic) {
GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(codeSnippet));
GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer));
parser.setBuildParseTree(true);
configureNoError(parser);
try {
return parseLogic.apply(parser);
} catch (Exception e) {
lexer.reset();
parser.reset();
configureError(parser);
return parseLogic.apply(parser);
}
}
}
Loading

0 comments on commit e3cb096

Please sign in to comment.