From 159f7ff8cc55e1bdd2eea7d31d0a33495d192ccc Mon Sep 17 00:00:00 2001 From: Eclipse-Sol Date: Wed, 7 Oct 2026 17:59:21 -0400 Subject: [PATCH] Fix NaNs on EP (#2231) * Fix NaNs on EP * Also add hidden writes handling * optimize this mess * Another optimization pass --- .../glsm/CompatShaderTransformer.java | 8 +- .../angelica/glsm/GlslTransformUtils.java | 87 ++-- .../glsm/shader/GlslVulkanPreprocess.java | 226 ++++------ .../angelica/glsm/shader/ShaderIndex.java | 153 +++++++ .../glsm/shader/SpirvShaderTranslator.java | 21 +- .../angelica/sdlgpu/shader/ClipZRemap.java | 29 +- .../sdlgpu/shader/DerivativeHoisting.java | 398 ++++++++++++++++++ .../sdlgpu/shader/LogicOpLowering.java | 6 +- .../sdlgpu/shader/PerFrameBlockInjector.java | 58 +-- .../sdlgpu/shader/SamplerStripper.java | 124 +++--- .../angelica/sdlgpu/shader/ShaderManager.java | 47 ++- .../angelica/sdlgpu/shader/UscaledRetype.java | 12 +- .../sdlgpu/shader/DerivativeHoistingTest.java | 179 ++++++++ .../sdlgpu/shader/ShaderTransformChain.java | 11 +- .../transform/RwImageStoreExtractor.java | 8 +- .../pipeline/transform/ShaderTransformer.java | 6 - 16 files changed, 968 insertions(+), 405 deletions(-) create mode 100644 glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/ShaderIndex.java create mode 100644 sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoisting.java create mode 100644 sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoistingTest.java diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CompatShaderTransformer.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CompatShaderTransformer.java index b5ac0bb29..5b23e8898 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CompatShaderTransformer.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/CompatShaderTransformer.java @@ -4,6 +4,7 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormatElement.Usage import com.gtnewhorizons.angelica.config.SystemProperties; import com.gtnewhorizons.angelica.glsm.backend.RenderBackend; import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import com.gtnewhorizons.angelica.glsm.shader.SpirvShaderTranslator; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; @@ -456,12 +457,7 @@ public class CompatShaderTransformer { continue; } - final List declarators = new ArrayList<>(); - declarators.add(decl.typeless_declaration()); - if (decl.getParent() instanceof GLSLParser.Init_declarator_listContext list) { - declarators.addAll(list.typeless_declaration()); - } - for (GLSLParser.Typeless_declarationContext d : declarators) { + for (GLSLParser.Typeless_declarationContext d : ShaderIndex.declarators((GLSLParser.Init_declarator_listContext) decl.getParent())) { if (d.IDENTIFIER() == null) continue; final int slots = baseSlots(baseType) * typeArraySlots * arraySlots(d.array_specifier()); final String arraySuffix = typeArray + arraySuffix(d.array_specifier()); diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GlslTransformUtils.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GlslTransformUtils.java index 59b1cda49..5e5912e9d 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GlslTransformUtils.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/GlslTransformUtils.java @@ -1,21 +1,25 @@ package com.gtnewhorizons.angelica.glsm; +import org.antlr.v4.runtime.BailErrorStrategy; import org.antlr.v4.runtime.BufferedTokenStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.DefaultErrorStrategy; +import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.WritableToken; +import org.antlr.v4.runtime.atn.PredictionMode; +import org.antlr.v4.runtime.misc.ParseCancellationException; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; import org.taumc.glsl.grammar.GLSLLexer; import org.taumc.glsl.grammar.GLSLParser; import org.taumc.glsl.grammar.GLSLPreParser; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -131,72 +135,35 @@ public class GlslTransformUtils { public record QuietParse(GLSLParser.Translation_unitContext full, GLSLPreParser.Translation_unitContext pre) {} public static GLSLParser.Translation_unitContext parseFullQuiet(String source) { - final GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(source)); - lexer.removeErrorListeners(); - final GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer)); - parser.removeErrorListeners(); - parser.setBuildParseTree(true); - return parser.translation_unit(); - } - - public static GLSLPreParser.Translation_unitContext parsePreQuiet(String source) { - final GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(source)); - lexer.removeErrorListeners(); - final GLSLPreParser preParser = new GLSLPreParser(new CommonTokenStream(lexer, GLSLLexer.DIRECTIVES)); - preParser.removeErrorListeners(); - preParser.setBuildParseTree(true); - return preParser.translation_unit(); + return parseQuiet(new GLSLParser(new CommonTokenStream(quietLexer(source))), GLSLParser::translation_unit); } public static QuietParse parseBothQuiet(String source) { - final GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(source)); - lexer.removeErrorListeners(); - final GLSLPreParser preParser = new GLSLPreParser(new BufferedTokenStream(lexer)); - preParser.removeErrorListeners(); - preParser.setBuildParseTree(true); - final GLSLPreParser.Translation_unitContext pre = preParser.translation_unit(); + final GLSLLexer lexer = quietLexer(source); + final GLSLPreParser.Translation_unitContext pre = parseQuiet(new GLSLPreParser(new BufferedTokenStream(lexer)), GLSLPreParser::translation_unit); lexer.reset(); - final GLSLParser parser = new GLSLParser(new CommonTokenStream(lexer)); - parser.removeErrorListeners(); - parser.setBuildParseTree(true); - return new QuietParse(parser.translation_unit(), pre); + return new QuietParse(parseQuiet(new GLSLParser(new CommonTokenStream(lexer)), GLSLParser::translation_unit), pre); } - public static Set identifiersInDirectiveText(String source) { + private static

T parseQuiet(P parser, Function rule) { + parser.removeErrorListeners(); + parser.setErrorHandler(new BailErrorStrategy()); + parser.getInterpreter().setPredictionMode(PredictionMode.SLL); + try { + return rule.apply(parser); + } catch (ParseCancellationException e) { + // Rewinds the token stream, which keeps every token SLL already pulled from the lexer. + parser.reset(); + parser.setErrorHandler(new DefaultErrorStrategy()); + parser.getInterpreter().setPredictionMode(PredictionMode.LL); + return rule.apply(parser); + } + } + + public static GLSLLexer quietLexer(String source) { final GLSLLexer lexer = new GLSLLexer(CharStreams.fromString(source)); lexer.removeErrorListeners(); - final BufferedTokenStream tokens = new BufferedTokenStream(lexer); - tokens.fill(); - - final Set names = new HashSet<>(); - for (Token token : tokens.getTokens()) { - switch (token.getType()) { - case GLSLLexer.PROGRAM_TEXT, GLSLLexer.MACRO_TEXT, GLSLLexer.CONSTANT_EXPRESSION -> collectIdentifiers(token.getText(), names); - default -> { } - } - } - return names; - } - - private static void collectIdentifiers(String text, Set out) { - if (text == null) return; - final int len = text.length(); - int i = 0; - while (i < len) { - final char c = text.charAt(i); - if (c != '_' && !Character.isLetter(c)) { - i++; - continue; - } - int end = i + 1; - while (end < len) { - final char n = text.charAt(end); - if (n != '_' && !Character.isLetterOrDigit(n)) break; - end++; - } - out.add(text.substring(i, end)); - i = end; - } + return lexer; } public static String getFormattedShader(ParseTree tree, String header) { diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/GlslVulkanPreprocess.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/GlslVulkanPreprocess.java index ef65dd8df..1ad09429b 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/GlslVulkanPreprocess.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/GlslVulkanPreprocess.java @@ -4,17 +4,11 @@ import com.gtnewhorizons.angelica.glsm.GlslTransformUtils; import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.antlr.v4.runtime.tree.TerminalNode; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import org.lwjgl.opengl.GL20; -import org.taumc.glsl.grammar.GLSLLexer; import org.taumc.glsl.grammar.GLSLParser; -import org.taumc.glsl.grammar.GLSLPreParser; -import org.taumc.glsl.grammar.GLSLParserBaseListener; import java.util.ArrayList; import java.util.Comparator; @@ -99,7 +93,7 @@ public final class GlslVulkanPreprocess { } final List edits = new ArrayList<>(); - final Metadata meta = collectEdits(source, root, glShaderType, debugName, separateReadOnlyImages, edits); + final Metadata meta = collectEdits(new ShaderIndex(root, source), glShaderType, debugName, separateReadOnlyImages, edits); final Result out = new Result(applyEdits(source, edits), meta.boolUniforms(), meta.explicitVsInputs()); synchronized (CACHE) { @@ -111,129 +105,100 @@ public final class GlslVulkanPreprocess { public record Metadata(Set boolUniforms, Set explicitVsInputs, boolean needsSamplerless) {} - public static Metadata collectEdits(String source, GLSLParser.Translation_unitContext root, int glShaderType, String debugName, boolean separateReadOnlyImages, List edits) { + public static Metadata collectEdits(ShaderIndex index, int glShaderType, String debugName, boolean separateReadOnlyImages, List edits) { final boolean isVertex = glShaderType == GL20.GL_VERTEX_SHADER; final Set bools = new HashSet<>(); final Set explicitInputs = new HashSet<>(); final Set readOnlyImages = new HashSet<>(); - final boolean[] needsSamplerless = { false }; + boolean needsSamplerless = false; - final int[] maxExplicitLoc = { -1 }; + int maxExplicitLoc = -1; final List unlocatedVsInputStarts = new ArrayList<>(); - ParseTreeWalker.DEFAULT.walk(new GLSLParserBaseListener() { - @Override - public void enterInit_declarator_list(GLSLParser.Init_declarator_listContext ctx) { - final GLSLParser.Single_declarationContext single = ctx.single_declaration(); - if (single == null || single.fully_specified_type() == null) return; + for (GLSLParser.DeclarationContext declaration : index.globalDeclarations()) { + final GLSLParser.Init_declarator_listContext list = declaration.init_declarator_list(); + if (list == null) continue; + final GLSLParser.Single_declarationContext single = list.single_declaration(); + if (single == null || single.fully_specified_type() == null) continue; - final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); - final GLSLParser.Type_qualifierContext tq = fst.type_qualifier(); - if (tq == null) return; + final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); + final GLSLParser.Type_qualifierContext tq = fst.type_qualifier(); + if (tq == null) continue; - boolean hasUniform = false, hasIn = false, hasLocation = false, hasReadonly = false; - int locValue = -1; - int bindingValue = -1; - for (GLSLParser.Single_type_qualifierContext stq : tq.single_type_qualifier()) { - if (stq.storage_qualifier() != null) { - final String s = stq.storage_qualifier().getText(); - if ("uniform".equals(s)) hasUniform = true; - else if ("in".equals(s)) hasIn = true; - else if ("readonly".equals(s)) hasReadonly = true; - } else if (stq.layout_qualifier() != null) { - for (GLSLParser.Layout_qualifier_idContext id : stq.layout_qualifier().layout_qualifier_id_list().layout_qualifier_id()) { - if (id.IDENTIFIER() == null) continue; - final String qualifier = id.IDENTIFIER().getText(); - if ("location".equals(qualifier)) { - hasLocation = true; - if (id.constant_expression() != null) { - try { locValue = Integer.parseInt(id.constant_expression().getText()); } catch (NumberFormatException ignored) {} - } - } else if ("binding".equals(qualifier) && id.constant_expression() != null) { - try { bindingValue = Integer.parseInt(id.constant_expression().getText()); } catch (NumberFormatException ignored) {} + boolean hasUniform = false, hasIn = false, hasLocation = false, hasReadonly = false; + int locValue = -1; + int bindingValue = -1; + for (GLSLParser.Single_type_qualifierContext stq : tq.single_type_qualifier()) { + if (stq.storage_qualifier() != null) { + final String s = stq.storage_qualifier().getText(); + if ("uniform".equals(s)) hasUniform = true; + else if ("in".equals(s)) hasIn = true; + else if ("readonly".equals(s)) hasReadonly = true; + } else if (stq.layout_qualifier() != null) { + for (GLSLParser.Layout_qualifier_idContext id : stq.layout_qualifier().layout_qualifier_id_list().layout_qualifier_id()) { + if (id.IDENTIFIER() == null) continue; + final String qualifier = id.IDENTIFIER().getText(); + if ("location".equals(qualifier)) { + hasLocation = true; + if (id.constant_expression() != null) { + try { locValue = Integer.parseInt(id.constant_expression().getText()); } catch (NumberFormatException ignored) {} } + } else if ("binding".equals(qualifier) && id.constant_expression() != null) { + try { bindingValue = Integer.parseInt(id.constant_expression().getText()); } catch (NumberFormatException ignored) {} } } } + } - final boolean boolDecl = hasUniform && isBool(fst); - final boolean explicitInputDecl = isVertex && hasIn && hasLocation; + final boolean boolDecl = hasUniform && isBool(fst); + final boolean explicitInputDecl = isVertex && hasIn && hasLocation; + final List declarators = ShaderIndex.declarators(list); + for (GLSLParser.Typeless_declarationContext td : declarators) { + handleDeclarator(td, hasUniform, boolDecl, explicitInputDecl, bools, explicitInputs, edits); + } - if (single.typeless_declaration() != null) { - handleDeclarator(single.typeless_declaration(), hasUniform, boolDecl, explicitInputDecl, bools, explicitInputs, edits); - } - for (GLSLParser.Typeless_declarationContext td : ctx.typeless_declaration()) { - handleDeclarator(td, hasUniform, boolDecl, explicitInputDecl, bools, explicitInputs, edits); - } - - if (separateReadOnlyImages && hasUniform && fst.type_specifier() != null) { - final String typeText = fst.type_specifier().getText(); - if (separateTextureType(typeText) != null) { - needsSamplerless[0] = true; - } else if (hasReadonly) { - final String asTexture = separateTextureTypeForImage(typeText); - if (asTexture != null) { - needsSamplerless[0] = true; - collectDeclaredNames(single, ctx, readOnlyImages); - final String binding = bindingValue >= 0 ? "layout(binding = " + bindingValue + ") " : ""; - edits.add(new Edit(startIdx(fst), stopIdx(fst), binding + "uniform " + asTexture)); + if (separateReadOnlyImages && hasUniform && fst.type_specifier() != null) { + final String typeText = fst.type_specifier().getText(); + if (separateTextureType(typeText) != null) { + needsSamplerless = true; + } else if (hasReadonly) { + final String asTexture = separateTextureTypeForImage(typeText); + if (asTexture != null) { + needsSamplerless = true; + for (GLSLParser.Typeless_declarationContext td : declarators) { + if (td.IDENTIFIER() != null) readOnlyImages.add(td.IDENTIFIER().getText()); } - } - } - - if (isVertex && hasIn) { - if (hasLocation && locValue >= 0) { - if (locValue > maxExplicitLoc[0]) maxExplicitLoc[0] = locValue; - } else if (!hasLocation) { - unlocatedVsInputStarts.add(ctx.getStart().getStartIndex()); + final String binding = bindingValue >= 0 ? "layout(binding = " + bindingValue + ") " : ""; + edits.add(new Edit(startIdx(fst), stopIdx(fst), binding + "uniform " + asTexture)); } } } - @Override - public void enterPostfix_expression(GLSLParser.Postfix_expressionContext ctx) { - if (readOnlyImages.isEmpty() || ctx.LEFT_PAREN() == null || ctx.RIGHT_PAREN() == null) return; - final GLSLParser.Function_call_parametersContext params = ctx.function_call_parameters(); - if (params == null || params.assignment_expression() == null || params.assignment_expression().isEmpty()) return; - if (!readOnlyImages.contains(params.assignment_expression(0).getText())) return; - - final ParseTree callee = ctx.getChild(0); - if (!(callee instanceof ParserRuleContext calleeCtx)) return; - final String replacement = switch (calleeCtx.getText()) { - case "imageLoad" -> "texelFetch"; - case "imageSize" -> "textureSize"; - default -> null; - }; - if (replacement == null) return; - - edits.add(new Edit(startIdx(calleeCtx), stopIdx(calleeCtx), replacement)); - final int rparen = ctx.RIGHT_PAREN().getSymbol().getStartIndex(); - edits.add(new Edit(rparen, rparen - 1, ", 0")); - } - - @Override - public void visitTerminal(TerminalNode node) { - final Token tok = node.getSymbol(); - if (tok.getType() != GLSLLexer.IDENTIFIER) return; - final String text = tok.getText(); - switch (text) { - case "sampler" -> edits.add(new Edit(tok.getStartIndex(), tok.getStopIndex(), SAMPLER_RENAMED)); - case "gl_VertexID" -> edits.add(new Edit(tok.getStartIndex(), tok.getStopIndex(), "gl_VertexIndex")); - case "gl_InstanceID" -> edits.add(new Edit(tok.getStartIndex(), tok.getStopIndex(), "gl_InstanceIndex")); - default -> { - if (TARGET_RESERVED.contains(text)) edits.add(new Edit(tok.getStartIndex(), tok.getStopIndex(), RESERVED_RENAMED_PREFIX + text)); - } + if (isVertex && hasIn) { + if (hasLocation && locValue >= 0) { + if (locValue > maxExplicitLoc) maxExplicitLoc = locValue; + } else if (!hasLocation) { + unlocatedVsInputStarts.add(list.getStart().getStartIndex()); } } - }, root); - - if (needsSamplerless[0] && source != null) { - final int at = versionDirectiveEnd(source); - if (at >= 0) edits.add(new Edit(at, at - 1, "\n" + SAMPLERLESS_EXTENSION)); } - if (maxExplicitLoc[0] >= 0 && !unlocatedVsInputStarts.isEmpty()) { - int next = maxExplicitLoc[0] + 1; + if (!readOnlyImages.isEmpty()) { + rewriteReadOnlyImageCalls(index.callsTo("imageLoad"), "texelFetch", readOnlyImages, edits); + rewriteReadOnlyImageCalls(index.callsTo("imageSize"), "textureSize", readOnlyImages, edits); + } + if (needsSamplerless) { + final int versionEnd = index.directives().versionEnd(); + if (versionEnd >= 0) edits.add(new Edit(versionEnd, versionEnd - 1, "\n" + SAMPLERLESS_EXTENSION)); + } + + rename(index, "sampler", SAMPLER_RENAMED, edits); + rename(index, "gl_VertexID", "gl_VertexIndex", edits); + rename(index, "gl_InstanceID", "gl_InstanceIndex", edits); + for (String reserved : TARGET_RESERVED) rename(index, reserved, RESERVED_RENAMED_PREFIX + reserved, edits); + + if (maxExplicitLoc >= 0 && !unlocatedVsInputStarts.isEmpty()) { + int next = maxExplicitLoc + 1; int dropped = 0; for (int start : unlocatedVsInputStarts) { if (next >= MAX_VS_INPUT_LOCATIONS) { dropped++; continue; } @@ -245,22 +210,31 @@ public final class GlslVulkanPreprocess { } } - return new Metadata(bools, explicitInputs, needsSamplerless[0]); + return new Metadata(bools, explicitInputs, needsSamplerless); + } + + private static void rename(ShaderIndex index, String name, String replacement, List edits) { + for (Token tok : index.occurrences(name)) edits.add(new Edit(tok.getStartIndex(), tok.getStopIndex(), replacement)); + } + + private static void rewriteReadOnlyImageCalls(List calls, String replacement, Set readOnlyImages, List edits) { + for (GLSLParser.Postfix_expressionContext call : calls) { + if (call.RIGHT_PAREN() == null) continue; + final GLSLParser.Function_call_parametersContext params = call.function_call_parameters(); + if (params == null || params.assignment_expression().isEmpty()) continue; + if (!readOnlyImages.contains(params.assignment_expression(0).getText())) continue; + + final ParserRuleContext callee = (ParserRuleContext) call.getChild(0); + edits.add(new Edit(startIdx(callee), stopIdx(callee), replacement)); + final int rparen = call.RIGHT_PAREN().getSymbol().getStartIndex(); + edits.add(new Edit(rparen, rparen - 1, ", 0")); + } } public static void clearCache() { synchronized (CACHE) { CACHE.clear(); } } - private static void collectDeclaredNames(GLSLParser.Single_declarationContext single, GLSLParser.Init_declarator_listContext ctx, Set out) { - if (single.typeless_declaration() != null && single.typeless_declaration().IDENTIFIER() != null) { - out.add(single.typeless_declaration().IDENTIFIER().getText()); - } - for (GLSLParser.Typeless_declarationContext td : ctx.typeless_declaration()) { - if (td.IDENTIFIER() != null) out.add(td.IDENTIFIER().getText()); - } - } - private static @Nullable String separateTextureTypeForImage(String typeText) { final String dim = switch (typeText) { case "image1D", "iimage1D", "uimage1D" -> "1D"; @@ -281,26 +255,6 @@ public final class GlslVulkanPreprocess { }; } - private static int versionDirectiveEnd(String source) { - final GLSLPreParser.Translation_unitContext pre; - try { - pre = GlslTransformUtils.parsePreQuiet(source); - } catch (Exception e) { - return -1; - } - final GLSLPreParser.Version_directiveContext ctx = firstVersionDirective(pre); - return ctx == null || ctx.getStop() == null ? -1 : ctx.getStop().getStopIndex() + 1; - } - - private static GLSLPreParser.@Nullable Version_directiveContext firstVersionDirective(ParseTree node) { - if (node instanceof GLSLPreParser.Version_directiveContext v) return v; - for (int i = 0; i < node.getChildCount(); i++) { - final GLSLPreParser.Version_directiveContext found = firstVersionDirective(node.getChild(i)); - if (found != null) return found; - } - return null; - } - private static void handleDeclarator(GLSLParser.Typeless_declarationContext td, boolean hasUniform, boolean asBool, boolean asInput, Set bools, Set inputs, List edits) { if (td.IDENTIFIER() == null) return; final String name = td.IDENTIFIER().getText(); diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/ShaderIndex.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/ShaderIndex.java new file mode 100644 index 000000000..0df317028 --- /dev/null +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/ShaderIndex.java @@ -0,0 +1,153 @@ +package com.gtnewhorizons.angelica.glsm.shader; + +import com.gtnewhorizons.angelica.glsm.GlslTransformUtils; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.TerminalNode; +import org.jetbrains.annotations.Nullable; +import org.taumc.glsl.grammar.GLSLLexer; +import org.taumc.glsl.grammar.GLSLParser; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * What the source passes look up in a parsed shader. + */ +public final class ShaderIndex { + + public record Directives(Set identifiers, boolean hasConditionalOrMacro, int versionEnd) { + private static final Directives NONE = new Directives(Set.of(), false, -1); + } + + private final GLSLParser.Translation_unitContext root; + private final String source; + private final Map> identifiers = new HashMap<>(); + private final Map> calls = new HashMap<>(); + private final List globalDeclarations = new ArrayList<>(); + private @Nullable Directives directives; + + public ShaderIndex(GLSLParser.Translation_unitContext root, String source) { + this.root = root; + this.source = source; + collect(root); + for (GLSLParser.External_declarationContext external : root.external_declaration()) { + if (external.declaration() != null) globalDeclarations.add(external.declaration()); + } + } + + private void collect(ParseTree node) { + if (node instanceof TerminalNode terminal) { + final Token token = terminal.getSymbol(); + if (token.getType() == GLSLLexer.IDENTIFIER) identifiers.computeIfAbsent(token.getText(), k -> new ArrayList<>(2)).add(token); + return; + } + if (node instanceof GLSLParser.Postfix_expressionContext p && p.LEFT_PAREN() != null) { + calls.computeIfAbsent(calleeName(p), k -> new ArrayList<>(2)).add(p); + } + for (int i = 0, n = node.getChildCount(); i < n; i++) collect(node.getChild(i)); + } + + public GLSLParser.Translation_unitContext root() { + return root; + } + + public String source() { + return source; + } + + public List occurrences(String name) { + return identifiers.getOrDefault(name, List.of()); + } + + public List callsTo(String name) { + return calls.getOrDefault(name, List.of()); + } + + public List globalDeclarations() { + return globalDeclarations; + } + + /** Lexes the source on first use. */ + public Directives directives() { + if (directives == null) directives = scanDirectives(source); + return directives; + } + + private static Directives scanDirectives(String source) { + if (source.indexOf('#') < 0) return Directives.NONE; + final GLSLLexer lexer = GlslTransformUtils.quietLexer(source); + final Set names = new HashSet<>(); + boolean conditionalOrMacro = false; + int versionEnd = -1; + boolean inVersion = false; + for (Token token = lexer.nextToken(); token.getType() != Token.EOF; token = lexer.nextToken()) { + final int type = token.getType(); + if (type == GLSLLexer.VERSION_DIRECTIVE && versionEnd < 0) { + inVersion = true; + versionEnd = token.getStopIndex() + 1; + continue; + } + if (inVersion && (type == GLSLLexer.NUMBER || type == GLSLLexer.PROFILE)) { + versionEnd = token.getStopIndex() + 1; + continue; + } + inVersion = false; + switch (type) { + case GLSLLexer.PROGRAM_TEXT, GLSLLexer.MACRO_TEXT, GLSLLexer.CONSTANT_EXPRESSION -> collectIdentifiers(token.getText(), names); + case GLSLLexer.IF_DIRECTIVE, GLSLLexer.IFDEF_DIRECTIVE, GLSLLexer.IFNDEF_DIRECTIVE, GLSLLexer.DEFINE_DIRECTIVE -> conditionalOrMacro = true; + default -> { } + } + } + return new Directives(names, conditionalOrMacro, versionEnd); + } + + private static void collectIdentifiers(String text, Set out) { + if (text == null) return; + final int len = text.length(); + int i = 0; + while (i < len) { + final char c = text.charAt(i); + if (c != '_' && !Character.isLetter(c)) { + i++; + continue; + } + int end = i + 1; + while (end < len) { + final char n = text.charAt(end); + if (n != '_' && !Character.isLetterOrDigit(n)) break; + end++; + } + out.add(text.substring(i, end)); + i = end; + } + } + + public static String calleeName(GLSLParser.Postfix_expressionContext call) { + final ParseTree callee = call.getChild(0); + return callee instanceof ParserRuleContext ? callee.getText() : ""; + } + + public static List declarators(GLSLParser.Init_declarator_listContext list) { + final GLSLParser.Single_declarationContext single = list.single_declaration(); + final List rest = list.typeless_declaration(); + if (single == null || single.typeless_declaration() == null) return rest; + final List all = new ArrayList<>(rest.size() + 1); + all.add(single.typeless_declaration()); + all.addAll(rest); + return all; + } + + public static boolean hasStorageQualifier(@Nullable GLSLParser.Type_qualifierContext qualifier, String keyword) { + if (qualifier == null) return false; + for (GLSLParser.Single_type_qualifierContext q : qualifier.single_type_qualifier()) { + if (q.storage_qualifier() != null && keyword.equals(q.storage_qualifier().getText())) return true; + } + return false; + } +} diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/SpirvShaderTranslator.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/SpirvShaderTranslator.java index 8225c9b38..16ad636c8 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/SpirvShaderTranslator.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/shader/SpirvShaderTranslator.java @@ -47,14 +47,6 @@ public final class SpirvShaderTranslator { private SpirvShaderTranslator() {} public static @Nullable String glslToGlslEs(String source, int glShaderType, String debugName) { - return glslToGlslEsImpl(source, glShaderType, debugName, false); - } - - static @Nullable String glslToGlslEsRawForTest(String source, int glShaderType, String debugName) { - return glslToGlslEsImpl(source, glShaderType, debugName, true); - } - - private static @Nullable String glslToGlslEsImpl(String source, int glShaderType, String debugName, boolean rawOutput) { final int shaderKind = shaderKindForGlType(glShaderType); if (shaderKind < 0) { LOGGER.warn("Unknown GL shader type 0x{} for '{}' - skipping SPIR-V translation", Integer.toHexString(glShaderType), debugName); @@ -74,7 +66,7 @@ public final class SpirvShaderTranslator { try { final String emitted = emitGlslEs(cr.spirv(), debugName, glShaderType, pre.explicitVsInputs()); if (emitted == null) return null; - return rawOutput ? emitted : postProcessEsOutput(emitted, pre.boolUniforms()); + return postProcessEsOutput(emitted, pre.boolUniforms()); } finally { memFree(cr.spirv()); } @@ -163,16 +155,7 @@ public final class SpirvShaderTranslator { final GLSLParser.Single_declarationContext single = ctx.single_declaration(); if (single == null || single.typeless_declaration() == null) return; final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); - if (fst == null || fst.type_qualifier() == null) return; - - boolean isUniform = false; - for (GLSLParser.Single_type_qualifierContext stq : fst.type_qualifier().single_type_qualifier()) { - if (stq.storage_qualifier() != null && "uniform".equals(stq.storage_qualifier().getText())) { - isUniform = true; - break; - } - } - if (!isUniform) return; + if (fst == null || !ShaderIndex.hasStorageQualifier(fst.type_qualifier(), "uniform")) return; if (fst.type_specifier() == null || fst.type_specifier().type_specifier_nonarray() == null) return; final GLSLParser.Type_specifier_nonarrayContext ts = fst.type_specifier().type_specifier_nonarray(); diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ClipZRemap.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ClipZRemap.java index 1d4f962ed..03727c4a2 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ClipZRemap.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ClipZRemap.java @@ -1,9 +1,7 @@ package com.gtnewhorizons.angelica.sdlgpu.shader; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; -import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.taumc.glsl.grammar.GLSLParser; -import org.taumc.glsl.grammar.GLSLParserBaseListener; import java.util.List; @@ -14,22 +12,15 @@ public final class ClipZRemap { private ClipZRemap() {} public static void collectEdits(GLSLParser.Translation_unitContext root, List edits) { - final int[] mainCloseBraceIdx = { -1 }; - ParseTreeWalker.DEFAULT.walk(new GLSLParserBaseListener() { - @Override - public void enterFunction_definition(GLSLParser.Function_definitionContext ctx) { - if (mainCloseBraceIdx[0] >= 0) return; - final GLSLParser.Function_prototypeContext proto = ctx.function_prototype(); - if (proto == null || proto.IDENTIFIER() == null) return; - if (!"main".equals(proto.IDENTIFIER().getText())) return; - final GLSLParser.Compound_statement_no_new_scopeContext body = ctx.compound_statement_no_new_scope(); - if (body == null || body.RIGHT_BRACE() == null) return; - mainCloseBraceIdx[0] = body.RIGHT_BRACE().getSymbol().getStartIndex(); - } - }, root); - - if (mainCloseBraceIdx[0] < 0) return; - final int idx = mainCloseBraceIdx[0]; - edits.add(new Edit(idx, idx - 1, INJECTION_STD)); + for (GLSLParser.External_declarationContext external : root.external_declaration()) { + final GLSLParser.Function_definitionContext function = external.function_definition(); + if (function == null || function.function_prototype().IDENTIFIER() == null) continue; + if (!"main".equals(function.function_prototype().IDENTIFIER().getText())) continue; + final GLSLParser.Compound_statement_no_new_scopeContext body = function.compound_statement_no_new_scope(); + if (body == null || body.RIGHT_BRACE() == null) return; + final int idx = body.RIGHT_BRACE().getSymbol().getStartIndex(); + edits.add(new Edit(idx, idx - 1, INJECTION_STD)); + return; + } } } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoisting.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoisting.java new file mode 100644 index 000000000..913210660 --- /dev/null +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoisting.java @@ -0,0 +1,398 @@ +package com.gtnewhorizons.angelica.sdlgpu.shader; + +import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess; +import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.ParseTree; +import org.jetbrains.annotations.Nullable; +import org.taumc.glsl.grammar.GLSLLexer; +import org.taumc.glsl.grammar.GLSLParser; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.startIdx; +import static com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.stopIdx; + +/** + * Moves {@code dFdx}/{@code dFdy}/{@code fwidth} calls in front of the {@code if} blocks around them, so every section of + * the 2x2 quad computes them. Packs take derivatives inside branches that only part of a quad enters, which GLSL + * leaves undefined. ACO writes {@code src - src[lane0]} in the active lanes only, then reads lane 1/2 with + * fetch-inactive, so a neighbor that skipped the branch hands back a stale register, often NaN! + * This was observed on Euphoria Patches and this aims to fix this wherever possible on Vulkan. + */ +public final class DerivativeHoisting { + + private static final String PREFIX = "angelica_derivative_"; + + private static final Set DERIVATIVES = Set.of( + "dFdx", "dFdy", "fwidth", + "dFdxFine", "dFdyFine", "fwidthFine", + "dFdxCoarse", "dFdyCoarse", "fwidthCoarse"); + + private static final Map> BUILTIN_OUT_PARAMS = Map.of( + "modf", Set.of(1), + "frexp", Set.of(1), + "uaddCarry", Set.of(2), + "usubBorrow", Set.of(2), + "umulExtended", Set.of(2, 3), + "imulExtended", Set.of(2, 3)); + + private static final String UNKNOWN = "*"; + + private DerivativeHoisting() {} + + private record Declared(String type, boolean global) {} + + private record Operand(String name, @Nullable String swizzle) {} + + private record DerivativeCall(GLSLParser.Postfix_expressionContext call, String callee) {} + + private record Effects(Set declared, Set written, Set called) { + Effects() { + this(new HashSet<>(), new HashSet<>(), new HashSet<>()); + } + } + + public static void collectEdits(ShaderIndex index, List edits) { + final List calls = new ArrayList<>(); + for (String callee : DERIVATIVES) { + for (GLSLParser.Postfix_expressionContext call : index.callsTo(callee)) calls.add(new DerivativeCall(call, callee)); + } + if (calls.isEmpty()) return; + calls.sort(Comparator.comparingInt(c -> startIdx(c.call()))); + + final String source = index.source(); + final GLSLParser.Translation_unitContext root = index.root(); + final Functions functions = new Functions(index); + final int earlierEditCount = edits.size(); + final Map ifEffects = new HashMap<>(); + final Map declarations = new LinkedHashMap<>(); + final Map hoisted = new HashMap<>(); + boolean checkedDirectives = false; + + for (DerivativeCall derivative : calls) { + final GLSLParser.Postfix_expressionContext call = derivative.call(); + final GLSLParser.Function_call_parametersContext params = call.function_call_parameters(); + if (params == null || params.assignment_expression().size() != 1) continue; + final GLSLParser.Assignment_expressionContext arg = params.assignment_expression(0); + final Operand operand = operand(arg); + if (operand == null) continue; + + final Declared declared = declaredType(call, operand.name(), root); + if (declared == null) continue; + final String type = resultType(declared.type(), operand.swizzle()); + if (type == null) continue; + + final GLSLParser.Selection_statementContext target = outermostSafeIf(call, operand.name(), declared.global(), functions, ifEffects); + if (target == null) continue; + if (!checkedDirectives) { + if (index.directives().hasConditionalOrMacro()) return; + checkedDirectives = true; + } + + final String callee = derivative.callee(); + final String argText = sliceWithEdits(source, startIdx(arg), stopIdx(arg), edits.subList(0, earlierEditCount)); + final String key = startIdx(target) + "|" + callee + "|" + argText; + String variable = hoisted.get(key); + if (variable == null) { + variable = PREFIX + hoisted.size(); + hoisted.put(key, variable); + declarations.computeIfAbsent(target, k -> new StringBuilder()) + .append(type).append(' ').append(variable).append(" = ").append(callee).append('(').append(argText).append(");\n") + .append(indentBefore(source, startIdx(target))); + } + edits.add(new Edit(startIdx(call), stopIdx(call), variable)); + } + + for (Map.Entry e : declarations.entrySet()) { + final int at = startIdx(e.getKey()); + edits.add(new Edit(at, at - 1, e.getValue().toString())); + } + } + + private static final class Functions { + final Map> outParams = new HashMap<>(); + final Set names = new HashSet<>(); + final Map calleeNames = new HashMap<>(); + private final List definitions = new ArrayList<>(); + private @Nullable Map> globalWrites; + + Functions(ShaderIndex index) { + for (GLSLParser.External_declarationContext external : index.root().external_declaration()) { + final GLSLParser.Function_prototypeContext prototype; + if (external.function_definition() != null) { + definitions.add(external.function_definition()); + prototype = external.function_definition().function_prototype(); + } else if (external.declaration() != null) { + prototype = external.declaration().function_prototype(); + } else { + prototype = null; + } + if (prototype == null || prototype.IDENTIFIER() == null) continue; + final String name = prototype.IDENTIFIER().getText(); + names.add(name); + final List params = parameters(prototype); + for (int i = 0; i < params.size(); i++) { + final GLSLParser.Type_qualifierContext q = params.get(i).type_qualifier(); + if (ShaderIndex.hasStorageQualifier(q, "out") || ShaderIndex.hasStorageQualifier(q, "inout")) { + outParams.computeIfAbsent(name, k -> new HashSet<>()).add(i); + } + } + } + for (String name : names) addCalls(index, name); + for (String name : BUILTIN_OUT_PARAMS.keySet()) addCalls(index, name); + } + + private void addCalls(ShaderIndex index, String name) { + for (GLSLParser.Postfix_expressionContext call : index.callsTo(name)) calleeNames.put(call, name); + } + + Set globalWrites(String function) { + if (globalWrites == null) globalWrites = summarize(); + return globalWrites.getOrDefault(function, Set.of()); + } + + private Map> summarize() { + final Map> writes = new HashMap<>(); + final Map> callees = new HashMap<>(); + for (GLSLParser.Function_definitionContext definition : definitions) { + final String name = definition.function_prototype().IDENTIFIER().getText(); + final Effects effects = new Effects(); + collectEffects(definition.compound_statement_no_new_scope(), this, effects); + for (GLSLParser.Parameter_declarationContext param : parameters(definition.function_prototype())) { + final String parameter = parameterName(param); + if (parameter != null) effects.written().remove(parameter); + } + writes.computeIfAbsent(name, k -> new HashSet<>()).addAll(effects.written()); + callees.computeIfAbsent(name, k -> new HashSet<>()).addAll(effects.called()); + } + boolean changed = true; + while (changed) { + changed = false; + for (Map.Entry> e : callees.entrySet()) { + final Set into = writes.get(e.getKey()); + for (String callee : e.getValue()) { + final Set from = writes.get(callee); + if (from != null) changed |= into.addAll(from); + } + } + } + return writes; + } + } + + private static @Nullable Operand operand(GLSLParser.Assignment_expressionContext arg) { + if (arg.assignment_operator() != null) return null; + final GLSLParser.Constant_expressionContext c = arg.constant_expression(); + if (c == null || c.QUESTION() != null) return null; + final GLSLParser.Unary_expressionContext u = c.binary_expression().unary_expression(); + if (u == null || u.postfix_expression() == null) return null; + final GLSLParser.Postfix_expressionContext p = u.postfix_expression(); + + final String name = variableName(p); + if (name != null) return new Operand(name, null); + if (p.DOT() == null || p.field_selection() == null || p.field_selection().variable_identifier() == null) return null; + final String base = variableName(p.postfix_expression()); + return base == null ? null : new Operand(base, p.field_selection().variable_identifier().getText()); + } + + private static @Nullable String variableName(@Nullable GLSLParser.Postfix_expressionContext p) { + if (p == null || p.primary_expression() == null || p.primary_expression().variable_identifier() == null) return null; + return p.primary_expression().variable_identifier().getText(); + } + + private static @Nullable String resultType(String declaredType, @Nullable String swizzle) { + final int size = switch (declaredType) { + case "float" -> 1; + case "vec2" -> 2; + case "vec3" -> 3; + case "vec4" -> 4; + default -> 0; + }; + if (size == 0) return null; + if (swizzle == null) return declaredType; + if (size == 1 || swizzle.isEmpty() || swizzle.length() > 4) return null; + for (String set : new String[] { "xyzw", "rgba", "stpq" }) { + boolean all = true; + for (int i = 0; i < swizzle.length(); i++) { + final int component = set.indexOf(swizzle.charAt(i)); + if (component < 0 || component >= size) { all = false; break; } + } + if (all) return swizzle.length() == 1 ? "float" : "vec" + swizzle.length(); + } + return null; + } + + private static @Nullable Declared declaredType(ParserRuleContext from, String name, GLSLParser.Translation_unitContext root) { + ParseTree child = from; + for (ParserRuleContext p = from.getParent(); p != null; child = p, p = p.getParent()) { + if (p instanceof GLSLParser.Statement_listContext list) { + for (int i = list.children.indexOf(child) - 1; i >= 0; i--) { + final String type = typeOf(list.getChild(i), name); + if (type != null) return new Declared(type, false); + } + } else if (p instanceof GLSLParser.Iteration_statementContext loop && loop.for_init_statement() != null && child != loop.for_init_statement()) { + final String type = typeOf(loop.for_init_statement(), name); + if (type != null) return new Declared(type, false); + } else if (p instanceof GLSLParser.Function_definitionContext function) { + for (GLSLParser.Parameter_declarationContext param : parameters(function.function_prototype())) { + if (name.equals(parameterName(param))) { + final GLSLParser.Parameter_declaratorContext d = param.parameter_declarator(); + return new Declared(d.array_specifier() == null ? baseType(d.type_specifier()) : "", false); + } + } + break; + } + } + for (GLSLParser.External_declarationContext external : root.external_declaration()) { + if (external.declaration() == null) continue; + final String type = typeOf(external.declaration(), name); + if (type != null) return new Declared(type, true); + } + return null; + } + + private static @Nullable String typeOf(ParseTree node, String name) { + final GLSLParser.Init_declarator_listContext list = firstDeclaratorList(node); + if (list == null || list.single_declaration() == null) return null; + for (GLSLParser.Typeless_declarationContext d : ShaderIndex.declarators(list)) { + if (d.IDENTIFIER() == null || !name.equals(d.IDENTIFIER().getText())) continue; + if (d.array_specifier() != null) return ""; + return baseType(list.single_declaration().fully_specified_type().type_specifier()); + } + return null; + } + + private static GLSLParser.@Nullable Init_declarator_listContext firstDeclaratorList(ParseTree node) { + ParseTree n = node; + while (n != null) { + if (n instanceof GLSLParser.Init_declarator_listContext list) return list; + if (n instanceof GLSLParser.Simple_statementContext s) n = s.declaration_statement(); + else if (n instanceof GLSLParser.StatementContext s) n = s.simple_statement(); + else if (n instanceof GLSLParser.For_init_statementContext f) n = f.declaration_statement(); + else if (n instanceof GLSLParser.Declaration_statementContext d) n = d.declaration(); + else if (n instanceof GLSLParser.DeclarationContext d) n = d.init_declarator_list(); + else return null; + } + return null; + } + + private static String baseType(GLSLParser.Type_specifierContext type) { + if (type == null || type.array_specifier() != null || type.type_specifier_nonarray() == null) return ""; + return type.type_specifier_nonarray().getText(); + } + + private static List parameters(GLSLParser.Function_prototypeContext prototype) { + final GLSLParser.Function_parametersContext params = prototype.function_parameters(); + return params == null ? List.of() : params.parameter_declaration(); + } + + private static @Nullable String parameterName(GLSLParser.Parameter_declarationContext param) { + final GLSLParser.Parameter_declaratorContext d = param.parameter_declarator(); + return d == null || d.IDENTIFIER() == null ? null : d.IDENTIFIER().getText(); + } + + private static GLSLParser.@Nullable Selection_statementContext outermostSafeIf(ParserRuleContext call, String name, boolean global, Functions functions, + Map ifEffects) { + final List enclosing = new ArrayList<>(); + for (ParserRuleContext p = call.getParent(); p != null; p = p.getParent()) { + if (p instanceof GLSLParser.Selection_rest_statementContext rest) { + enclosing.add((GLSLParser.Selection_statementContext) rest.getParent()); + } else if (p instanceof GLSLParser.Iteration_statementContext || p instanceof GLSLParser.Switch_statementContext + || p instanceof GLSLParser.Function_definitionContext) { + break; + } + } + for (int i = enclosing.size() - 1; i >= 0; i--) { + final GLSLParser.Selection_statementContext candidate = enclosing.get(i); + if (!inStatementList(candidate)) continue; + Effects effects = ifEffects.get(candidate); + if (effects == null) { + effects = new Effects(); + collectEffects(candidate, functions, effects); + ifEffects.put(candidate, effects); + } + if (effects.declared().contains(name) || writes(effects, name, global, functions)) continue; + return candidate; + } + return null; + } + + private static boolean inStatementList(GLSLParser.Selection_statementContext s) { + return s.getParent() instanceof GLSLParser.Simple_statementContext simple + && simple.getParent() instanceof GLSLParser.StatementContext statement + && statement.getParent() instanceof GLSLParser.Statement_listContext list + && !(list.getParent() instanceof GLSLParser.Switch_statementContext); + } + + private static boolean writes(Effects effects, String name, boolean global, Functions functions) { + if (effects.written().contains(name) || effects.written().contains(UNKNOWN)) return true; + if (!global) return false; + for (String callee : effects.called()) { + final Set w = functions.globalWrites(callee); + if (w.contains(name) || w.contains(UNKNOWN)) return true; + } + return false; + } + + private static void collectEffects(ParseTree node, Functions functions, Effects effects) { + final Set written = effects.written(); + if (node instanceof GLSLParser.Typeless_declarationContext d && d.IDENTIFIER() != null) { + effects.declared().add(d.IDENTIFIER().getText()); + } else if (node instanceof GLSLParser.ConditionContext c && c.IDENTIFIER() != null) { + effects.declared().add(c.IDENTIFIER().getText()); + } else if (node instanceof GLSLParser.Assignment_expressionContext a && a.assignment_operator() != null) { + written.add(lvalueName(a.unary_expression())); + } else if (node instanceof GLSLParser.Unary_expressionContext u && (u.INC_OP() != null || u.DEC_OP() != null)) { + written.add(lvalueName(u.unary_expression())); + } else if (node instanceof GLSLParser.Postfix_expressionContext p) { + if (p.INC_OP() != null || p.DEC_OP() != null) written.add(lvalueName(p.postfix_expression())); + final String callee = functions.calleeNames.get(p); + if (callee != null) { + if (functions.names.contains(callee)) effects.called().add(callee); + Set positions = functions.outParams.get(callee); + if (positions == null) positions = BUILTIN_OUT_PARAMS.get(callee); + if (positions != null && p.function_call_parameters() != null) { + final List args = p.function_call_parameters().assignment_expression(); + for (int i : positions) { + if (i < args.size()) written.add(lvalueName(args.get(i))); + } + } + } + } + for (int i = 0; i < node.getChildCount(); i++) { + collectEffects(node.getChild(i), functions, effects); + } + } + + private static String lvalueName(@Nullable ParserRuleContext lvalue) { + if (lvalue == null) return UNKNOWN; + final Token start = lvalue.getStart(); + return start != null && start.getType() == GLSLLexer.IDENTIFIER ? start.getText() : UNKNOWN; + } + + private static String indentBefore(String source, int at) { + int lineStart = at; + while (lineStart > 0 && (source.charAt(lineStart - 1) == ' ' || source.charAt(lineStart - 1) == '\t')) lineStart--; + return source.substring(lineStart, at); + } + + private static String sliceWithEdits(String source, int start, int stop, List edits) { + final List inside = new ArrayList<>(); + for (Edit e : edits) { + if (e.startIdx() >= start && e.startIdx() <= stop && e.stopIdx() <= stop) { + inside.add(new Edit(e.startIdx() - start, e.stopIdx() - start, e.replacement())); + } + } + return GlslVulkanPreprocess.applyEdits(source.substring(start, stop + 1), inside); + } +} diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/LogicOpLowering.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/LogicOpLowering.java index f92033706..2a9c351a4 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/LogicOpLowering.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/LogicOpLowering.java @@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.sdlgpu.shader; import com.gtnewhorizons.angelica.glsm.GlslTransformUtils; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import org.antlr.v4.runtime.Token; import org.embeddedt.embeddium.impl.render.shader.ShaderLoader; import org.taumc.glsl.grammar.GLSLParser; @@ -112,11 +113,8 @@ public final class LogicOpLowering { final int vecSize = floatVecSize(ts.type_specifier_nonarray().getText()); final int typeArrayLen = ts.array_specifier() != null ? arrayLen(ts.array_specifier(), program) : 0; - final List names = new ArrayList<>(); - if (single.typeless_declaration() != null) names.add(single.typeless_declaration()); - names.addAll(idl.typeless_declaration()); int nextLocation = location; - for (GLSLParser.Typeless_declarationContext td : names) { + for (GLSLParser.Typeless_declarationContext td : ShaderIndex.declarators(idl)) { final int len = td.array_specifier() != null ? arrayLen(td.array_specifier(), program) : typeArrayLen; outputs.add(new Output(td.IDENTIFIER().getText(), nextLocation, len, vecSize)); if (nextLocation >= 0) nextLocation += Math.max(len, 1); diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/PerFrameBlockInjector.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/PerFrameBlockInjector.java index bd89ec97b..bd35c5eb6 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/PerFrameBlockInjector.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/PerFrameBlockInjector.java @@ -3,13 +3,9 @@ package com.gtnewhorizons.angelica.sdlgpu.shader; import com.gtnewhorizons.angelica.glsm.hooks.PerFrameUniformBlock; import com.gtnewhorizons.angelica.glsm.hooks.PerFrameUniformBlock.Member; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import com.gtnewhorizons.angelica.glsm.shader.UniformType; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.antlr.v4.runtime.tree.TerminalNode; -import org.taumc.glsl.grammar.GLSLLexer; import org.taumc.glsl.grammar.GLSLParser; -import org.taumc.glsl.grammar.GLSLParserBaseListener; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; @@ -28,13 +24,13 @@ public final class PerFrameBlockInjector { private PerFrameBlockInjector() {} - public static void collectEdits(GLSLParser.Translation_unitContext root, PerFrameUniformBlock perFrame, PerFrameUniformBlock perPass, List out) { + public static void collectEdits(ShaderIndex index, PerFrameUniformBlock perFrame, PerFrameUniformBlock perPass, List out) { final PerFrameUniformBlock[] blocks = new PerFrameUniformBlock[ShaderManager.BLOCK_COUNT]; blocks[ShaderManager.BLOCK_PER_FRAME] = normalize(perFrame); blocks[ShaderManager.BLOCK_PER_PASS] = normalize(perPass); if (blocks[0] == null && blocks[1] == null) return; - final int insertAt = firstDeclarationStart(root); + final int insertAt = firstDeclarationStart(index.root()); if (insertAt < 0) return; final Map memberTypes = new LinkedHashMap<>(); @@ -51,9 +47,12 @@ public final class PerFrameBlockInjector { final List stripEdits = new ArrayList<>(); final IntArrayList stripBlocks = new IntArrayList(); final Set shadowed = new HashSet<>(); - classifyDeclarations(root, memberTypes, memberBlock, stripEdits, stripBlocks, shadowed); + classifyDeclarations(index, memberTypes, memberBlock, stripEdits, stripBlocks, shadowed); - final Set referenced = referencedMembers(root, memberTypes.keySet(), shadowed); + final Set referenced = new HashSet<>(); + for (String name : memberTypes.keySet()) { + if (!shadowed.contains(name) && !index.occurrences(name).isEmpty()) referenced.add(name); + } final boolean[] injected = new boolean[blocks.length]; boolean anyInjected = false; for (int b = 0; b < blocks.length; b++) { @@ -108,11 +107,8 @@ public final class PerFrameBlockInjector { return (decls == null || decls.isEmpty()) ? -1 : decls.get(0).getStart().getStartIndex(); } - private static void classifyDeclarations(GLSLParser.Translation_unitContext root, Map memberTypes, Object2IntOpenHashMap memberBlock, List stripEdits, IntArrayList stripBlocks, Set shadowed) { - for (GLSLParser.External_declarationContext ed : root.external_declaration()) { - final GLSLParser.DeclarationContext ctx = ed.declaration(); - if (ctx == null) continue; - + private static void classifyDeclarations(ShaderIndex index, Map memberTypes, Object2IntOpenHashMap memberBlock, List stripEdits, IntArrayList stripBlocks, Set shadowed) { + for (GLSLParser.DeclarationContext ctx : index.globalDeclarations()) { final List declared = declaredNames(ctx); if (declared.isEmpty()) continue; @@ -141,16 +137,7 @@ public final class PerFrameBlockInjector { final GLSLParser.Single_declarationContext single = idl.single_declaration(); if (single == null || single.fully_specified_type() == null) return false; final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); - if (fst.type_qualifier() == null) return false; - - boolean hasUniform = false; - for (GLSLParser.Single_type_qualifierContext stq : fst.type_qualifier().single_type_qualifier()) { - if (stq.storage_qualifier() != null && "uniform".equals(stq.storage_qualifier().getText())) { - hasUniform = true; - break; - } - } - if (!hasUniform) return false; + if (!ShaderIndex.hasStorageQualifier(fst.type_qualifier(), "uniform")) return false; if (fst.type_specifier() == null || fst.type_specifier().type_specifier_nonarray() == null) return false; final String declaredType = fst.type_specifier().type_specifier_nonarray().getText(); @@ -160,30 +147,11 @@ public final class PerFrameBlockInjector { return true; } - private static Set referencedMembers(GLSLParser.Translation_unitContext root, Set memberNames, Set shadowed) { - final Set referenced = new HashSet<>(); - ParseTreeWalker.DEFAULT.walk(new GLSLParserBaseListener() { - @Override - public void visitTerminal(TerminalNode node) { - final Token tok = node.getSymbol(); - if (tok.getType() != GLSLLexer.IDENTIFIER) return; - final String name = tok.getText(); - if (memberNames.contains(name) && !shadowed.contains(name)) referenced.add(name); - } - }, root); - return referenced; - } - private static List declaredNames(GLSLParser.DeclarationContext ctx) { final GLSLParser.Init_declarator_listContext idl = ctx.init_declarator_list(); - if (idl == null) return List.of(); - final GLSLParser.Single_declarationContext single = idl.single_declaration(); - if (single == null) return List.of(); + if (idl == null || idl.single_declaration() == null) return List.of(); final List out = new ArrayList<>(2); - if (single.typeless_declaration() != null && single.typeless_declaration().IDENTIFIER() != null) { - out.add(single.typeless_declaration().IDENTIFIER().getText()); - } - for (GLSLParser.Typeless_declarationContext td : idl.typeless_declaration()) { + for (GLSLParser.Typeless_declarationContext td : ShaderIndex.declarators(idl)) { if (td.IDENTIFIER() != null) out.add(td.IDENTIFIER().getText()); } return out; diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/SamplerStripper.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/SamplerStripper.java index da9264212..c1ffb65d8 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/SamplerStripper.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/SamplerStripper.java @@ -1,20 +1,15 @@ package com.gtnewhorizons.angelica.sdlgpu.shader; -import com.gtnewhorizons.angelica.glsm.GlslTransformUtils; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.antlr.v4.runtime.tree.TerminalNode; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.taumc.glsl.grammar.GLSLLexer; +import org.jetbrains.annotations.Nullable; import org.taumc.glsl.grammar.GLSLParser; -import org.taumc.glsl.grammar.GLSLParserBaseListener; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; @@ -24,79 +19,68 @@ public final class SamplerStripper { private SamplerStripper() {} - public static void collectEdits(GLSLParser.Translation_unitContext root, String source, List edits) { - record SamplerDecl(String name, int start, int stop) {} + private record SamplerDecl(List names, int start, int stop) {} + + public static void collectEdits(ShaderIndex index, List edits) { final List samplers = new ArrayList<>(); - - ParseTreeWalker.DEFAULT.walk(new GLSLParserBaseListener() { - @Override - public void enterDeclaration(GLSLParser.DeclarationContext ctx) { - final GLSLParser.Init_declarator_listContext idl = ctx.init_declarator_list(); - if (idl == null) return; - final GLSLParser.Single_declarationContext single = idl.single_declaration(); - if (single == null || single.fully_specified_type() == null) return; - final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); - if (fst.type_qualifier() == null) return; - - boolean hasUniform = false; - for (GLSLParser.Single_type_qualifierContext stq : fst.type_qualifier().single_type_qualifier()) { - if (stq.storage_qualifier() != null && "uniform".equals(stq.storage_qualifier().getText())) { - hasUniform = true; - break; - } - } - if (!hasUniform) return; - if (fst.type_specifier() == null || fst.type_specifier().type_specifier_nonarray() == null) return; - - final String typeText = fst.type_specifier().type_specifier_nonarray().getText(); - if (!isSamplerType(typeText)) return; - - final int start = ctx.getStart().getStartIndex(); - final int stop = ctx.getStop().getStopIndex(); - if (single.typeless_declaration() != null && single.typeless_declaration().IDENTIFIER() != null) { - samplers.add(new SamplerDecl(single.typeless_declaration().IDENTIFIER().getText(), start, stop)); - } - for (GLSLParser.Typeless_declarationContext td : idl.typeless_declaration()) { - if (td.IDENTIFIER() != null) { - samplers.add(new SamplerDecl(td.IDENTIFIER().getText(), start, stop)); - } - } - } - }, root); - + for (GLSLParser.DeclarationContext ctx : index.globalDeclarations()) { + final SamplerDecl decl = samplerDeclaration(ctx); + if (decl != null) samplers.add(decl); + } if (samplers.isEmpty()) return; - final Map refCounts = new HashMap<>(); - for (SamplerDecl s : samplers) refCounts.put(s.name(), 0); - - ParseTreeWalker.DEFAULT.walk(new GLSLParserBaseListener() { - @Override - public void visitTerminal(TerminalNode node) { - final Token tok = node.getSymbol(); - if (tok.getType() != GLSLLexer.IDENTIFIER) return; - final String name = tok.getText(); - final Integer prior = refCounts.get(name); - if (prior == null) return; - final int pos = tok.getStartIndex(); - for (SamplerDecl s : samplers) { - if (pos >= s.start() && pos <= s.stop()) return; - } - refCounts.put(name, prior + 1); - } - }, root); - - final Set directiveRefs = GlslTransformUtils.identifiersInDirectiveText(source); - + Set directiveRefs = null; for (SamplerDecl s : samplers) { - if (refCounts.getOrDefault(s.name(), 0) != 0) continue; - if (directiveRefs.contains(s.name())) { - LOGGER.debug("Keeping sampler {}: referenced only inside a preprocessor block", s.name()); + if (!allUnreferenced(s, samplers, index)) continue; + if (directiveRefs == null) directiveRefs = index.directives().identifiers(); + if (anyIn(s, directiveRefs)) { + LOGGER.debug("Keeping sampler {}: referenced only inside a preprocessor block", s.names()); continue; } edits.add(new Edit(s.start(), s.stop(), "")); } } + private static @Nullable SamplerDecl samplerDeclaration(GLSLParser.DeclarationContext ctx) { + final GLSLParser.Init_declarator_listContext idl = ctx.init_declarator_list(); + if (idl == null) return null; + final GLSLParser.Single_declarationContext single = idl.single_declaration(); + if (single == null || single.fully_specified_type() == null) return null; + final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); + if (!ShaderIndex.hasStorageQualifier(fst.type_qualifier(), "uniform")) return null; + if (fst.type_specifier() == null || fst.type_specifier().type_specifier_nonarray() == null) return null; + if (!isSamplerType(fst.type_specifier().type_specifier_nonarray().getText())) return null; + + final List names = new ArrayList<>(1); + for (GLSLParser.Typeless_declarationContext td : ShaderIndex.declarators(idl)) { + if (td.IDENTIFIER() != null) names.add(td.IDENTIFIER().getText()); + } + return names.isEmpty() ? null : new SamplerDecl(names, ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex()); + } + + private static boolean insideAny(List samplers, int pos) { + for (SamplerDecl s : samplers) { + if (pos >= s.start() && pos <= s.stop()) return true; + } + return false; + } + + private static boolean allUnreferenced(SamplerDecl s, List samplers, ShaderIndex index) { + for (String name : s.names()) { + for (Token tok : index.occurrences(name)) { + if (!insideAny(samplers, tok.getStartIndex())) return false; + } + } + return true; + } + + private static boolean anyIn(SamplerDecl s, Set names) { + for (String name : s.names()) { + if (names.contains(name)) return true; + } + return false; + } + private static boolean isSamplerType(String t) { return t.startsWith("sampler") || t.startsWith("isampler") || t.startsWith("usampler"); } diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java index dda7b38a6..dfd4ed24a 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderManager.java @@ -9,6 +9,7 @@ import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; import com.gtnewhorizons.angelica.glsm.shader.ShaderCacheIO; import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import com.gtnewhorizons.angelica.glsm.shader.SpirvCompiler; import com.gtnewhorizons.angelica.glsm.threading.AngelicaWorkers; import com.gtnewhorizons.angelica.sdlgpu.compute.VertexWriteReplayer; @@ -316,40 +317,40 @@ public final class ShaderManager { LOG.warn("glsl-transformation-lib parse failed for '{}': {}", debugName, e.getMessage()); return null; } - final List edits = new ArrayList<>(); - if (isGraphics(glShaderType)) { - PerFrameBlockInjector.collectEdits(root, perFrame, perPass, edits); - } - final GlslVulkanPreprocess.Metadata meta = GlslVulkanPreprocess.collectEdits(transformedSource, root, glShaderType, debugName, true, edits); - if (glShaderType == GL20.GL_VERTEX_SHADER) { - ClipZRemap.collectEdits(root, edits); - } - SamplerStripper.collectEdits(root, transformedSource, edits); - final String s = edits.isEmpty() ? transformedSource : GlslVulkanPreprocess.applyEdits(transformedSource, edits); - return new PrewarmTransformResult(s, meta.boolUniforms()); + return transform(transformedSource, 0, root, glShaderType, debugName, perFrame, perPass); } public static PrewarmTransformResult applyPrewarmTransformsFull(String finalSource, GLSLParser.Translation_unitContext bodyTree, int headerLen, int glShaderType, PerFrameUniformBlock perFrame, PerFrameUniformBlock perPass) { - final String header = finalSource.substring(0, headerLen); - final String body = finalSource.substring(headerLen); + return transform(finalSource, headerLen, bodyTree, glShaderType, "prewarm", perFrame, perPass); + } + + private static PrewarmTransformResult transform(String source, int headerLen, GLSLParser.Translation_unitContext tree, int glShaderType, String debugName, PerFrameUniformBlock perFrame, PerFrameUniformBlock perPass) { + final String body = source.substring(headerLen); + final ShaderIndex index = new ShaderIndex(tree, body); final List edits = new ArrayList<>(); if (isGraphics(glShaderType)) { - PerFrameBlockInjector.collectEdits(bodyTree, perFrame, perPass, edits); + PerFrameBlockInjector.collectEdits(index, perFrame, perPass, edits); } - final GlslVulkanPreprocess.Metadata meta = GlslVulkanPreprocess.collectEdits(null, bodyTree, glShaderType, "prewarm", true, edits); + final GlslVulkanPreprocess.Metadata meta = GlslVulkanPreprocess.collectEdits(index, glShaderType, debugName, true, edits); if (glShaderType == GL20.GL_VERTEX_SHADER) { - ClipZRemap.collectEdits(bodyTree, edits); + ClipZRemap.collectEdits(tree, edits); } - SamplerStripper.collectEdits(bodyTree, body, edits); - String outHeader = header; + SamplerStripper.collectEdits(index, edits); + if (glShaderType == GL20.GL_FRAGMENT_SHADER) { + DerivativeHoisting.collectEdits(index, edits); + } + if (headerLen == 0) { + return new PrewarmTransformResult(GlslVulkanPreprocess.applyEdits(source, edits), meta.boolUniforms()); + } + if (edits.isEmpty() && !meta.needsSamplerless()) { + return new PrewarmTransformResult(source, meta.boolUniforms()); + } + String header = source.substring(0, headerLen); if (meta.needsSamplerless()) { final int nl = header.indexOf('\n'); - outHeader = header.substring(0, nl) + "\n" + GlslVulkanPreprocess.SAMPLERLESS_EXTENSION + header.substring(nl); + header = header.substring(0, nl) + "\n" + GlslVulkanPreprocess.SAMPLERLESS_EXTENSION + header.substring(nl); } - if (edits.isEmpty() && outHeader == header) { - return new PrewarmTransformResult(finalSource, meta.boolUniforms()); - } - return new PrewarmTransformResult(outHeader + GlslVulkanPreprocess.applyEdits(body, edits), meta.boolUniforms()); + return new PrewarmTransformResult(header + GlslVulkanPreprocess.applyEdits(body, edits), meta.boolUniforms()); } public static void prewarmSpirv(String transformedSource, int glShaderType) { diff --git a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/UscaledRetype.java b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/UscaledRetype.java index a79d0a46c..4f117d53d 100644 --- a/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/UscaledRetype.java +++ b/sdl-gpu/src/main/java/com/gtnewhorizons/angelica/sdlgpu/shader/UscaledRetype.java @@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.sdlgpu.shader; import com.gtnewhorizons.angelica.glsm.GlslTransformUtils; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.ParseTreeWalker; @@ -60,16 +61,7 @@ public final class UscaledRetype { final GLSLParser.Single_declarationContext single = idl.single_declaration(); if (single == null || single.fully_specified_type() == null) return; final GLSLParser.Fully_specified_typeContext fst = single.fully_specified_type(); - if (fst.type_qualifier() == null) return; - - boolean isIn = false; - for (GLSLParser.Single_type_qualifierContext stq : fst.type_qualifier().single_type_qualifier()) { - if (stq.storage_qualifier() != null && "in".equals(stq.storage_qualifier().getText())) { - isIn = true; - break; - } - } - if (!isIn) return; + if (!ShaderIndex.hasStorageQualifier(fst.type_qualifier(), "in")) return; if (fst.type_specifier() == null || fst.type_specifier().type_specifier_nonarray() == null) return; if (single.typeless_declaration() == null || single.typeless_declaration().IDENTIFIER() == null) return; diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoistingTest.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoistingTest.java new file mode 100644 index 000000000..17f723684 --- /dev/null +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/DerivativeHoistingTest.java @@ -0,0 +1,179 @@ +package com.gtnewhorizons.angelica.sdlgpu.shader; + +import com.gtnewhorizons.angelica.glsm.GlslTransformUtils; +import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess; +import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; +import com.gtnewhorizons.angelica.glsm.shader.SpirvCompiler; +import org.junit.jupiter.api.Test; +import org.lwjgl.util.shaderc.Shaderc; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.lwjgl.system.MemoryUtil.memFree; + +class DerivativeHoistingTest { + + private static String hoist(String source) { + final List edits = new ArrayList<>(); + DerivativeHoisting.collectEdits(new ShaderIndex(GlslTransformUtils.parseFullQuiet(source), source), edits); + return GlslVulkanPreprocess.applyEdits(source, edits); + } + + private static void assertCompiles(String source) { + final SpirvCompiler.Result r = SpirvCompiler.compile(source, Shaderc.shaderc_fragment_shader, "derivatives.frag", SpirvCompiler.Options.vulkanForced460Core(), false); + assertNotNull(r.spirv(), () -> "compile failed: " + r.error() + "\n" + source); + memFree(r.spirv()); + } + + @Test + void directionalBlocklightDerivativesLeaveTheLightmapBranch() { + final String out = hoist(""" + #version 420 core + in vec2 lmCoord; + in vec3 viewIn; + out vec4 color; + float light(vec3 viewPos) { + vec2 oldLightmap = lmCoord; + float result = oldLightmap.x; + if (oldLightmap.x > 0.035) { + vec2 dFdBlock = vec2(dFdx(oldLightmap.x), dFdy(oldLightmap.x)); + vec3 dir = vec3(0.0); + if (length(dFdBlock) < 1e-6) { + dir = vec3(1.0); + } else { + dir = dFdx(viewPos) * dFdBlock.x + dFdy(viewPos) * dFdBlock.y; + } + result *= dot(normalize(dir), vec3(0.0, 1.0, 0.0)); + } + return result; + } + void main() { + color = vec4(light(viewIn)); + } + """); + + final int branch = out.indexOf("if (oldLightmap.x > 0.035)"); + assertTrue(branch > 0, out); + assertFalse(out.substring(branch).contains("dFdx("), out); + assertFalse(out.substring(branch).contains("dFdy("), out); + final String hoisted = out.substring(out.indexOf("float result"), branch); + assertTrue(hoisted.contains("float angelica_derivative_0 = dFdx(oldLightmap.x);"), out); + assertTrue(hoisted.contains("float angelica_derivative_1 = dFdy(oldLightmap.x);"), out); + assertTrue(hoisted.contains("vec3 angelica_derivative_2 = dFdx(viewPos);"), out); + assertTrue(hoisted.contains("vec3 angelica_derivative_3 = dFdy(viewPos);"), out); + assertCompiles(out); + } + + @Test + void variableWrittenInsideTheBranchStaysPut() { + final String source = """ + #version 420 core + in vec2 uv; + out vec4 color; + void bump(inout vec2 p) { p += 1.0; } + void main() { + vec2 a = uv; + vec2 b = uv; + if (uv.x > 0.5) { + a *= 2.0; + bump(b); + color = vec4(dFdx(a), dFdy(b)); + } + } + """; + assertEquals(source, hoist(source)); + } + + @Test + void writesHiddenInsideDirectivesBlockTheMove() { + final String source = """ + #version 420 core + in vec2 uv; + out vec4 color; + void main() { + vec2 p = uv; + if (true) { + #if 1 + p *= 2.0; + #endif + color = vec4(dFdx(p), 0.0, 1.0); + } + } + """; + assertEquals(source, hoist(source)); + } + + @Test + void parenthesizedBuiltinOutArgumentCountsAsAWrite() { + final String source = """ + #version 420 core + in vec2 uv; + out vec4 color; + void main() { + vec2 p = uv; + if (true) { + modf(uv * 2.0, (p)); + color = vec4(dFdx(p), 0.0, 1.0); + } + } + """; + assertEquals(source, hoist(source)); + } + + @Test + void globalsStayOnlyWhenACalleeWritesThem() { + final String out = hoist(""" + #version 420 core + in vec4 colorIn; + out vec4 color; + vec4 glColor = colorIn; + vec2 nudged = vec2(0.0); + float square(float x) { return x * x; } + void nudge() { nudged += 1.0; } + void touch() { nudge(); } + void main() { + if (glColor.a > 0.5) { + touch(); + color = vec4(dFdx(glColor.a) + square(1.0), dFdy(nudged), 1.0); + } + } + """); + + final int branch = out.indexOf("if (glColor.a > 0.5)"); + assertTrue(out.substring(0, branch).contains("float angelica_derivative_0 = dFdx(glColor.a);"), out); + assertTrue(out.substring(branch).contains("dFdy(nudged)"), out); + assertCompiles(out); + } + + @Test + void loopsAreNotCrossed() { + final String out = hoist(""" + #version 420 core + in vec2 uv; + out vec4 color; + void main() { + color = vec4(0.0); + if (uv.y > 0.5) { + for (int i = 0; i < 4; i++) { + vec2 p = uv * float(i); + if (p.x > 1.0) { + color.xy += dFdx(p); + } + } + } + } + """); + + final int loop = out.indexOf("for ("); + final int inner = out.indexOf("if (p.x > 1.0)"); + final int hoisted = out.indexOf("vec2 angelica_derivative_0 = dFdx(p);"); + assertTrue(loop < hoisted && hoisted < inner, out); + assertCompiles(out); + } +} diff --git a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderTransformChain.java b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderTransformChain.java index 304460a31..af1bce84a 100644 --- a/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderTransformChain.java +++ b/sdl-gpu/src/test/java/com/gtnewhorizons/angelica/sdlgpu/shader/ShaderTransformChain.java @@ -5,6 +5,7 @@ import com.gtnewhorizons.angelica.glsm.hooks.GLSMHooks; import com.gtnewhorizons.angelica.glsm.hooks.PerFrameUniformBlock; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess; import com.gtnewhorizons.angelica.glsm.shader.GlslVulkanPreprocess.Edit; +import com.gtnewhorizons.angelica.glsm.shader.ShaderIndex; import org.lwjgl.opengl.GL20; import org.taumc.glsl.grammar.GLSLParser; @@ -30,19 +31,19 @@ final class ShaderTransformChain { } static String clipZ(String source) { - return edit(source, ClipZRemap::collectEdits); + return edit(source, (index, edits) -> ClipZRemap.collectEdits(index.root(), edits)); } static String stripUnused(String source) { if (!source.contains("sampler")) return source; - return edit(source, (root, edits) -> SamplerStripper.collectEdits(root, source, edits)); + return edit(source, SamplerStripper::collectEdits); } static String inject(String source, PerFrameUniformBlock perFrame, PerFrameUniformBlock perPass) { - return edit(source, (root, edits) -> PerFrameBlockInjector.collectEdits(root, perFrame, perPass, edits)); + return edit(source, (index, edits) -> PerFrameBlockInjector.collectEdits(index, perFrame, perPass, edits)); } - private static String edit(String source, BiConsumer> collect) { + private static String edit(String source, BiConsumer> collect) { final GLSLParser.Translation_unitContext root; try { root = GlslTransformUtils.parseFullQuiet(source); @@ -50,7 +51,7 @@ final class ShaderTransformChain { return source; } final List edits = new ArrayList<>(); - collect.accept(root, edits); + collect.accept(new ShaderIndex(root, source), edits); return edits.isEmpty() ? source : GlslVulkanPreprocess.applyEdits(source, edits); } } diff --git a/src/main/java/net/coderbot/iris/pipeline/transform/RwImageStoreExtractor.java b/src/main/java/net/coderbot/iris/pipeline/transform/RwImageStoreExtractor.java index 3f1399d30..439b2e688 100644 --- a/src/main/java/net/coderbot/iris/pipeline/transform/RwImageStoreExtractor.java +++ b/src/main/java/net/coderbot/iris/pipeline/transform/RwImageStoreExtractor.java @@ -108,7 +108,7 @@ public final class RwImageStoreExtractor { } public static Result tryExtract(String source, PatchShaderType stage, String programName, RwExtractMode vertexMode) { - if (source == null) return null; + if (source == null || !mayWriteImages(source)) return null; if (stage != PatchShaderType.VERTEX && stage != PatchShaderType.FRAGMENT) return null; final ShaderParser.ParsedShader parsed = ShaderParser.parseShader(source); @@ -146,8 +146,12 @@ public final class RwImageStoreExtractor { return new Result(stripped, compute, writtenImages, mode, atomicImages(root, writtenImages)); } + private static boolean mayWriteImages(String source) { + return source.contains("imageStore") || source.contains("imageAtomic"); + } + public static Result tryExtractVertexReplay(String source) { - if (source == null) return null; + if (source == null || !mayWriteImages(source)) return null; final ShaderParser.ParsedShader parsed = ShaderParser.parseShader(source); final GLSLParser.Translation_unitContext root = parsed.full(); diff --git a/src/main/java/net/coderbot/iris/pipeline/transform/ShaderTransformer.java b/src/main/java/net/coderbot/iris/pipeline/transform/ShaderTransformer.java index 2a37cc9d6..12d33c115 100644 --- a/src/main/java/net/coderbot/iris/pipeline/transform/ShaderTransformer.java +++ b/src/main/java/net/coderbot/iris/pipeline/transform/ShaderTransformer.java @@ -756,12 +756,6 @@ public class ShaderTransformer { } } - public static void addIfNotExistsType(Transformer transformer, String name, String type) { - if (!transformer.hasVariable(name)) { - transformer.injectVariable(type + " " + name + ";"); - } - } - private static final class CeleritasHeader { static final String VALUE = computeCeleritasHeader(); }