Skip to content

Commit

Permalink
fix texture3D and array expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Oct 24, 2024
1 parent 1e24b38 commit 0510f4b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/main/antlr/GLSLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ type_specifier_nonarray
| IIMAGE2DMSARRAY
| UIMAGE2DMSARRAY
| TEXTURE2D // embeddedt: make texture2D a valid type name for now, so texture2D(t, v) is valid input
| TEXTURE3D // ferriarnus: make texture3D a valid type name for now, so texture3D(t, v) is valid input
| struct_specifier
| type_name
;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/taumc/glsl/ExpressionRenamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public void enterFunction_prototype(GLSLParser.Function_prototypeContext ctx) {
public void enterType_specifier_nonarray(GLSLParser.Type_specifier_nonarrayContext ctx) {
if (ctx.TEXTURE2D() != null) {
handleIdentifier(ctx.TEXTURE2D());
} else if (ctx.TEXTURE3D() != null) {
handleIdentifier(ctx.TEXTURE3D());
}
}
}
15 changes: 0 additions & 15 deletions src/main/java/org/taumc/glsl/FunctionCallWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ public FunctionCallWrapper(String name, String code) {
this.code = code;
}

// @Override
// public void enterFunction_call_generic(GLSLParser.Function_call_genericContext ctx) {
// String function = ctx.getText();
// if (function.startsWith(name + "(")) {
// function = code + "(" + function + ")";
// GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(function));
// GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer));
// var def = parser.function_call_generic();
// var parent = ctx.getParent();
// def.setParent(parent);
// parent.children.set(parent.children.indexOf(ctx), def);
// }
// }


@Override
public void enterPostfix_expression(GLSLParser.Postfix_expressionContext ctx) {
String function = ctx.getText();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/taumc/glsl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void main(String[] args) throws Exception {
Util.removeUnusedFunctions(translationUnit);
Util.rewriteStructArrays(translationUnit);
Util.renameFunctionCall(translationUnit, "texture2D", "texture");
Util.replaceExpression(translationUnit, "gl_TextureMatrix[0]", "mat4(1.0f)");

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

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/taumc/glsl/ReplaceExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public void enterBinary_expression(GLSLParser.Binary_expressionContext ctx) {
expr.parent = ctx.getParent();
int i = ctx.getParent().children.indexOf(ctx);
ctx.getParent().children.set(i, expr);
} else if (ctx.getText().startsWith(oldExpression.getText())) {
if (ctx.unary_expression() != null) {
if (ctx.unary_expression().postfix_expression() != null) {
if (ctx.unary_expression().postfix_expression().postfix_expression() != null) {
if (ctx.unary_expression().postfix_expression().postfix_expression().getText().equals(oldExpression.getText())) {
GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(newExpression));
GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer));
var expr = parser.postfix_expression();
expr.parent = ctx.unary_expression().postfix_expression();
int i = ctx.unary_expression().postfix_expression().children.indexOf(ctx.unary_expression().postfix_expression().postfix_expression());
ctx.unary_expression().postfix_expression().children.set(i, expr);
}
}

}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void main() {
}
const float test = _vert_init(vec2(1,1));

if (vartest > test) {
if (vartest > gl_TextureMatrix[0][3]) {
test = 4;
}

Expand Down

0 comments on commit 0510f4b

Please sign in to comment.