Skip to content

Commit

Permalink
Added dynamic buffer injection for veil shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot5836 committed Nov 23, 2024
1 parent cfc8062 commit ef017fc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import foundry.veil.impl.glsl.GlslInjectionPoint;
import foundry.veil.impl.glsl.GlslParser;
import foundry.veil.impl.glsl.GlslSyntaxException;
import foundry.veil.impl.glsl.grammar.GlslSpecifiedType;
import foundry.veil.impl.glsl.grammar.GlslTypeQualifier;
import foundry.veil.impl.glsl.node.GlslNode;
import foundry.veil.impl.glsl.node.GlslTree;
import foundry.veil.impl.glsl.node.function.GlslFunctionNode;
import foundry.veil.impl.glsl.node.variable.GlslNewNode;
import foundry.veil.impl.glsl.visitor.GlslStringWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.lwjgl.opengl.GL20C.GL_FRAGMENT_SHADER;
Expand Down Expand Up @@ -48,11 +52,58 @@ public String modify(Context ctx, String source) throws IOException {
}

if (modified) {
List<GlslNewNode> outputs = new ArrayList<>();
tree.fields().forEach(node -> {
GlslSpecifiedType type = node.getType();
boolean valid = false;
for (GlslTypeQualifier qualifier : type.getQualifiers()) {
if (qualifier == GlslTypeQualifier.StorageType.OUT) {
valid = true;
break;
}
}

if (!valid) {
return;
}

for (GlslTypeQualifier qualifier : type.getQualifiers()) {
if (qualifier instanceof GlslTypeQualifier.Layout(List<GlslTypeQualifier.LayoutId> layoutIds)) {
for (GlslTypeQualifier.LayoutId layoutId : layoutIds) {
if ("location".equals(layoutId.identifier())) {
GlslNode expression = layoutId.expression();
if (expression != null) {
try {
int location = Integer.parseInt(expression.getSourceString());
if (location == 0) {
outputs.clear();
return;
}
valid = false;
break;
} catch (NumberFormatException ignored) {
}
}
}
}
}
}

if (valid) {
outputs.add(node);
}
});

for (GlslNewNode output : outputs) {
output.getType().addLayoutId("location", GlslNode.intConstant(0));
}

GlslStringWriter writer = new GlslStringWriter();
tree.visit(writer);
return writer.toString();
}
} catch (GlslSyntaxException e) {
} catch (
GlslSyntaxException e) {
Veil.LOGGER.error("Failed to transform shader: {}", ctx.name(), e);
}
return source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ private void clearShader() {
}

private void link() throws ShaderException {
this.uniforms.clear();
this.uniformBlocks.clear();
this.textures.clear();

glLinkProgram(this.program);
if (glGetProgrami(this.program, GL_LINK_STATUS) != GL_TRUE) {
String log = glGetProgramInfoLog(this.program);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ private static List<GlslParameterDeclaration> parseParameterList(GlslTokenReader
return null;
}

return GlslTypeQualifier.layout(layoutQualifierIds.toArray(GlslTypeQualifier.LayoutId[]::new));
return GlslTypeQualifier.layout(layoutQualifierIds);
}

private static @Nullable List<GlslTypeQualifier> parseTypeQualifiers(GlslTokenReader reader) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package foundry.veil.impl.glsl.grammar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import foundry.veil.impl.glsl.node.GlslNode;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
* Specifies the full operand of something in GLSL in addition to all qualifiers.
Expand Down Expand Up @@ -35,6 +35,24 @@ public GlslTypeSpecifier getSpecifier() {
return this.specifier;
}

/**
* Adds a layout id to the qualifier list, or adds to an existing layout.
*
* @param identifier The name of the identifier
* @param expression The value to assign it to
*/
public GlslSpecifiedType addLayoutId(String identifier, @Nullable GlslNode expression) {
for (GlslTypeQualifier qualifier : this.qualifiers) {
if (qualifier instanceof GlslTypeQualifier.Layout(List<GlslTypeQualifier.LayoutId> layoutIds)) {
layoutIds.add(new GlslTypeQualifier.LayoutId(identifier, expression));
return this;
}
}

this.qualifiers.addFirst(GlslTypeQualifier.layout(Collections.singleton(new GlslTypeQualifier.LayoutId(identifier, expression))));
return this;
}

/**
* @return The qualifiers applied to it, for example <code>layout()</code> or <code>flat</code>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import foundry.veil.impl.glsl.node.GlslNode;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Locale;
import java.util.*;

public sealed interface GlslTypeQualifier {

Expand All @@ -14,8 +13,8 @@ static GlslTypeQualifier storage(String[] typeNames) {
return new StorageSubroutine(typeNames);
}

static Layout layout(LayoutId... ids) {
return new Layout(ids);
static Layout layout(Collection<LayoutId> ids) {
return new Layout(new ArrayList<>(ids));
}

static LayoutId identifierLayoutId(String identifier, @Nullable GlslNode constantExpression) {
Expand Down Expand Up @@ -48,7 +47,7 @@ public String getSourceString() {
}
}

record Layout(LayoutId[] layoutIds) implements GlslTypeQualifier {
record Layout(List<LayoutId> layoutIds) implements GlslTypeQualifier {
@Override
public String getSourceString() {
StringBuilder builder = new StringBuilder();
Expand Down

0 comments on commit ef017fc

Please sign in to comment.