Skip to content

Commit

Permalink
Fixed some shaders failing to transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot5836 committed Nov 26, 2024
1 parent 4fe3200 commit c1886e2
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public String modify(Context ctx, String source) throws IOException {

// Inject Normal passthrough into vertex and fragment shaders
if (type == DynamicBufferType.NORMAL && !markers.containsKey("veil:" + DynamicBufferType.NORMAL.getName())) {
// Inject a normal output into the particle fragment shader
if (fragmentShader && "particle".equals(ctx.shaderInstance())) {
// Inject a normal output into the particle, lead, and text fragment shaders
if (fragmentShader && ("particle".equals(ctx.shaderInstance()) || "rendertype_leash".equals(ctx.shaderInstance()) || "rendertype_text".equals(ctx.shaderInstance()))) {
tree.add(GlslInjectionPoint.BEFORE_MAIN, GlslParser.parseExpression(output));
mainFunctionBody.add(new GlslAssignmentNode(new GlslVariableNode(type.getSourceName()), GlslParser.parseExpression("vec4(0.0, 0.0, 1.0, 1.0)"), GlslAssignmentNode.Operand.EQUAL));
modified = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void inject(GlslTree tree, VeilJobParameters parameters) throws GlslSynta
return new IOException("Unknown function with " + paramCount + " parameters: " + name);
});

GlslNode insert = new GlslCompoundNode(GlslParser.parseExpressionList(this.fillPlaceholders(function.code())));
GlslNode insert = GlslNode.compound(GlslParser.parseExpressionList(this.fillPlaceholders(function.code())));
if (function.head()) {
body.add(0, insert);
} else {
Expand Down
6 changes: 3 additions & 3 deletions common/src/main/java/foundry/veil/impl/glsl/GlslLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ public enum TokenType {

// TYPE_NAME ??
FLOATING_CONSTANT("(?:(?:\\d+\\.\\d+|\\d+\\.|\\.\\d+)(?:[eE][+-]?\\d+)?(?:f|F|lf|LF)?)|(?:\\d+)(?:\\.|[eE][+-]?\\d+)(?:f|F|lf|LF)?"),
UINTEGER_HEXADECIMAL_CONSTANT("0[xX][0-9a-fA-F]*[uU]?"),
UINTEGER_OCTAL_CONSTANT("0[0-7]*[uU]?"),
UINTEGER_DECIMAL_CONSTANT("[1-9][\\d]*[uU]?"),
UINTEGER_HEXADECIMAL_CONSTANT("0[xX][0-9a-fA-F]*[uU]"),
UINTEGER_OCTAL_CONSTANT("0[0-7]*[uU]"),
UINTEGER_DECIMAL_CONSTANT("[1-9][\\d]*[uU]"),
INTEGER_HEXADECIMAL_CONSTANT("0[xX][0-9a-fA-F]*"),
INTEGER_OCTAL_CONSTANT("0[0-7]*"),
INTEGER_DECIMAL_CONSTANT("[1-9][\\d]*"),
Expand Down
185 changes: 127 additions & 58 deletions common/src/main/java/foundry/veil/impl/glsl/GlslParser.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ public String toString() {
}

public String getSourceString() {
return this.returnType.getSourceString() + ' ' + this.name + '(' +
return this.returnType.getSourceString() + this.returnType.getPostSourceString() + ' ' + this.name + '(' +
this.parameters.stream().map(parameter -> {
String name = parameter.getName();
GlslSpecifiedType type = parameter.getType();
if (name != null) {
return parameter.getType().getSourceString() + " " + name;
return type.getSourceString() + " " + name + type.getPostSourceString();
}
return parameter.getType().getSourceString();
return type.getSourceString();
}).collect(Collectors.joining(", ")) + ')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public String toString() {

@Override
public String getSourceString() {
return "case: " + this.condition.getSourceString();
return this.condition == null ? "default" : "case " + this.condition.getSourceString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public String getSourceString() {
StringBuilder builder = new StringBuilder("switch(");
builder.append(this.condition.getSourceString()).append(") {");
for (GlslNode branch : this.branches) {
builder.append('\t').append(branch.getSourceString()).append('\n');
builder.append('\t').append(branch.getSourceString());
builder.append(branch instanceof GlslCaseLabelNode ? ':' : ';');
builder.append('\n');
}
builder.append('}');
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public GlslAndNode setExpressions(GlslNode... expressions) {

@Override
public String getSourceString() {
return this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" & "));
return '(' + this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" & ")) + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public GlslConditionalNode setSecond(GlslNode second) {

@Override
public String getSourceString() {
return this.condition.getSourceString() + " ? " + this.first.getSourceString() + " : " + this.second.getSourceString();
return '(' + this.condition.getSourceString() + " ? " + this.first.getSourceString() + " : " + this.second.getSourceString() + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<GlslNode> getExpressions() {

@Override
public String getSourceString() {
return this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" ^ "));
return '(' + this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" ^ ")) + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<GlslNode> getExpressions() {

@Override
public String getSourceString() {
return this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" | "));
return '(' + this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" | ")) + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<GlslNode> getExpressions() {

@Override
public String getSourceString() {
return this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" && "));
return '(' + this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" && ")) + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<GlslNode> getExpressions() {

@Override
public String getSourceString() {
return this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" || "));
return '(' + this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" || ")) + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<GlslNode> getExpressions() {

@Override
public String getSourceString() {
return this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" ^^ "));
return '(' + this.expressions.stream().map(GlslNode::getSourceString).collect(Collectors.joining(" ^^ ")) + ')';
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public boolean isNumber() {
@Override
public String getSourceString() {
return switch (this.format) {
case HEXADECIMAL -> "0x" + Integer.toHexString(this.value);
case OCTAL -> Integer.toOctalString(this.value);
case DECIMAL -> Integer.toString(this.value);
case HEXADECIMAL -> "0x" + Integer.toHexString(this.value) + (this.signed ? "" : "u");
case OCTAL -> Integer.toOctalString(this.value) + (this.signed ? "" : "u");
case DECIMAL -> this.value + (this.signed ? "" : "u");
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public GlslSpecifiedType getType() {

@Override
public Stream<GlslNode> stream() {
return Stream.concat(Stream.of(this), this.initializer.stream());
return this.initializer != null ? Stream.concat(Stream.of(this), this.initializer.stream()) : Stream.of(this);
}

public @Nullable String getName() {
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/resources/veil.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "foundry.veil.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"refmap": "${mod_id}.refmap.json",
"mixins": [
"debug.SimpleReloadInstanceMixin",
Expand Down
30 changes: 30 additions & 0 deletions common/src/test/java/GlslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,34 @@ void main() {
tree.visit(writer);
System.out.println(writer);
}

@Test
void testWeird() throws GlslSyntaxException {
GlslTree tree = GlslParser.parse("""
void main() {
int m0=x%2,m=m0+1;
}
""");

GlslStringWriter writer = new GlslStringWriter();
tree.visit(writer);
System.out.println(writer);
}

@Test
void testMatrix() throws GlslSyntaxException {
GlslTree tree = GlslParser.parse("""
void main() {
if (ProjMat[2][3] != 0.) {
shadeColor = vec4(0.);
vertexColor = vec4(-1.);
lightMapColor = vec4(1.);
}
}
""");

GlslStringWriter writer = new GlslStringWriter();
tree.visit(writer);
System.out.println(writer);
}
}
2 changes: 1 addition & 1 deletion fabric/src/main/resources/veil.fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "foundry.veil.fabric.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"refmap": "${mod_id}.refmap.json",
"plugin": "foundry.veil.fabric.VeilMixinPlugin",
"mixins": [
Expand Down
2 changes: 1 addition & 1 deletion neoforge/src/main/resources/veil.neoforge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"required": true,
"minVersion": "0.8",
"package": "foundry.veil.forge.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"refmap": "${mod_id}.refmap.json",
"plugin": "foundry.veil.forge.VeilMixinPlugin",
"mixins": [
Expand Down

0 comments on commit c1886e2

Please sign in to comment.