mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-17 19:15:06 +00:00
Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
159f7ff8cc | ||
|
|
0d959c2b90 | ||
|
|
ce2462cad3 | ||
|
|
5457df5369 | ||
|
|
acedba4eac | ||
|
|
21a054a4db | ||
|
|
d056bf6fa7 | ||
|
|
5ed1c92590 | ||
|
|
3389b952d0 | ||
|
|
b49d37d827 | ||
|
|
12857006c6 | ||
|
|
38af64f10c |
No files matched your search
@@ -6,6 +6,8 @@ plugins {
|
||||
id("com.gtnewhorizons.gtnhconvention")
|
||||
}
|
||||
|
||||
apply(from = "gradle/mod-version.gradle.kts")
|
||||
|
||||
val lwjglDebug = false
|
||||
val gpuHud = false
|
||||
val renderdoc = false
|
||||
@@ -146,6 +148,17 @@ fun Test.configureAngelicaJava8() {
|
||||
}
|
||||
}
|
||||
|
||||
if (isMacOs && providers.gradleProperty("clientJvmVendor").isPresent) {
|
||||
for (javaVersion in listOf(17, 21, 25)) {
|
||||
tasks.named<JavaExec>("runClient$javaVersion") {
|
||||
javaLauncher = javaToolchains.launcherFor {
|
||||
languageVersion = JavaLanguageVersion.of(javaVersion)
|
||||
vendor = JvmVendorSpec.matching(providers.gradleProperty("clientJvmVendor").get())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform { excludeTags = setOf("gl-core") }
|
||||
configureAngelicaJava8()
|
||||
|
||||
@@ -62,6 +62,7 @@ public final class SystemProperties {
|
||||
public static final long FLYBY_TIME_OF_DAY = Math.floorMod(Long.getLong("angelica.flyby.timeOfDay", 6000L), 24000L);
|
||||
public static final String FLYBY_COMMANDS = System.getProperty("angelica.flyby.commands", "");
|
||||
public static final String FLYBY_ORIGIN = System.getProperty("angelica.flyby.origin", "");
|
||||
public static final Integer FLYBY_DIMENSION = Integer.getInteger("angelica.flyby.dimension");
|
||||
public static final FlybyPacing FLYBY_PACING = parseEnum("angelica.flyby.pacing", FlybyPacing.UNCAPPED, FlybyPacing.UNCAPPED, FlybyPacing.class);
|
||||
public static final FlybyWeather FLYBY_WEATHER = parseEnum("angelica.flyby.weather", FlybyWeather.CLEAR, FlybyWeather.CLEAR, FlybyWeather.class);
|
||||
public static final FlybyCamera FLYBY_CAMERA = parseEnum("angelica.flyby.camera", FlybyCamera.FIRST_PERSON, FlybyCamera.FIRST_PERSON, FlybyCamera.class);
|
||||
|
||||
@@ -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<GLSLParser.Typeless_declarationContext> 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());
|
||||
|
||||
@@ -6987,7 +6987,7 @@ public class GLStateManager {
|
||||
|
||||
private static void onBufferStorage(int target, int flags) {
|
||||
if ((flags & MAP_PERSISTENT_BIT) != 0) {
|
||||
markPersistentlyMapped(target);
|
||||
if (!RENDER_BACKEND.isBufferImmutable(bufferForTarget(ctx(), target))) markPersistentlyMapped(target);
|
||||
return;
|
||||
}
|
||||
onBufferRespec(target);
|
||||
@@ -7070,14 +7070,27 @@ public class GLStateManager {
|
||||
|
||||
public static void nglBufferSubData(int target, long offset, long size, long data) { glBufferSubData(target, offset, MemoryUtilities.memByteBuffer(data, checkedSize(size))); }
|
||||
|
||||
public static ByteBuffer glMapBuffer(int target, int access) { onBufferMap(target, access); return RENDER_BACKEND.mapBuffer(target, access); }
|
||||
public static ByteBuffer glMapBuffer(int target, int access) {
|
||||
final ByteBuffer buf = RENDER_BACKEND.mapBuffer(target, access);
|
||||
if (buf != null) onBufferMap(target, access);
|
||||
return buf;
|
||||
}
|
||||
public static ByteBuffer glMapBuffer(int target, int access, ByteBuffer old_buffer) { return glMapBuffer(target, access); }
|
||||
public static ByteBuffer glMapBuffer(int target, int access, long length, ByteBuffer old_buffer) { onBufferMap(target, access); return RENDER_BACKEND.mapBuffer(target, access, length, old_buffer); }
|
||||
public static ByteBuffer glMapBufferRange(int target, long offset, long length, int access) { onBufferMapRange(target, access); return RENDER_BACKEND.mapBufferRange(target, offset, length, access); }
|
||||
public static ByteBuffer glMapBuffer(int target, int access, long length, ByteBuffer old_buffer) {
|
||||
final ByteBuffer buf = RENDER_BACKEND.mapBuffer(target, access, length, old_buffer);
|
||||
if (buf != null) onBufferMap(target, access);
|
||||
return buf;
|
||||
}
|
||||
public static ByteBuffer glMapBufferRange(int target, long offset, long length, int access) {
|
||||
final ByteBuffer buf = RENDER_BACKEND.mapBufferRange(target, offset, length, access);
|
||||
if (buf != null) onBufferMapRange(target, access);
|
||||
return buf;
|
||||
}
|
||||
public static ByteBuffer glMapBufferRange(int target, long offset, long length, int access, ByteBuffer old_buffer) { return glMapBufferRange(target, offset, length, access); }
|
||||
public static long glMapBufferRangeAddress(int target, long offset, long length, int access) {
|
||||
onBufferMapRange(target, access);
|
||||
return RENDER_BACKEND.mapBufferRangeAddress(target, offset, length, access);
|
||||
final long address = RENDER_BACKEND.mapBufferRangeAddress(target, offset, length, access);
|
||||
if (address != 0L) onBufferMapRange(target, access);
|
||||
return address;
|
||||
}
|
||||
public static void glFlushMappedBufferRange(int target, long offset, long length) { RENDER_BACKEND.flushMappedBufferRange(target, offset, length); }
|
||||
public static boolean glUnmapBuffer(int target) { onBufferUnmap(target); return RENDER_BACKEND.unmapBuffer(target); }
|
||||
@@ -7466,8 +7479,8 @@ public class GLStateManager {
|
||||
}
|
||||
}
|
||||
public static long nglMapBuffer(int target, int access) {
|
||||
onBufferMap(target, access);
|
||||
final ByteBuffer buf = RENDER_BACKEND.mapBuffer(target, access);
|
||||
if (buf != null) onBufferMap(target, access);
|
||||
return buf == null ? 0L : MemoryUtilities.memAddress0(buf);
|
||||
}
|
||||
public static void nglMultiDrawElementsBaseVertex(int mode, long count, int type, long indices, int primcount, long basevertex) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -72,6 +76,10 @@ public class GlslTransformUtils {
|
||||
}
|
||||
|
||||
public static String renameReservedWords(String source, int targetVersion) {
|
||||
if (!containsWholeWord(source, "sample") && !containsWholeWord(source, "new")
|
||||
&& (targetVersion < 400 || !containsWholeWord(source, "sampler"))) {
|
||||
return source;
|
||||
}
|
||||
for (var entry : VERSIONED_RESERVED_WORDS.entrySet()) {
|
||||
// Rename identifiers that become reserved words at or above this GLSL version
|
||||
if (targetVersion >= entry.getKey()) {
|
||||
@@ -83,6 +91,23 @@ public class GlslTransformUtils {
|
||||
return source;
|
||||
}
|
||||
|
||||
static boolean containsWholeWord(String source, String word) {
|
||||
int from = 0;
|
||||
while (true) {
|
||||
final int match = source.indexOf(word, from);
|
||||
if (match < 0) return false;
|
||||
final int end = match + word.length();
|
||||
if ((match == 0 || !isAsciiWordChar(source.charAt(match - 1))) && (end == source.length() || !isAsciiWordChar(source.charAt(end)))) {
|
||||
return true;
|
||||
}
|
||||
from = match + 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAsciiWordChar(char c) {
|
||||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
|
||||
}
|
||||
|
||||
public static String restoreReservedWords(String source) {
|
||||
source = source.replace(RENAMED_PREFIX + "texture", "texture");
|
||||
source = source.replace(RENAMED_PREFIX + "sampler", "sampler");
|
||||
@@ -110,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<String> identifiersInDirectiveText(String source) {
|
||||
private static <P extends Parser, T> T parseQuiet(P parser, Function<P, T> 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<String> 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<String> 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) {
|
||||
|
||||
@@ -59,6 +59,7 @@ public abstract class RenderBackend {
|
||||
|
||||
public boolean framebufferCompletenessIsMeaningful() { return true; }
|
||||
public void onPersistentBufferWrite(int glId, long offset, long size) {}
|
||||
public boolean isBufferImmutable(int glId) { return false; }
|
||||
|
||||
public boolean isAnisotropicSupported() { return true; }
|
||||
|
||||
@@ -275,6 +276,12 @@ public abstract class RenderBackend {
|
||||
public void voxelizeRegion(long pass, boolean rebindVertexBuffer, float x, float y, float z, int rangeBase, int rangeCount, int vertexTotal) {}
|
||||
public void endVoxelizationBatch(long pass) {}
|
||||
|
||||
public void setVertexWriteReplay(int graphicsProgram, VertexWriteReplaySetup setup) {}
|
||||
|
||||
public boolean supportsComputeImageAtomics() { return true; }
|
||||
|
||||
public boolean supportsComputeImageAtomics(int width, int height, int depth) { return supportsComputeImageAtomics(); }
|
||||
|
||||
public abstract void enable(int cap);
|
||||
public abstract void enablei(int cap, int index);
|
||||
public abstract void disable(int cap);
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.gtnewhorizons.angelica.glsm.backend;
|
||||
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
/**
|
||||
* How to replay a graphics program's vertex-stage image writes.
|
||||
*
|
||||
* @param inputLocations per input, the declared location or -1 for whatever the graphics program linked
|
||||
* @param images per image the replay uses, in binding order: the written ones, then the read-only ones
|
||||
* @param writtenImageCount how many of {@code images} the replay writes
|
||||
* @param samplerNames samplers the graphics program may not have, because only the stripped writes read them
|
||||
* @param samplerUnits per sampler name, the texture unit it reads at each draw, or -1 to use samplerTextures
|
||||
* @param samplerTextures per sampler name, its texture when samplerUnits is -1
|
||||
* @param samplerObjects per sampler name, the GL sampler object to sample it with when samplerUnits is -1, or 0
|
||||
* @param vertexBufferBinding storage buffer binding of the first vertex buffer the replay program declares
|
||||
* @param vertexBufferCount how many vertex buffers it declares, at consecutive bindings
|
||||
* @param indexBufferBinding storage buffer binding of its index buffer
|
||||
*/
|
||||
public record VertexWriteReplaySetup(int replayProgram, String[] inputNames, int[] inputLocations,
|
||||
IntSupplier[] images, int writtenImageCount,
|
||||
String[] samplerNames, int[] samplerUnits, IntSupplier[] samplerTextures, int[] samplerObjects,
|
||||
int vertexBufferBinding, int vertexBufferCount, int indexBufferBinding) {
|
||||
}
|
||||
+90
-136
@@ -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<Edit> 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<String> boolUniforms, Set<String> explicitVsInputs, boolean needsSamplerless) {}
|
||||
|
||||
public static Metadata collectEdits(String source, GLSLParser.Translation_unitContext root, int glShaderType, String debugName, boolean separateReadOnlyImages, List<Edit> edits) {
|
||||
public static Metadata collectEdits(ShaderIndex index, int glShaderType, String debugName, boolean separateReadOnlyImages, List<Edit> edits) {
|
||||
final boolean isVertex = glShaderType == GL20.GL_VERTEX_SHADER;
|
||||
final Set<String> bools = new HashSet<>();
|
||||
final Set<String> explicitInputs = new HashSet<>();
|
||||
final Set<String> readOnlyImages = new HashSet<>();
|
||||
final boolean[] needsSamplerless = { false };
|
||||
boolean needsSamplerless = false;
|
||||
|
||||
final int[] maxExplicitLoc = { -1 };
|
||||
int maxExplicitLoc = -1;
|
||||
final List<Integer> 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<GLSLParser.Typeless_declarationContext> 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<Edit> edits) {
|
||||
for (Token tok : index.occurrences(name)) edits.add(new Edit(tok.getStartIndex(), tok.getStopIndex(), replacement));
|
||||
}
|
||||
|
||||
private static void rewriteReadOnlyImageCalls(List<GLSLParser.Postfix_expressionContext> calls, String replacement, Set<String> readOnlyImages, List<Edit> 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<String> 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<String> bools, Set<String> inputs, List<Edit> edits) {
|
||||
if (td.IDENTIFIER() == null) return;
|
||||
final String name = td.IDENTIFIER().getText();
|
||||
|
||||
@@ -106,14 +106,20 @@ public final class ProgramBinaryCache {
|
||||
}
|
||||
|
||||
public static void save(Key key, int program) {
|
||||
if (!saveBinary(key, program) && fullCompileWrites != null) {
|
||||
fullCompileWrites.add(CompletableFuture.completedFuture(false));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean saveBinary(Key key, int program) {
|
||||
final int length = GLStateManager.glGetProgrami(program, GL41.GL_PROGRAM_BINARY_LENGTH);
|
||||
if (length <= 0) return;
|
||||
if (length <= 0) return false;
|
||||
final ByteBuffer data = MemoryUtilities.memAlloc(length);
|
||||
final IntBuffer written = MemoryUtilities.memAllocInt(1);
|
||||
final IntBuffer format = MemoryUtilities.memAllocInt(1);
|
||||
try {
|
||||
GLStateManager.glGetProgramBinary(program, written, format, data);
|
||||
if (written.get(0) <= 0) return;
|
||||
if (written.get(0) <= 0) return false;
|
||||
data.limit(written.get(0));
|
||||
final byte[] binary = ShaderCacheIO.toHeap(data);
|
||||
final String formatTag = Integer.toString(format.get(0));
|
||||
@@ -130,6 +136,7 @@ public final class ProgramBinaryCache {
|
||||
});
|
||||
final List<CompletableFuture<Boolean>> writes = fullCompileWrites;
|
||||
if (writes != null) writes.add(write);
|
||||
return true;
|
||||
} finally {
|
||||
MemoryUtilities.memFree(data);
|
||||
MemoryUtilities.memFree(written);
|
||||
@@ -137,6 +144,18 @@ public final class ProgramBinaryCache {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isFullCompileRunning() {
|
||||
return keysInUse != null;
|
||||
}
|
||||
|
||||
public static boolean saveRetained(Key key, int program) {
|
||||
if (ShaderDiskCache.getBlob(key.diskKey) != null) {
|
||||
keep(key.diskKey);
|
||||
return true;
|
||||
}
|
||||
return saveBinary(key, program);
|
||||
}
|
||||
|
||||
public static void beginFullCompile() {
|
||||
fullCompileLayer = packLayer;
|
||||
keysInUse = new HashSet<>();
|
||||
|
||||
@@ -53,7 +53,10 @@ public final class ShaderDiskCache {
|
||||
|
||||
private ShaderDiskCache() {}
|
||||
|
||||
public static void configure(Path dir, String salt) {
|
||||
private static String configuredSalt;
|
||||
|
||||
public static synchronized void configure(Path dir, String salt) {
|
||||
if (dir.equals(root) && salt.equals(configuredSalt)) return;
|
||||
try {
|
||||
Files.createDirectories(dir);
|
||||
final Path saltFile = dir.resolve("salt");
|
||||
@@ -63,6 +66,7 @@ public final class ShaderDiskCache {
|
||||
Files.write(saltFile, salt.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
root = dir;
|
||||
configuredSalt = salt;
|
||||
AngelicaWorkers.run(() -> enforceCap(dir, MAX_BYTES));
|
||||
LOGGER.info("Shader disk cache at {}", dir);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
|
||||
@@ -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<String> 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<String, List<Token>> identifiers = new HashMap<>();
|
||||
private final Map<String, List<GLSLParser.Postfix_expressionContext>> calls = new HashMap<>();
|
||||
private final List<GLSLParser.DeclarationContext> 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<Token> occurrences(String name) {
|
||||
return identifiers.getOrDefault(name, List.of());
|
||||
}
|
||||
|
||||
public List<GLSLParser.Postfix_expressionContext> callsTo(String name) {
|
||||
return calls.getOrDefault(name, List.of());
|
||||
}
|
||||
|
||||
public List<GLSLParser.DeclarationContext> 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<String> 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<String> 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<GLSLParser.Typeless_declarationContext> declarators(GLSLParser.Init_declarator_listContext list) {
|
||||
final GLSLParser.Single_declarationContext single = list.single_declaration();
|
||||
final List<GLSLParser.Typeless_declarationContext> rest = list.typeless_declaration();
|
||||
if (single == null || single.typeless_declaration() == null) return rest;
|
||||
final List<GLSLParser.Typeless_declarationContext> 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;
|
||||
}
|
||||
}
|
||||
+2
-19
@@ -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();
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
@@ -16,6 +17,7 @@ import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public final class AngelicaWorkers {
|
||||
@@ -153,6 +155,51 @@ public final class AngelicaWorkers {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs every task and returns the results in task order.
|
||||
*/
|
||||
public static <T> List<T> invokeAll(List<? extends Supplier<? extends T>> tasks) {
|
||||
final int count = tasks.size();
|
||||
final Object[] results = new Object[count];
|
||||
if (count == 0) return new ArrayList<>();
|
||||
final AtomicInteger next = new AtomicInteger();
|
||||
final CountDownLatch finished = new CountDownLatch(count);
|
||||
final AtomicReference<Throwable> failure = new AtomicReference<>();
|
||||
final Runnable drain = () -> {
|
||||
int i;
|
||||
while ((i = next.getAndIncrement()) < count) {
|
||||
try {
|
||||
results[i] = tasks.get(i).get();
|
||||
} catch (Throwable t) {
|
||||
failure.compareAndSet(null, t);
|
||||
} finally {
|
||||
finished.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
final int helpers = Math.min(threads(), count - 1);
|
||||
for (int h = 0; h < helpers; h++) {
|
||||
run(drain);
|
||||
}
|
||||
drain.run();
|
||||
try {
|
||||
finished.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted waiting for worker tasks", e);
|
||||
}
|
||||
final Throwable t = failure.get();
|
||||
if (t instanceof RuntimeException re) throw re;
|
||||
if (t instanceof Error err) throw err;
|
||||
if (t != null) throw new IllegalStateException(t);
|
||||
final List<T> out = new ArrayList<>(count);
|
||||
for (Object result : results) {
|
||||
@SuppressWarnings("unchecked") final T typed = (T) result;
|
||||
out.add(typed);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static void checkIdleShutdown() {
|
||||
synchronized (lock) {
|
||||
final ExecutorService current = executor;
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
# LOCAL to test local config updates.
|
||||
gtnh.settings.blowdryerTag = 0.2.2
|
||||
|
||||
# See mod-version-gradle.kts
|
||||
gtnh.modules.gitVersion = false
|
||||
|
||||
# Human-readable mod name, available for mcmod.info population.
|
||||
modName = Angelica
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Replaces GTNHGradle's GitVersionModule (gtnh.modules.gitVersion = false): same version format, but only
|
||||
// x.y.z[-suffix] tags count, so additional tags (moltenvk-v*, tracy-v*) on master never become the mod version.
|
||||
|
||||
fun git(vararg args: String): String? = try {
|
||||
val exec = providers.exec {
|
||||
commandLine("git", *args)
|
||||
workingDir = rootDir
|
||||
isIgnoreExitValue = true
|
||||
}
|
||||
if (exec.result.get().exitValue == 0) exec.standardOutput.asText.get().trim() else null
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
fun gitVersion(): String {
|
||||
val described = git("describe", "--tags", "--long", "--first-parent", "--abbrev=7", "--match=[0-9]*.[0-9]*.[0-9]*", "HEAD")
|
||||
val match = described?.let { Regex("(.*)-([0-9]+)-g[0-9a-f]+").matchEntire(it) }
|
||||
val tag = match?.groupValues?.get(1)
|
||||
val distance = match?.groupValues?.get(2)?.toInt() ?: 0
|
||||
val hash = git("rev-parse", "HEAD")?.take(10)?.ifEmpty { null }
|
||||
val base = tag ?: hash
|
||||
if (base == null) {
|
||||
logger.error("No git repository found; set the VERSION environment variable to version this build.")
|
||||
return "NO-GIT-TAG-SET"
|
||||
}
|
||||
val dirty = !git("status", "--porcelain").isNullOrEmpty()
|
||||
val branch = (git("branch", "--show-current")?.ifEmpty { null } ?: System.getenv("GIT_BRANCH") ?: "git")
|
||||
.removePrefix("origin/")
|
||||
.replace(Regex("[^a-zA-Z0-9-]+"), "-")
|
||||
return when {
|
||||
distance > 0 -> "$base-$branch.$distance+$hash${if (dirty) "-dirty" else ""}"
|
||||
dirty -> "$base-$branch+$hash-dirty"
|
||||
else -> base
|
||||
}
|
||||
}
|
||||
|
||||
extra["modVersion"] = providers.environmentVariable("VERSION").orNull ?: gitVersion()
|
||||
@@ -10,6 +10,8 @@
|
||||
# length - Units: blocks (straight), blocks per leg (circuit), degrees (pan), ticks (static).
|
||||
# extra - passed through to gradle; -Dangelica.* reaches the client (e.g. -Dangelica.flyby.timeOfDay=1500).
|
||||
# -Dangelica.flyby.camera=FIRST_PERSON|THIRD_PERSON_BACK|THIRD_PERSON_FRONT (default FIRST_PERSON).
|
||||
# -Dangelica.flyby.dimension=<id> moves the player there first (1 = End), after world saving is off.
|
||||
# -PclientJvmVendor=adoptium (macOS) picks the JVM vendor for runClient17/21/25.
|
||||
#
|
||||
# FLYBY_SPEED - blocks per tick. (0.5 ~= creative flight).
|
||||
# FLYBY_PACING - UNCAPPED | CONFIGURED. Passed through when set.
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# End crystals next to the arrival platform. Use with -Dangelica.flyby.dimension=1, route pan.
|
||||
summon EnderCrystal ~ ~2 ~12 {ForgeData:{flyby:1b}}
|
||||
summon EnderCrystal ~8 ~2 ~8 {ForgeData:{flyby:1b}}
|
||||
summon EnderCrystal ~-8 ~4 ~10 {ForgeData:{flyby:1b}}
|
||||
+254
-134
@@ -1,6 +1,7 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.hooks.GLSMHooks;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.compute.VertexWriteReplayer;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.compute.VoxelizationDispatcher;
|
||||
import com.gtnewhorizons.angelica.config.SystemProperties;
|
||||
import com.gtnewhorizons.angelica.glsm.CaptureGate;
|
||||
@@ -10,6 +11,7 @@ import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.BackendOptions;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.GLDebugMessageListener;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.MainThreadPump;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VertexWriteReplaySetup;
|
||||
import me.eigenraven.lwjgl3ify.client.MainThreadExec;
|
||||
import org.lwjglx.Lwjgl3ifyEventLoop;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VSyncMode;
|
||||
@@ -41,6 +43,7 @@ import com.gtnewhorizons.angelica.sdlgpu.resource.FBOClearTracker;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.FboState;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.FormatMap;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.Image3DClear;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.MappedRange;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.PersistentBufferSync;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.PackState;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.PersistentMapping;
|
||||
@@ -64,6 +67,7 @@ import com.gtnewhorizons.angelica.sdlgpu.util.ThreadRegistry;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.longs.LongArrayFIFOQueue;
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -162,12 +166,14 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
@Override public long nextSeq() { return TransferThread.nextSeq(); }
|
||||
});
|
||||
private final PipelineApplier pipelineApplier = new PipelineApplier(frameManager, resourceManager, shaderManager, pipelineStore, fboClearTracker, persistentSync, samplerBinder, storageTextureBinder, storageBufferBinder);
|
||||
private final VertexWriteReplayer vertexWriteReplayer = new VertexWriteReplayer(shaderManager, resourceManager, pipelineApplier, computeBinder, SDLGPURenderBackend::s);
|
||||
private final AttachmentClear attachmentClear = new AttachmentClear(frameManager, pipelineStore, shaderManager);
|
||||
private final DepthStencilReadback depthStencilReadback = new DepthStencilReadback(frameManager, resourceManager, pipelineStore, shaderManager, fboClearTracker);
|
||||
private final DrawDispatch drawDispatch = new DrawDispatch(device, frameManager, resourceManager, pipelineApplier, this::enqueuePreCopied);
|
||||
private final FFPTrace ffpTrace = SystemProperties.FFP_TRACE ? new FFPTrace(resourceManager) : null;
|
||||
|
||||
private final IntOpenHashSet missingProgramWarned = new IntOpenHashSet();
|
||||
private final LongOpenHashSet warnedBufferOps = new LongOpenHashSet();
|
||||
|
||||
private boolean droppedCopyWarned;
|
||||
private boolean droppedSsboWriteWarned;
|
||||
@@ -248,7 +254,9 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
|
||||
|
||||
@Override public void onPersistentBufferWrite(int glId, long offset, long size) {
|
||||
persistentSync.onPersistentBufferWrite(glId, offset, size);
|
||||
if (!persistentSync.onPersistentBufferWrite(glId, offset, size)) {
|
||||
warnInvalidBufferOp("persistent write out of range", glId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onPostWindowCreate(long window) {
|
||||
@@ -296,6 +304,8 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
});
|
||||
pipelineApplier.setDeferredUploadSink(this::enqueuePreCopied);
|
||||
frameManager.setRenderPassEndHook(vertexWriteReplayer::flush);
|
||||
resourceManager.setTextureReplaceHook(this::flushVertexWritesBeforeTextureChange);
|
||||
frameManager.setPreRenderPassHook(() -> {
|
||||
final ContextState st = s();
|
||||
pipelineApplier.flushUniformBlocks(st);
|
||||
@@ -515,14 +525,13 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
st.appliedLogicOpKey = 0;
|
||||
st.fragSamplerBindings.free();
|
||||
st.vertSamplerBindings.free();
|
||||
if (st.mappedStagingBuffer != null) {
|
||||
MemoryUtil.memFree(st.mappedStagingBuffer);
|
||||
}
|
||||
clearMappedState(st);
|
||||
}
|
||||
registeredStates.clear();
|
||||
|
||||
drawDispatch.clearWarnedState();
|
||||
synchronized (warnedBufferOps) {
|
||||
warnedBufferOps.clear();
|
||||
}
|
||||
attachmentClear.shutdown();
|
||||
depthStencilReadback.shutdown();
|
||||
textureOps.shutdown();
|
||||
@@ -1203,9 +1212,11 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
if (st.rasterizerDiscard) return;
|
||||
final FrameState f = frameManager.frame();
|
||||
if (!f.frameActive) { f.droppedDrawsThisFrame++; return; }
|
||||
prepareVertexWrites(st);
|
||||
if (mode == GL11.GL_TRIANGLE_FAN && count >= 3) {
|
||||
if (!drawDispatch.drawTriangleFanAsTriangleList(st, first, count)) { f.droppedDrawsThisFrame++; return; }
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "arraysFan", mode, count, 0, first, 0);
|
||||
captureVertexWrites(st, first, count, 1, 0, 0, 0);
|
||||
return;
|
||||
}
|
||||
drawDispatch.setPrimitiveTypeForDraw(st, FormatMap.mapPrimitiveType(mode));
|
||||
@@ -1214,6 +1225,7 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
if (!pipelineApplier.applyPipelineAndState(st, f)) { f.droppedDrawsThisFrame++; return; }
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "arrays", mode, count, 0, first, 0);
|
||||
SDL_DrawGPUPrimitives(f.renderPass, count, 1, first, 0);
|
||||
captureVertexWrites(st, first, count, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
@Override public void drawElements(int mode, int count, int type, long indices) {
|
||||
@@ -1223,6 +1235,7 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
final int firstIndex = (int) (indices / FormatMap.indexElementSize(type));
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elements", mode, count, type, firstIndex, 0);
|
||||
drawDispatch.issueIndexedDraw(st, rp, st.currentVao.elementBuffer, type, count, 1, firstIndex, 0);
|
||||
captureVertexWrites(st, 0, count, 1, type, indices, 0);
|
||||
}
|
||||
|
||||
@Override public void drawElementsInstanced(int mode, int count, int type, long indices, int primcount) {
|
||||
@@ -1232,6 +1245,7 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
final int firstIndex = (int) (indices / FormatMap.indexElementSize(type));
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elementsInstanced", mode, count, type, firstIndex, 0);
|
||||
drawDispatch.issueIndexedDraw(st, rp, st.currentVao.elementBuffer, type, count, primcount, firstIndex, 0);
|
||||
captureVertexWrites(st, 0, count, primcount, type, indices, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -1242,10 +1256,12 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
final int firstIndex = (int) (indices / FormatMap.indexElementSize(type));
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elementsBaseVertex", mode, count, type, firstIndex, baseVertex);
|
||||
drawDispatch.issueIndexedDraw(st, rp, st.currentVao.elementBuffer, type, count, 1, firstIndex, baseVertex);
|
||||
captureVertexWrites(st, 0, count, 1, type, indices, baseVertex);
|
||||
}
|
||||
|
||||
private long prepareIndexedDrawBind(ContextState st, int mode, int type, String opName) {
|
||||
if (st.rasterizerDiscard) return 0;
|
||||
prepareVertexWrites(st);
|
||||
if (!drawDispatch.prepareIndexedDraw(st, mode, type)) return 0;
|
||||
final int ebo = st.currentVao.elementBuffer;
|
||||
final long eboHandle = resourceManager.getBufferHandle(ebo);
|
||||
@@ -1274,6 +1290,7 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
final int baseVertex = MemoryUtil.memGetInt(pBaseVertex + offset);
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "multiElementsBaseVertex", mode, count, type, firstIndex, baseVertex);
|
||||
drawDispatch.issueIndexedDraw(st, rp, ebo, type, count, 1, firstIndex, baseVertex);
|
||||
captureVertexWrites(st, 0, count, 1, type, indices, baseVertex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1300,6 +1317,7 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "elementsIndirect", mode, drawcount, type, (int) indirect, 0);
|
||||
SDL_DrawGPUIndexedPrimitivesIndirect(rp, indirectHandle, (int) indirect, drawcount);
|
||||
if (st.boundProgramObj != null && st.boundProgramObj.vertexWriteReplay != null) vertexWriteReplayer.noteIndirectDraw();
|
||||
} finally {
|
||||
Tracy.endZone();
|
||||
}
|
||||
@@ -1333,6 +1351,12 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
final int srcGlId = getBoundBuffer(readTarget);
|
||||
final int dstGlId = getBoundBuffer(writeTarget);
|
||||
if (srcGlId == 0 || dstGlId == 0 || size <= 0) return;
|
||||
final long srcSize = resourceManager.getBufferSize(srcGlId);
|
||||
final long dstSize = resourceManager.getBufferSize(dstGlId);
|
||||
if (readOffset < 0 || writeOffset < 0 || (srcSize > 0 && (readOffset > srcSize || size > srcSize - readOffset)) || (dstSize > 0 && (writeOffset > dstSize || size > dstSize - writeOffset))) {
|
||||
warnInvalidBufferOp("glCopyBufferSubData out of range", dstGlId);
|
||||
return;
|
||||
}
|
||||
resourceManager.dropArrayShadow(dstGlId);
|
||||
resourceManager.dropEboShadow(dstGlId);
|
||||
resourceManager.markBufferContentsDefined(dstGlId);
|
||||
@@ -1347,7 +1371,7 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
deferredCopyOp.dstHandle = dstHandle;
|
||||
deferredCopyOp.writeOffset = writeOffset;
|
||||
deferredCopyOp.seq = seq;
|
||||
final boolean handled = resourceManager.enqueuePersistentCopyDeferred(srcGlId, dstGlId, seq, deferredCopyOp);
|
||||
final boolean handled = resourceManager.enqueuePersistentCopyDeferred(srcGlId, dstGlId, seq, readOffset, size, deferredCopyOp);
|
||||
if (handled) {
|
||||
wakeTransferThread();
|
||||
return;
|
||||
@@ -1408,6 +1432,11 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
resourceManager.deleteTexture(texture);
|
||||
}
|
||||
|
||||
/** Captured vertex-write replays name textures by GL id, which a delete or reallocation would make mean something else. */
|
||||
private void flushVertexWritesBeforeTextureChange() {
|
||||
if (vertexWriteReplayer.hasPending()) frameManager.endRenderPassIfActive(FrameManager.PASS_END_COMPUTE);
|
||||
}
|
||||
|
||||
|
||||
@Override public void bindTexture(int target, int texture) {
|
||||
if (isProxyTarget(target)) return;
|
||||
@@ -2761,8 +2790,10 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
|
||||
private void deleteBuffer(int buffer) {
|
||||
dropMappingFor(buffer);
|
||||
if (buffer != 0) unbindDeletedBuffer(s(), buffer);
|
||||
if (buffer != 0) {
|
||||
freePlainMappingFor(buffer, false);
|
||||
unbindDeletedBuffer(s(), buffer);
|
||||
}
|
||||
readbackShadows.release(buffer);
|
||||
resourceManager.deleteBuffer(buffer);
|
||||
}
|
||||
@@ -2809,12 +2840,6 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
if (ssboChanged) st.ssboBindGen++;
|
||||
}
|
||||
|
||||
private static void dropMappingFor(int buffer) {
|
||||
final ContextState st = s();
|
||||
if (buffer == 0 || st.mappedBufferGlId != buffer || st.mappedStagingBuffer == null) return;
|
||||
MemoryUtil.memFree(st.mappedStagingBuffer);
|
||||
clearMappedState(st);
|
||||
}
|
||||
@Override public void bindBuffer(int target, int buffer) {
|
||||
final ContextState st = s();
|
||||
switch (target) {
|
||||
@@ -2914,8 +2939,15 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
|
||||
@Override public void bufferData(int target, long size, int usage) {
|
||||
final int glId = getBoundBuffer(target);
|
||||
final ContextState cs = s();
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
if (glId == 0) return;
|
||||
if (refuseImmutable(glId, "glBufferData on immutable storage")) return;
|
||||
respecifyStore(cs, target, glId, size, usage);
|
||||
}
|
||||
|
||||
private void respecifyStore(ContextState cs, int target, int glId, long size, int usage) {
|
||||
freePlainMappingFor(glId, true);
|
||||
resourceManager.recordBufferGlParams(glId, usage, BufferParams.MUTABLE_STORE);
|
||||
if (target == GL31.GL_UNIFORM_BUFFER) {
|
||||
if (resourceManager.getBufferSize(glId) == size) return;
|
||||
@@ -2931,14 +2963,45 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
readbackShadows.release(glId);
|
||||
resourceManager.deleteBuffer(glId);
|
||||
resourceManager.createBuffer(glId, sdlUsage, size);
|
||||
if (target == GL15.GL_ARRAY_BUFFER) s().bumpAttribStateGen();
|
||||
if (target == GL15.GL_ARRAY_BUFFER) cs.bumpAttribStateGen();
|
||||
}
|
||||
|
||||
private void freePlainMappingFor(int glId, boolean warn) {
|
||||
final ByteBuffer staging = resourceManager.takePlainStaging(glId);
|
||||
if (staging == null) return;
|
||||
MemoryUtil.memFree(staging);
|
||||
if (warn) warnInvalidBufferOp("glBufferData on mapped buffer", glId);
|
||||
}
|
||||
|
||||
private boolean refuseImmutable(int glId, String op) {
|
||||
if (resourceManager.getBufferStorageFlags(glId) == BufferParams.MUTABLE_STORE) return false;
|
||||
warnInvalidBufferOp(op, glId);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override public boolean isBufferImmutable(int glId) {
|
||||
return resourceManager.getBufferStorageFlags(glId) != BufferParams.MUTABLE_STORE;
|
||||
}
|
||||
|
||||
private void warnInvalidBufferOp(String op, int glId) {
|
||||
synchronized (warnedBufferOps) {
|
||||
if (warnedBufferOps.add(((long) op.hashCode() << 32) | (glId & 0xFFFFFFFFL))) {
|
||||
LOG.warn("{} (buffer {})", op, glId, new Throwable());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void bufferData(int target, ByteBuffer data, int usage) {
|
||||
final ContextState cs = s();
|
||||
final int glId = getBoundBuffer(target);
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
if (glId == 0 || data == null) return;
|
||||
if (refuseImmutable(glId, "glBufferData on immutable storage")) return;
|
||||
respecifyStore(cs, target, glId, data, usage);
|
||||
}
|
||||
|
||||
private void respecifyStore(ContextState cs, int target, int glId, ByteBuffer data, int usage) {
|
||||
final long dataSize = data.remaining();
|
||||
freePlainMappingFor(glId, true);
|
||||
resourceManager.recordBufferGlParams(glId, usage, BufferParams.MUTABLE_STORE);
|
||||
if (target == GL31.GL_UNIFORM_BUFFER) {
|
||||
if (dataSize > 0) {
|
||||
@@ -3028,11 +3091,15 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
|
||||
@Override public void bufferSubData(int target, long offset, ByteBuffer data) {
|
||||
final ContextState cs = s();
|
||||
final int glId = getBoundBuffer(target);
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
if (glId == 0 || data == null) return;
|
||||
if (target == GL31.GL_UNIFORM_BUFFER) {
|
||||
final ByteBuffer shadow = resourceManager.getUboShadow(glId);
|
||||
if (shadow != null) {
|
||||
if (offset < 0 || offset + data.remaining() > shadow.capacity()) {
|
||||
warnInvalidBufferOp("glBufferSubData out of range on uniform buffer", glId);
|
||||
return;
|
||||
}
|
||||
shadow.position(0);
|
||||
ByteRegionCopy.copyByteRegion(data, data.position(), shadow, (int) offset, data.remaining());
|
||||
}
|
||||
@@ -3101,133 +3168,132 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
|
||||
@Override public boolean unmapBuffer(int target) {
|
||||
final ContextState cs = s();
|
||||
final int glId = getBoundBuffer(target);
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
|
||||
final PersistentMapping pm = resourceManager.removePersistentMapping(glId);
|
||||
if (pm != null) {
|
||||
resourceManager.releasePersistentStaging(pm);
|
||||
if (resourceManager.getPersistentMapping(glId) != null) {
|
||||
final PersistentMapping pm = resourceManager.removePersistentMapping(glId);
|
||||
if (pm != null) resourceManager.releasePersistentStaging(pm, glId, "unmap");
|
||||
return true;
|
||||
}
|
||||
|
||||
final ContextState st = cs;
|
||||
if (st.mappedStagingBuffer == null) return false;
|
||||
final MappedRange m = cs.mappingScratch;
|
||||
if (!resourceManager.takePlainMapping(glId, m)) {
|
||||
warnInvalidBufferOp("glUnmapBuffer on unmapped buffer", glId);
|
||||
return false;
|
||||
}
|
||||
|
||||
final boolean writeMap = (st.mappedAccessFlags & GL30.GL_MAP_WRITE_BIT) != 0;
|
||||
final ByteBuffer staging = m.staging;
|
||||
m.staging = null;
|
||||
final boolean writeMap = (m.accessFlags & GL30.GL_MAP_WRITE_BIT) != 0;
|
||||
|
||||
if (target == GL30.GL_PIXEL_UNPACK_BUFFER) {
|
||||
st.mappedStagingBuffer.position(0).limit((int) st.mappedLength);
|
||||
final long gpuHandle = resourceManager.getBufferHandle(st.mappedBufferGlId);
|
||||
staging.position(0).limit((int) m.length);
|
||||
final long gpuHandle = resourceManager.getBufferHandle(glId);
|
||||
if (writeMap && gpuHandle != 0) {
|
||||
if (st.deferUploads) {
|
||||
enqueuePreCopied(st.mappedStagingBuffer, gpuHandle, st.mappedOffset, false);
|
||||
if (cs.deferUploads) {
|
||||
enqueuePreCopied(staging, gpuHandle, m.offset, false);
|
||||
} else if (frameManager.getCommandBuffer() != 0) {
|
||||
resourceManager.uploadToBuffer(frameManager.ensureCopyPass(), st.mappedStagingBuffer, gpuHandle, st.mappedOffset, false);
|
||||
resourceManager.uploadToBuffer(frameManager.ensureCopyPass(), staging, gpuHandle, m.offset, false);
|
||||
}
|
||||
resourceManager.markBufferContentsDefined(st.mappedBufferGlId);
|
||||
resourceManager.markBufferContentsDefined(glId);
|
||||
}
|
||||
final ByteBuffer old = resourceManager.putPboStaging(glId, st.mappedStagingBuffer);
|
||||
final ByteBuffer old = resourceManager.putPboStaging(glId, staging);
|
||||
if (old != null) MemoryUtil.memFree(old);
|
||||
clearMappedState(st);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (target == GL31.GL_UNIFORM_BUFFER) {
|
||||
final ByteBuffer shadow = resourceManager.getUboShadow(st.mappedBufferGlId);
|
||||
if (writeMap && shadow != null && st.mappedOffset + st.mappedLength <= shadow.capacity()) {
|
||||
st.mappedStagingBuffer.position(0).limit((int) st.mappedLength);
|
||||
shadow.position((int) st.mappedOffset).limit((int) (st.mappedOffset + st.mappedLength));
|
||||
shadow.put(st.mappedStagingBuffer);
|
||||
final ByteBuffer shadow = resourceManager.getUboShadow(glId);
|
||||
if (writeMap && shadow != null && m.offset + m.length <= shadow.capacity()) {
|
||||
staging.position(0).limit((int) m.length);
|
||||
shadow.position((int) m.offset).limit((int) (m.offset + m.length));
|
||||
shadow.put(staging);
|
||||
shadow.position(0).limit(shadow.capacity());
|
||||
resourceManager.markBufferContentsDefined(st.mappedBufferGlId);
|
||||
resourceManager.markBufferContentsDefined(glId);
|
||||
}
|
||||
MemoryUtil.memFree(st.mappedStagingBuffer);
|
||||
clearMappedState(st);
|
||||
MemoryUtil.memFree(staging);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!writeMap) {
|
||||
MemoryUtil.memFree(st.mappedStagingBuffer);
|
||||
clearMappedState(st);
|
||||
MemoryUtil.memFree(staging);
|
||||
return true;
|
||||
}
|
||||
|
||||
st.mappedStagingBuffer.position(0).limit((int) st.mappedLength);
|
||||
staging.position(0).limit((int) m.length);
|
||||
final long gpuHandle;
|
||||
if (st.mappedInvalidate) {
|
||||
if (m.invalidate) {
|
||||
final int sdlUsage = FormatMap.mapBufferUsage(0);
|
||||
final long glSize = Math.max(resourceManager.getBufferSize(st.mappedBufferGlId), st.mappedLength);
|
||||
final int glUsage = resourceManager.getBufferGlUsage(st.mappedBufferGlId);
|
||||
final int storageFlags = resourceManager.getBufferStorageFlags(st.mappedBufferGlId);
|
||||
resourceManager.deleteBuffer(st.mappedBufferGlId);
|
||||
gpuHandle = resourceManager.createBuffer(st.mappedBufferGlId, sdlUsage, glSize);
|
||||
resourceManager.recordBufferGlParams(st.mappedBufferGlId, glUsage, storageFlags);
|
||||
st.bumpAttribStateGen();
|
||||
final long glSize = Math.max(resourceManager.getBufferSize(glId), m.length);
|
||||
final int glUsage = resourceManager.getBufferGlUsage(glId);
|
||||
final int storageFlags = resourceManager.getBufferStorageFlags(glId);
|
||||
resourceManager.deleteBuffer(glId);
|
||||
gpuHandle = resourceManager.createBuffer(glId, sdlUsage, glSize);
|
||||
resourceManager.recordBufferGlParams(glId, glUsage, storageFlags);
|
||||
cs.bumpAttribStateGen();
|
||||
} else {
|
||||
gpuHandle = resourceManager.getBufferHandle(st.mappedBufferGlId);
|
||||
gpuHandle = resourceManager.getBufferHandle(glId);
|
||||
}
|
||||
if (target == GL15.GL_ELEMENT_ARRAY_BUFFER
|
||||
&& (cs.primitiveRestartEnabled || resourceManager.hasEboShadow(st.mappedBufferGlId))) {
|
||||
final int len = (int) st.mappedLength;
|
||||
st.mappedStagingBuffer.position(0).limit(len);
|
||||
persistentSync.mirrorEboShadow(st.mappedBufferGlId, st.mappedStagingBuffer, (int) st.mappedOffset, len);
|
||||
st.mappedStagingBuffer.position(0).limit(len);
|
||||
&& (cs.primitiveRestartEnabled || resourceManager.hasEboShadow(glId))) {
|
||||
final int len = (int) m.length;
|
||||
staging.position(0).limit(len);
|
||||
persistentSync.mirrorEboShadow(glId, staging, (int) m.offset, len);
|
||||
staging.position(0).limit(len);
|
||||
}
|
||||
if (target == GL15.GL_ARRAY_BUFFER && resourceManager.hasArrayShadow(st.mappedBufferGlId)) {
|
||||
final int len = (int) st.mappedLength;
|
||||
st.mappedStagingBuffer.position(0).limit(len);
|
||||
resourceManager.mirrorArrayShadowRegion(st.mappedBufferGlId, st.mappedStagingBuffer, (int) st.mappedOffset);
|
||||
st.mappedStagingBuffer.position(0).limit(len);
|
||||
if (target == GL15.GL_ARRAY_BUFFER && resourceManager.hasArrayShadow(glId)) {
|
||||
final int len = (int) m.length;
|
||||
staging.position(0).limit(len);
|
||||
resourceManager.mirrorArrayShadowRegion(glId, staging, (int) m.offset);
|
||||
staging.position(0).limit(len);
|
||||
}
|
||||
if (gpuHandle != 0) {
|
||||
if (st.deferUploads) {
|
||||
enqueuePreCopied(st.mappedStagingBuffer, gpuHandle, st.mappedOffset, false);
|
||||
if (cs.deferUploads) {
|
||||
enqueuePreCopied(staging, gpuHandle, m.offset, false);
|
||||
} else if (frameManager.getCommandBuffer() != 0) {
|
||||
resourceManager.uploadToBuffer(frameManager.ensureCopyPass(), st.mappedStagingBuffer, gpuHandle, st.mappedOffset, false);
|
||||
resourceManager.uploadToBuffer(frameManager.ensureCopyPass(), staging, gpuHandle, m.offset, false);
|
||||
}
|
||||
resourceManager.markBufferContentsDefined(st.mappedBufferGlId);
|
||||
resourceManager.markBufferContentsDefined(glId);
|
||||
}
|
||||
|
||||
MemoryUtil.memFree(st.mappedStagingBuffer);
|
||||
clearMappedState(st);
|
||||
MemoryUtil.memFree(staging);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void clearMappedState(ContextState st) {
|
||||
st.mappedStagingBuffer = null;
|
||||
st.mappedBufferGlId = 0;
|
||||
st.mappedOffset = 0;
|
||||
st.mappedLength = 0;
|
||||
st.mappedInvalidate = false;
|
||||
st.mappedAccessFlags = 0;
|
||||
}
|
||||
|
||||
@Override public ByteBuffer mapBufferRange(int target, long offset, long length, int access) {
|
||||
final int glId = getBoundBuffer(target);
|
||||
final ContextState cs = s();
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
if (glId == 0 || length <= 0) return null;
|
||||
|
||||
final boolean persistent = (access & GL44.GL_MAP_PERSISTENT_BIT) != 0;
|
||||
|
||||
if (persistent) {
|
||||
if (target == GL15.GL_ARRAY_BUFFER) resourceManager.dropArrayShadow(glId);
|
||||
final int storageFlags = resourceManager.getBufferStorageFlags(glId);
|
||||
if (storageFlags < 0 || (storageFlags & GL44.GL_MAP_PERSISTENT_BIT) == 0) {
|
||||
warnInvalidBufferOp("glMapBufferRange persistent without persistent storage", glId);
|
||||
return null;
|
||||
}
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc((int) length);
|
||||
final PersistentMapping fresh = new PersistentMapping(staging, offset, length, access);
|
||||
final PersistentMapping prior = resourceManager.swapPersistentMapping(glId, fresh);
|
||||
if (prior != null) resourceManager.releasePersistentStaging(prior);
|
||||
if (!resourceManager.putPersistentMappingIfAbsent(glId, fresh)) {
|
||||
MemoryUtil.memFree(staging);
|
||||
warnInvalidBufferOp("glMapBufferRange on mapped buffer", glId);
|
||||
return null;
|
||||
}
|
||||
if (target == GL15.GL_ARRAY_BUFFER) resourceManager.dropArrayShadow(glId);
|
||||
return staging;
|
||||
}
|
||||
|
||||
final ContextState st = s();
|
||||
if (st.mappedStagingBuffer != null) MemoryUtil.memFree(st.mappedStagingBuffer);
|
||||
st.mappedStagingBuffer = MemoryUtil.memAlloc((int) length);
|
||||
st.mappedBufferGlId = glId;
|
||||
st.mappedOffset = offset;
|
||||
st.mappedLength = length;
|
||||
st.mappedInvalidate = (access & GL30.GL_MAP_INVALIDATE_BUFFER_BIT) != 0;
|
||||
st.mappedAccessFlags = access;
|
||||
final ByteBuffer staging = MemoryUtil.memAlloc((int) length);
|
||||
if ((access & (GL30.GL_MAP_INVALIDATE_BUFFER_BIT | GL30.GL_MAP_INVALIDATE_RANGE_BIT)) == 0) {
|
||||
seedMappedStaging(target, glId, offset, length, st.mappedStagingBuffer);
|
||||
seedMappedStaging(target, glId, offset, length, staging);
|
||||
}
|
||||
return st.mappedStagingBuffer;
|
||||
if (!resourceManager.putPlainMappingIfAbsent(glId, staging, offset, length, (access & GL30.GL_MAP_INVALIDATE_BUFFER_BIT) != 0, access)) {
|
||||
MemoryUtil.memFree(staging);
|
||||
warnInvalidBufferOp("glMapBufferRange on mapped buffer", glId);
|
||||
return null;
|
||||
}
|
||||
return staging;
|
||||
}
|
||||
|
||||
private void seedMappedStaging(int target, int glId, long offset, long length, ByteBuffer staging) {
|
||||
@@ -3315,22 +3381,31 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
@Override public void flushMappedBufferRange(int target, long offset, long length) {
|
||||
final int glId = getBoundBuffer(target);
|
||||
if (glId == 0 || length <= 0) return;
|
||||
persistentSync.onPersistentBufferWrite(glId, offset, length);
|
||||
if (!persistentSync.onPersistentBufferWrite(glId, offset, length)) {
|
||||
warnInvalidBufferOp("glFlushMappedBufferRange out of range", glId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void bufferStorage(int target, ByteBuffer data, int flags) {
|
||||
bufferData(target, data, GL15.GL_DYNAMIC_DRAW);
|
||||
recordImmutableStorage(target, flags);
|
||||
final ContextState cs = s();
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
if (glId == 0) return;
|
||||
if (refuseImmutable(glId, "glBufferStorage on immutable storage")) return;
|
||||
if (data != null) respecifyStore(cs, target, glId, data, GL15.GL_DYNAMIC_DRAW);
|
||||
recordImmutableStorage(glId, flags);
|
||||
}
|
||||
|
||||
@Override public void bufferStorage(int target, long size, int flags) {
|
||||
bufferData(target, size, GL15.GL_DYNAMIC_DRAW);
|
||||
recordImmutableStorage(target, flags);
|
||||
final ContextState cs = s();
|
||||
final int glId = cs.getBoundBuffer(target);
|
||||
if (glId == 0) return;
|
||||
if (refuseImmutable(glId, "glBufferStorage on immutable storage")) return;
|
||||
respecifyStore(cs, target, glId, size, GL15.GL_DYNAMIC_DRAW);
|
||||
recordImmutableStorage(glId, flags);
|
||||
}
|
||||
|
||||
private void recordImmutableStorage(int target, int flags) {
|
||||
final int glId = getBoundBuffer(target);
|
||||
if (glId != 0) resourceManager.recordBufferGlParams(glId, GL15.GL_DYNAMIC_DRAW, flags);
|
||||
private void recordImmutableStorage(int glId, int flags) {
|
||||
resourceManager.recordBufferGlParams(glId, GL15.GL_DYNAMIC_DRAW, flags);
|
||||
}
|
||||
@Override public void getBufferSubData(int target, long offset, ByteBuffer data) {
|
||||
final int glId = getBoundBuffer(target);
|
||||
@@ -3346,23 +3421,12 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
|
||||
private void downloadBufferBlocking(long handle, int offset, int size, ByteBuffer out) {
|
||||
flushDeferredUploadsForRead();
|
||||
frameManager.endCopyPassIfActive();
|
||||
frameManager.endRenderPassIfActive(FrameManager.PASS_END_COPY);
|
||||
flushAndSubmitMidFrame();
|
||||
final long cb = SDL_AcquireGPUCommandBuffer(device.getDevice());
|
||||
if (cb == 0) return;
|
||||
resourceManager.downloadFromBuffer(cb, handle, offset, size, out);
|
||||
}
|
||||
|
||||
private void flushDeferredUploadsForRead() {
|
||||
final ContextState st = s();
|
||||
drainDeferredPersistentRegions(st);
|
||||
resourceManager.flushUploadArena();
|
||||
if (transferThread != null && st.frameHighestEnqueuedSeq > transferThread.getSubmittedSeq()) {
|
||||
transferThread.awaitSubmittedUpTo(st.frameHighestEnqueuedSeq);
|
||||
}
|
||||
}
|
||||
|
||||
private static void serveFromShadow(ByteBuffer shadow, int srcOffset, ByteBuffer data, int rem) {
|
||||
final int dstPos = data.position();
|
||||
if (shadow.isDirect() && data.isDirect()) {
|
||||
@@ -3399,8 +3463,9 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
|
||||
final PersistentMapping pm = resourceManager.getPersistentMapping(glId);
|
||||
final ContextState st = s();
|
||||
final boolean plainMapped = st.mappedStagingBuffer != null && st.mappedBufferGlId == glId;
|
||||
final MappedRange plain = s().mappingScratch;
|
||||
final boolean plainMapped = resourceManager.peekPlainMapping(glId, plain);
|
||||
plain.staging = null;
|
||||
|
||||
final boolean mapped;
|
||||
final int accessFlags;
|
||||
@@ -3413,9 +3478,9 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
mapLength = pm.length;
|
||||
} else if (plainMapped) {
|
||||
mapped = true;
|
||||
accessFlags = st.mappedAccessFlags;
|
||||
mapOffset = st.mappedOffset;
|
||||
mapLength = st.mappedLength;
|
||||
accessFlags = plain.accessFlags;
|
||||
mapOffset = plain.offset;
|
||||
mapLength = plain.length;
|
||||
} else {
|
||||
mapped = false;
|
||||
accessFlags = 0;
|
||||
@@ -3853,14 +3918,18 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
@Override public int createBuffers() { return resourceManager.genBuffer(); }
|
||||
@Override public void namedBufferData(int buffer, long size, int usage) {
|
||||
if (buffer == 0 || size <= 0) return;
|
||||
if (refuseImmutable(buffer, "glNamedBufferData on immutable storage")) return;
|
||||
final int sdlUsage = FormatMap.mapBufferUsage(GL15.GL_ARRAY_BUFFER);
|
||||
freePlainMappingFor(buffer, true);
|
||||
resourceManager.deleteBuffer(buffer);
|
||||
resourceManager.createBuffer(buffer, sdlUsage, size);
|
||||
resourceManager.recordBufferGlParams(buffer, usage, BufferParams.MUTABLE_STORE);
|
||||
}
|
||||
@Override public void namedBufferData(int buffer, ByteBuffer data, int usage) {
|
||||
if (buffer == 0 || data == null) return;
|
||||
if (refuseImmutable(buffer, "glNamedBufferData on immutable storage")) return;
|
||||
final int sdlUsage = FormatMap.mapBufferUsage(GL15.GL_ARRAY_BUFFER);
|
||||
freePlainMappingFor(buffer, true);
|
||||
resourceManager.deleteBuffer(buffer);
|
||||
final long handle = resourceManager.createBuffer(buffer, sdlUsage, data.remaining());
|
||||
resourceManager.recordBufferGlParams(buffer, usage, BufferParams.MUTABLE_STORE);
|
||||
@@ -4109,36 +4178,53 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
}
|
||||
|
||||
private static final int CLEAR_CHUNK_BYTES = 64 * 1024;
|
||||
private static final int FILL_CHUNK_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
private static boolean isAllZero(ByteBuffer data, int elemBytes) {
|
||||
final int n = Math.min(elemBytes, data.remaining());
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (data.get(data.position() + i) != 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override public void clearBufferSubData(int target, int internalFormat, long offset, long size, int format, int type, ByteBuffer data) {
|
||||
final int glId = getBoundBuffer(target);
|
||||
if (glId == 0 || size <= 0) return;
|
||||
final long bufSize = resourceManager.getBufferSize(glId);
|
||||
if (offset < 0 || (bufSize > 0 && (offset > bufSize || size > bufSize - offset))) {
|
||||
warnInvalidBufferOp("glClearBufferSubData out of range", glId);
|
||||
return;
|
||||
}
|
||||
final long handle = resourceManager.getBufferHandle(glId);
|
||||
if (handle == 0 || frameManager.getCommandBuffer() == 0) return;
|
||||
|
||||
final int elemBytes = Math.max(1, PixelOps.glPixelSize(format, type));
|
||||
final int chunkBytes = (int) Math.min(size, CLEAR_CHUNK_BYTES);
|
||||
final boolean defer = s().deferUploads && target != GL43.GL_SHADER_STORAGE_BUFFER;
|
||||
// Undeferred clears stage one large pattern and upload it repeatedly
|
||||
final int chunkBytes = (int) Math.min(size, defer ? CLEAR_CHUNK_BYTES : FILL_CHUNK_BYTES);
|
||||
final int alignedChunk = (chunkBytes / elemBytes) * elemBytes;
|
||||
if (alignedChunk == 0) return;
|
||||
resourceManager.markBufferContentsDefined(glId);
|
||||
final ByteBuffer chunk = MemoryUtil.memAlloc(alignedChunk);
|
||||
final boolean zero = data == null || isAllZero(data, elemBytes);
|
||||
final ByteBuffer chunk = zero ? MemoryUtil.memCalloc(alignedChunk) : MemoryUtil.memAlloc(alignedChunk);
|
||||
try {
|
||||
for (int i = 0; i < alignedChunk; i++) {
|
||||
final byte v = (data != null && (i % elemBytes) < data.remaining())
|
||||
? data.get(data.position() + (i % elemBytes)) : 0;
|
||||
chunk.put(i, v);
|
||||
if (!zero) {
|
||||
for (int i = 0; i < alignedChunk; i++) {
|
||||
final byte v = (i % elemBytes) < data.remaining() ? data.get(data.position() + (i % elemBytes)) : 0;
|
||||
chunk.put(i, v);
|
||||
}
|
||||
}
|
||||
chunk.position(0).limit(alignedChunk);
|
||||
if (!defer) {
|
||||
resourceManager.fillBuffer(frameManager.ensureCopyPass(), chunk, handle, offset, size);
|
||||
return;
|
||||
}
|
||||
long remaining = size;
|
||||
long writeOffset = offset;
|
||||
final boolean defer = s().deferUploads && target != GL43.GL_SHADER_STORAGE_BUFFER;
|
||||
while (remaining > 0) {
|
||||
final int n = (int) Math.min(remaining, alignedChunk);
|
||||
chunk.position(0).limit(n);
|
||||
if (defer) {
|
||||
enqueuePreCopied(chunk, handle, writeOffset, false);
|
||||
} else {
|
||||
resourceManager.uploadToBuffer(frameManager.ensureCopyPass(), chunk, handle, writeOffset, false);
|
||||
}
|
||||
enqueuePreCopied(chunk, handle, writeOffset, false);
|
||||
remaining -= n;
|
||||
writeOffset += n;
|
||||
}
|
||||
@@ -4163,11 +4249,14 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
return;
|
||||
}
|
||||
|
||||
// Volume-sized staging, so it is only reached for 3D formats the compute clear cannot address.
|
||||
final long totalBytes = (long) mipW * mipH * mipD * bpp;
|
||||
if (totalBytes <= 0 || totalBytes > Integer.MAX_VALUE) return;
|
||||
final long cp = frameManager.ensureCopyPass();
|
||||
if (cp == 0) return;
|
||||
// A volume-sized staging buffer would allocate and upload each byte (1 GB for Euphoria Patches' floodfill!!)
|
||||
if (mipD > 1 && resourceManager.zeroTexture3D(cp, texHandle, mipW, mipH, mipD, level, bpp)) {
|
||||
return;
|
||||
}
|
||||
final long totalBytes = (long) mipW * mipH * mipD * bpp;
|
||||
if (totalBytes <= 0 || totalBytes > Integer.MAX_VALUE) return;
|
||||
final ByteBuffer zeros = MemoryUtil.memCalloc((int) totalBytes);
|
||||
try {
|
||||
if (mipD > 1) {
|
||||
@@ -4201,6 +4290,10 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
@Override public void memoryBarrier(int barriers) {
|
||||
// Graphics storage bindings are read-only on SDL_GPU; RW is compute-pass only
|
||||
frameManager.endCopyPassIfActive();
|
||||
// Replayed vertex image writes land when the render pass ends, and a barrier promises them to the draws after it
|
||||
if ((barriers & (GL42.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL42.GL_TEXTURE_FETCH_BARRIER_BIT)) != 0 && vertexWriteReplayer.hasPending()) {
|
||||
frameManager.endRenderPassIfActive(FrameManager.PASS_END_COMPUTE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void copyImageSubData(int srcName, int srcTarget, int srcLevel, int srcX, int srcY, int srcZ,
|
||||
@@ -4347,18 +4440,45 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
voxelizationDispatcher.endBatch(pass);
|
||||
}
|
||||
|
||||
@Override public void setVertexWriteReplay(int graphicsProgram, VertexWriteReplaySetup setup) {
|
||||
vertexWriteReplayer.setReplay(graphicsProgram, setup);
|
||||
}
|
||||
|
||||
@Override public boolean supportsComputeImageAtomics() {
|
||||
return device.supportsSpirv() || !device.supportsMsl();
|
||||
}
|
||||
|
||||
@Override public boolean supportsComputeImageAtomics(int width, int height, int depth) {
|
||||
return supportsComputeImageAtomics() || device.supportsImageAtomics(width, height, depth);
|
||||
}
|
||||
|
||||
private void prepareVertexWrites(ContextState st) {
|
||||
final ShaderManager.ProgramObject prog = st.boundProgramObj;
|
||||
if (prog != null && prog.vertexWriteReplay != null) vertexWriteReplayer.ensureImageUsage(prog.vertexWriteReplay);
|
||||
}
|
||||
|
||||
private void captureVertexWrites(ContextState st, int first, int count, int instances, int indexType, long indexOffset, int baseVertex) {
|
||||
final ShaderManager.ProgramObject prog = st.boundProgramObj;
|
||||
if (prog != null && prog.vertexWriteReplay != null) {
|
||||
vertexWriteReplayer.capture(st, first, count, instances, indexType, indexOffset, baseVertex);
|
||||
if (!frameManager.isRenderPassActive()) vertexWriteReplayer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawArraysInstanced(int mode, int first, int count, int primcount) {
|
||||
if (count <= 0 || primcount <= 0) return;
|
||||
final ContextState st = s();
|
||||
if (st.rasterizerDiscard) return;
|
||||
if (!frameManager.isFrameActive()) return;
|
||||
prepareVertexWrites(st);
|
||||
drawDispatch.setPrimitiveTypeForDraw(st, FormatMap.mapPrimitiveType(mode));
|
||||
pipelineApplier.ensureDrawRenderPass(st);
|
||||
if (!frameManager.isRenderPassActive()) return;
|
||||
if (!pipelineApplier.applyPipelineAndState(st)) return;
|
||||
if (SystemProperties.FFP_TRACE) ffpTrace.trace(st, "arraysInstanced", mode, count, 0, first, 0);
|
||||
SDL_DrawGPUPrimitives(frameManager.getRenderPass(), count, primcount, first, 0);
|
||||
captureVertexWrites(st, first, count, primcount, 0, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -4469,8 +4589,8 @@ public class SDLGPURenderBackend extends RenderBackend {
|
||||
long readOffset, size, dstHandle, writeOffset, seq;
|
||||
@Override
|
||||
public void accept(PersistentMapping src, PersistentMapping dst) {
|
||||
enqueueUpload(TransferThread.StagingReadUpload.acquire(src.staging, readOffset, size, dstHandle, writeOffset, seq, false));
|
||||
if (dst != null) PersistentBufferSync.mirrorPersistentCopy(src.staging, readOffset, dst.staging, writeOffset, size);
|
||||
enqueueUpload(TransferThread.StagingReadUpload.acquire(src.staging, src.stagingIndex(readOffset), size, dstHandle, writeOffset, seq, false));
|
||||
if (dst != null) PersistentBufferSync.mirrorPersistentCopy(src, readOffset, dst, writeOffset, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
+94
-19
@@ -45,6 +45,27 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
private final ShaderManager shaderManager;
|
||||
private final SamplerBinder samplerBinder;
|
||||
private boolean droppedDispatchWarned;
|
||||
private ShaderManager.ProgramObject lastUniformProgram;
|
||||
private long lastUniformCb;
|
||||
private long[] samplerOverrides;
|
||||
private long[] rwBufferOverrides;
|
||||
private ByteBuffer[] uniformBlockOverrides;
|
||||
|
||||
public void setSamplerOverrides(long[] byUnit) {
|
||||
samplerOverrides = byUnit;
|
||||
}
|
||||
|
||||
public void setReadWriteStorageBufferOverrides(long[] byGlSlot) {
|
||||
rwBufferOverrides = byGlSlot;
|
||||
}
|
||||
|
||||
public void setUniformBlockOverrides(ByteBuffer[] byIndex) {
|
||||
uniformBlockOverrides = byIndex;
|
||||
}
|
||||
|
||||
public long samplerHandle(int samplerObject, int glTexId) {
|
||||
return samplerBinder.getSampler(samplerObject, glTexId);
|
||||
}
|
||||
|
||||
public ComputeBinder(FrameManager frameManager, ResourceManager resourceManager, ShaderManager shaderManager, SamplerBinder samplerBinder) {
|
||||
this.frameManager = frameManager;
|
||||
@@ -153,6 +174,43 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
|
||||
@Override
|
||||
public long beginBatchedComputeDispatch(ContextState st, boolean cycleRwBuffers) {
|
||||
return beginComputePass(st, cycleRwBuffers, true);
|
||||
}
|
||||
|
||||
public long beginComputePassWithoutStorageBuffers(ContextState st) {
|
||||
return beginComputePass(st, false, false);
|
||||
}
|
||||
|
||||
public boolean bindRoStorageBufferHandles(long pass, ContextState st, long[] handlesByGlSlot) {
|
||||
if (pass == 0) return false;
|
||||
final ShaderManager.ProgramObject prog = shaderManager.getProgram(st.boundProgram);
|
||||
if (prog == null || !prog.linked) return false;
|
||||
final int[] slots = prog.computeBindingMap.roSsboGlSlots();
|
||||
if (slots.length == 0) return true;
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
final PointerBuffer ptrs = stack.mallocPointer(slots.length);
|
||||
for (int i = 0; i < slots.length; i++) {
|
||||
long handle = slots[i] < handlesByGlSlot.length ? handlesByGlSlot[slots[i]] : 0;
|
||||
if (handle == 0) handle = resourceManager.getOrCreateFallbackStorageBuffer();
|
||||
if (handle == 0) return false;
|
||||
ptrs.put(i, handle);
|
||||
}
|
||||
SDL_BindGPUComputeStorageBuffers(pass, 0, ptrs);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean rebindSamplers(long pass, ContextState st) {
|
||||
if (pass == 0) return false;
|
||||
final ShaderManager.ProgramObject prog = shaderManager.getProgram(st.boundProgram);
|
||||
if (prog == null || !prog.linked) return false;
|
||||
final ShaderManager.ComputeBindingMap map = prog.computeBindingMap;
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
return bindComputeSamplers(st, prog, pass, map.samplerGlSlots(), map.samplerNames(), stack);
|
||||
}
|
||||
}
|
||||
|
||||
private long beginComputePass(ContextState st, boolean cycleRwBuffers, boolean bindRoBuffers) {
|
||||
final int programId = st.boundProgram;
|
||||
if (programId == 0) return 0;
|
||||
final ShaderManager.ProgramObject prog = shaderManager.getProgram(programId);
|
||||
@@ -196,7 +254,7 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
|
||||
final boolean ok = bindComputeSamplers(st, prog, pass, samplerSlots, map.samplerNames(), stack)
|
||||
&& bindRoStorageTextures(st, prog, pass, map, stack)
|
||||
&& bindRoStorageBuffers(st, pass, roBufSlots, stack);
|
||||
&& (!bindRoBuffers || bindRoStorageBuffers(st, pass, roBufSlots, stack));
|
||||
if (!ok) {
|
||||
SDL_EndGPUComputePass(pass);
|
||||
frameManager.noteComputePassEnded();
|
||||
@@ -298,7 +356,7 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
private SDL_GPUStorageBufferReadWriteBinding.Buffer buildRwBufferBindings(ContextState st, int[] slots, MemoryStack stack, boolean cycle) {
|
||||
final SDL_GPUStorageBufferReadWriteBinding.Buffer buf = SDL_GPUStorageBufferReadWriteBinding.calloc(slots.length, stack);
|
||||
for (int i = 0; i < slots.length; i++) {
|
||||
final long handle = resolveStorageBufHandle(st, slots[i]);
|
||||
final long handle = rwBufferOverrides != null && slots[i] < rwBufferOverrides.length ? rwBufferOverrides[slots[i]] : resolveStorageBufHandle(st, slots[i]);
|
||||
|
||||
if (handle == 0) { warnMissingBinding(st, 1, "RW storage buffer slot", slots[i], true); return null; }
|
||||
buf.get(i).buffer(handle).cycle(cycle);
|
||||
@@ -319,7 +377,8 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
if (texHandle == 0) { warnMissingBinding(st, 2, "sampler texture unit", unit, true); return false; }
|
||||
warnMissingBinding(st, 2, "sampler texture unit", unit, false);
|
||||
}
|
||||
long sampHandle = samplerBinder.getSamplerForUnit(st, unit, texGlId);
|
||||
final long override = samplerOverrides != null && unit >= 0 && unit < samplerOverrides.length ? samplerOverrides[unit] : 0;
|
||||
long sampHandle = override != 0 ? override : samplerBinder.getSamplerForUnit(st, unit, texGlId);
|
||||
if (sampHandle == 0) {
|
||||
sampHandle = resourceManager.getOrCreateDefaultSampler();
|
||||
if (sampHandle == 0) { warnMissingBinding(st, 3, "sampler unit", unit, true); return false; }
|
||||
@@ -374,6 +433,28 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
return true;
|
||||
}
|
||||
|
||||
public ByteBuffer uniformBlockView(ContextState st, ShaderManager.ProgramObject prog, int i) {
|
||||
final ShaderManager.ComputeBindingMap map = prog.computeBindingMap;
|
||||
final int[] uboSlots = map.uboGlSlots();
|
||||
final int[] declaredSizes = map.uboSizes();
|
||||
if (i >= uboSlots.length) return null;
|
||||
final int slot = uboSlots[i];
|
||||
final int bufGlId = st.boundUboByIndex[slot];
|
||||
if (bufGlId == 0) return null;
|
||||
final ByteBuffer shadow = resourceManager.getUboShadow(bufGlId);
|
||||
if (shadow == null) return null;
|
||||
final int offset = st.uboRangeOffset[slot];
|
||||
if (offset < 0 || offset >= shadow.capacity()) return null;
|
||||
int size = (i < declaredSizes.length && declaredSizes[i] > 0) ? declaredSizes[i] : shadow.capacity() - offset;
|
||||
final int rangeSize = st.uboRangeSize[slot];
|
||||
if (rangeSize > 0) size = Math.min(size, rangeSize);
|
||||
size = Math.min(size, shadow.capacity() - offset);
|
||||
if (size <= 0) return null;
|
||||
final ByteBuffer view = frameManager.getUboPushView(bufGlId, shadow);
|
||||
view.position(offset).limit(offset + size);
|
||||
return view;
|
||||
}
|
||||
|
||||
private void pushComputeUboData(ContextState st, long cb, ShaderManager.ProgramObject prog, int[] uboSlots) {
|
||||
if (prog.lastComputeUboHash == null || prog.lastComputeUboHash.length != uboSlots.length) {
|
||||
prog.lastComputeUboHash = new long[uboSlots.length];
|
||||
@@ -381,16 +462,18 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
prog.lastComputeFrame = 0;
|
||||
}
|
||||
final long curFrame = frameManager.getFrameNumber();
|
||||
final boolean cacheValid = cb == prog.lastComputeCb && curFrame == prog.lastComputeFrame;
|
||||
final boolean cacheValid = cb == prog.lastComputeCb && curFrame == prog.lastComputeFrame
|
||||
&& lastUniformProgram == prog && lastUniformCb == cb;
|
||||
final long[] cache = prog.lastComputeUboHash;
|
||||
if (!cacheValid) {
|
||||
for (int i = 0; i < cache.length; i++) cache[i] = 0L;
|
||||
prog.lastComputeCb = cb;
|
||||
prog.lastComputeFrame = curFrame;
|
||||
}
|
||||
lastUniformProgram = prog;
|
||||
lastUniformCb = cb;
|
||||
final ShaderManager.ComputeBindingMap map = prog.computeBindingMap;
|
||||
final boolean[] isDefault = map.uboIsDefaultBlock();
|
||||
final int[] declaredSizes = map.uboSizes();
|
||||
final UniformStaging us = st.uniformStaging(prog);
|
||||
for (int i = 0; i < uboSlots.length; i++) {
|
||||
final ByteBuffer view;
|
||||
@@ -398,21 +481,13 @@ public final class ComputeBinder implements ComputeDispatchSink {
|
||||
if (us.fsUniformBuf == null || prog.fragmentUboSize <= 0) continue;
|
||||
view = us.fsUniformBuf;
|
||||
view.position(0).limit(prog.fragmentUboSize);
|
||||
} else if (uniformBlockOverrides != null && i < uniformBlockOverrides.length && uniformBlockOverrides[i] != null) {
|
||||
view = uniformBlockOverrides[i];
|
||||
view.position(0);
|
||||
if (!view.hasRemaining()) continue;
|
||||
} else {
|
||||
final int slot = uboSlots[i];
|
||||
final int bufGlId = st.boundUboByIndex[slot];
|
||||
if (bufGlId == 0) continue;
|
||||
final ByteBuffer shadow = resourceManager.getUboShadow(bufGlId);
|
||||
if (shadow == null) continue;
|
||||
final int offset = st.uboRangeOffset[slot];
|
||||
if (offset < 0 || offset >= shadow.capacity()) continue;
|
||||
int size = (i < declaredSizes.length && declaredSizes[i] > 0) ? declaredSizes[i] : shadow.capacity() - offset;
|
||||
final int rangeSize = st.uboRangeSize[slot];
|
||||
if (rangeSize > 0) size = Math.min(size, rangeSize);
|
||||
size = Math.min(size, shadow.capacity() - offset);
|
||||
if (size <= 0) continue;
|
||||
view = frameManager.getUboPushView(bufGlId, shadow);
|
||||
view.position(offset).limit(offset + size);
|
||||
view = uniformBlockView(st, prog, i);
|
||||
if (view == null) continue;
|
||||
}
|
||||
final int pos = view.position();
|
||||
final int len = view.remaining();
|
||||
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.compute;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.shader.SpirvCompiler;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.ShaderManager;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.cross.CrossCompileCache;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.msl.MslCrossCompile;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.embeddedt.embeddium.impl.render.shader.ShaderLoader;
|
||||
import org.lwjgl.opengl.GL43;
|
||||
import org.lwjgl.sdl.SDLError;
|
||||
import org.lwjgl.sdl.SDL_GPUComputePipelineCreateInfo;
|
||||
import org.lwjgl.sdl.SDL_GPUStorageTextureReadWriteBinding;
|
||||
import org.lwjgl.sdl.SDL_GPUTextureCreateInfo;
|
||||
import org.lwjgl.sdl.SDL_GPUTextureRegion;
|
||||
import org.lwjgl.sdl.SDL_GPUTextureTransferInfo;
|
||||
import org.lwjgl.sdl.SDL_GPUTransferBufferCreateInfo;
|
||||
import org.lwjgl.system.JNI;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
import org.lwjgl.system.macosx.ObjCRuntime;
|
||||
import org.lwjgl.util.shaderc.Shaderc;
|
||||
|
||||
import static org.lwjgl.sdl.SDLGPU.*;
|
||||
|
||||
public final class ImageAtomicsProbe {
|
||||
private static final Logger LOG = LogManager.getLogger("Angelica-SDLGPU");
|
||||
|
||||
private static final String SOURCE = "angelica:sdlgpu/atomic_probe.csh";
|
||||
public static final int MTL_LANGUAGE_VERSION_3_1 = (3 << 16) + 1;
|
||||
private static final int GROUPS = 64;
|
||||
|
||||
private final long device;
|
||||
private long pipeline;
|
||||
|
||||
public ImageAtomicsProbe(long device) {
|
||||
this.device = device;
|
||||
final SpirvCompiler.Result compiled = SpirvCompiler.compile(ShaderLoader.getShaderSource(SOURCE), Shaderc.shaderc_compute_shader, "atomic_probe", SpirvCompiler.Options.vulkanForced460Core(), false);
|
||||
if (compiled.spirv() == null) {
|
||||
LOG.warn("Metal image atomics: probe pipeline failed: {}", compiled.error());
|
||||
return;
|
||||
}
|
||||
CrossCompileCache.Output msl = null;
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(compiled.spirv());
|
||||
msl = MslCrossCompile.compileAtVersion(compiled.spirv(), GL43.GL_COMPUTE_SHADER, MslCrossCompile.SPVC_MSL_3_1);
|
||||
pipeline = SDL_CreateGPUComputePipeline(device, SDL_GPUComputePipelineCreateInfo.calloc(stack)
|
||||
.code(msl.code())
|
||||
.entrypoint(stack.ASCII(msl.entrypoint()))
|
||||
.format(SDL_GPU_SHADERFORMAT_MSL)
|
||||
.num_readwrite_storage_textures(1)
|
||||
.threadcount_x(InvocationDispatch.WORKGROUP_SIZE).threadcount_y(1).threadcount_z(1));
|
||||
if (pipeline == 0) LOG.warn("Metal image atomics: probe pipeline failed: {}", SDLError.SDL_GetError());
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warn("Metal image atomics: probe pipeline failed", e);
|
||||
} finally {
|
||||
if (msl != null) MemoryUtil.memFree(msl.code());
|
||||
MemoryUtil.memFree(compiled.spirv());
|
||||
}
|
||||
}
|
||||
|
||||
public static int defaultMslVersion() {
|
||||
try {
|
||||
final long msgSend = ObjCRuntime.getLibrary().getFunctionAddress("objc_msgSend");
|
||||
final long cls = ObjCRuntime.objc_getClass("MTLCompileOptions");
|
||||
if (msgSend == 0 || cls == 0) return 0;
|
||||
final long options = JNI.invokePPP(cls, ObjCRuntime.sel_getUid("new"), msgSend);
|
||||
if (options == 0) return 0;
|
||||
final long version = JNI.invokePPP(options, ObjCRuntime.sel_getUid("languageVersion"), msgSend);
|
||||
JNI.invokePPV(options, ObjCRuntime.sel_getUid("release"), msgSend);
|
||||
return (int) version;
|
||||
} catch (RuntimeException | LinkageError e) {
|
||||
LOG.warn("Could not read the default Metal language version", e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean run(int width, int height) {
|
||||
if (pipeline == 0) return false;
|
||||
long texture = 0, upload = 0, download = 0, cb = 0;
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
texture = SDL_CreateGPUTexture(device, SDL_GPUTextureCreateInfo.calloc(stack)
|
||||
.type(SDL_GPU_TEXTURETYPE_2D)
|
||||
.format(SDL_GPU_TEXTUREFORMAT_R32_INT)
|
||||
.usage(SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE)
|
||||
.width(width).height(height).layer_count_or_depth(1).num_levels(1)
|
||||
.sample_count(SDL_GPU_SAMPLECOUNT_1));
|
||||
if (texture == 0) return failSdl(width, height, "texture");
|
||||
|
||||
upload = SDL_CreateGPUTransferBuffer(device, SDL_GPUTransferBufferCreateInfo.calloc(stack).usage(SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD).size(Integer.BYTES));
|
||||
download = SDL_CreateGPUTransferBuffer(device, SDL_GPUTransferBufferCreateInfo.calloc(stack).usage(SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD).size(Integer.BYTES));
|
||||
if (upload == 0 || download == 0) return failSdl(width, height, "transfer buffer");
|
||||
final long zero = nSDL_MapGPUTransferBuffer(device, upload, false);
|
||||
if (zero == 0) return failSdl(width, height, "map");
|
||||
MemoryUtil.memPutInt(zero, 0);
|
||||
SDL_UnmapGPUTransferBuffer(device, upload);
|
||||
|
||||
cb = SDL_AcquireGPUCommandBuffer(device);
|
||||
if (cb == 0) return failSdl(width, height, "command buffer");
|
||||
final SDL_GPUTextureRegion region = SDL_GPUTextureRegion.calloc(stack).texture(texture).w(1).h(1).d(1);
|
||||
long copy = SDL_BeginGPUCopyPass(cb);
|
||||
SDL_UploadToGPUTexture(copy, SDL_GPUTextureTransferInfo.calloc(stack).transfer_buffer(upload), region, false);
|
||||
SDL_EndGPUCopyPass(copy);
|
||||
|
||||
final SDL_GPUStorageTextureReadWriteBinding.Buffer binding = SDL_GPUStorageTextureReadWriteBinding.calloc(1, stack);
|
||||
binding.get(0).texture(texture);
|
||||
final long pass = SDL_BeginGPUComputePass(cb, binding, null);
|
||||
if (pass == 0) return failSdl(width, height, "compute pass");
|
||||
SDL_BindGPUComputePipeline(pass, pipeline);
|
||||
SDL_DispatchGPUCompute(pass, GROUPS, 1, 1);
|
||||
SDL_EndGPUComputePass(pass);
|
||||
|
||||
copy = SDL_BeginGPUCopyPass(cb);
|
||||
SDL_DownloadFromGPUTexture(copy, region, SDL_GPUTextureTransferInfo.calloc(stack).transfer_buffer(download));
|
||||
SDL_EndGPUCopyPass(copy);
|
||||
final boolean submitted = SDL_SubmitGPUCommandBuffer(cb);
|
||||
cb = 0;
|
||||
if (!submitted) return failSdl(width, height, "submit");
|
||||
SDL_WaitForGPUIdle(device);
|
||||
|
||||
final long result = nSDL_MapGPUTransferBuffer(device, download, false);
|
||||
if (result == 0) return failSdl(width, height, "map");
|
||||
final int adds = MemoryUtil.memGetInt(result);
|
||||
SDL_UnmapGPUTransferBuffer(device, download);
|
||||
|
||||
final int invocations = GROUPS * InvocationDispatch.WORKGROUP_SIZE;
|
||||
if (adds != invocations) LOG.info("Metal image atomics on {}x{}: {} invocations added {}", width, height, invocations, adds);
|
||||
return adds == invocations;
|
||||
} catch (RuntimeException e) {
|
||||
return fail(width, height, e.toString());
|
||||
} finally {
|
||||
if (cb != 0) SDL_CancelGPUCommandBuffer(cb);
|
||||
if (download != 0) SDL_ReleaseGPUTransferBuffer(device, download);
|
||||
if (upload != 0) SDL_ReleaseGPUTransferBuffer(device, upload);
|
||||
if (texture != 0) SDL_ReleaseGPUTexture(device, texture);
|
||||
}
|
||||
}
|
||||
|
||||
public void release() {
|
||||
if (pipeline != 0) SDL_ReleaseGPUComputePipeline(device, pipeline);
|
||||
pipeline = 0;
|
||||
}
|
||||
|
||||
private static boolean failSdl(int width, int height, String what) {
|
||||
return fail(width, height, what + " failed: " + SDLError.SDL_GetError());
|
||||
}
|
||||
|
||||
private static boolean fail(int width, int height, String reason) {
|
||||
LOG.warn("Metal image atomics on {}x{}: {}", width, height, reason);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.compute;
|
||||
|
||||
import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState;
|
||||
|
||||
public final class InvocationDispatch {
|
||||
static final int MAX_GROUPS_PER_DISPATCH = 65535;
|
||||
static final int WORKGROUP_SIZE = 64;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface IntUniformWriter {
|
||||
void write(ContextState st, int location, int value);
|
||||
}
|
||||
|
||||
private InvocationDispatch() {}
|
||||
|
||||
static void dispatch(ComputeDispatchSink sink, ContextState st, long pass, long invocations, int invocationBaseLocation, IntUniformWriter writer) {
|
||||
final long groups = (invocations + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE;
|
||||
for (long g0 = 0; g0 < groups; g0 += MAX_GROUPS_PER_DISPATCH) {
|
||||
if (invocationBaseLocation >= 0) writer.write(st, invocationBaseLocation, (int) (g0 * WORKGROUP_SIZE));
|
||||
sink.pushPendingComputeUniforms(st);
|
||||
sink.dispatchInBatch(pass, (int) Math.min(MAX_GROUPS_PER_DISPATCH, groups - g0), 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
+635
@@ -0,0 +1,635 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.compute;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VertexWriteReplaySetup;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.SDLGPURenderBackend;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.pipeline.PipelineApplier;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.ResourceManager;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.ShaderManager;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.UniformStaging;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.opengl.GL33;
|
||||
import org.lwjgl.opengl.GL41;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Performs the image writes of a vertex shader that SDL-GPU strips (graphics stages may only read storage images).
|
||||
* Every draw made with a program that has a {@link Replay} is captured: its vertex and index buffers, attribute layout,
|
||||
* draw range, the uniform values the replay reads and the textures it samples. When the render pass ends, the replay
|
||||
* compute shader re-runs the vertex shader for each captured vertex and performs the writes.
|
||||
*/
|
||||
public final class VertexWriteReplayer {
|
||||
private static final Logger LOG = LogManager.getLogger("Angelica-SDLGPU");
|
||||
|
||||
private static final int MAX_CAPTURES = 1 << 16;
|
||||
private static final int[] COMPONENT_SIZE = { 4, 2, 1, 1, 2, 2, 4, 4, 4, 4, 4, 4 };
|
||||
private static final int FIRST_PACKED_TYPE = 9;
|
||||
private static final int PACKED_10F_11F_11F = 11;
|
||||
|
||||
private static final int ENABLED = 0x2000;
|
||||
private static final int BGRA = 0x4000;
|
||||
private static final int NO_ELEMENT = 0x8000;
|
||||
private static final int MAX_DIVISOR = 0xFFFF;
|
||||
private static final int UNCHANGED = -1;
|
||||
private static final int TEXTURE_UNITS = 32;
|
||||
private static final int SAMPLER_OBJECT_OF_DRAW = -1;
|
||||
|
||||
public static final class Replay {
|
||||
final int replayProgram;
|
||||
final VertexWriteReplaySetup setup;
|
||||
|
||||
private boolean resolved;
|
||||
private boolean broken;
|
||||
int[] inputLocations;
|
||||
int[] uniformSources;
|
||||
int[] uniformTargets;
|
||||
int[] samplerUnits;
|
||||
IntSupplier[] samplerSources;
|
||||
int[] samplerObjects;
|
||||
int[] packStorageSlots;
|
||||
int[] packRwStorageSlots;
|
||||
int[] uniformBlockIndices;
|
||||
int uniformBlockCount;
|
||||
int locAttr, locAttrDefault, locDraw, locIndex, locRestart, locInvocationBase;
|
||||
long[] lastHashes;
|
||||
boolean lastHashesApplied;
|
||||
|
||||
public Replay(VertexWriteReplaySetup setup) {
|
||||
this.replayProgram = setup.replayProgram();
|
||||
this.setup = setup;
|
||||
}
|
||||
|
||||
IntSupplier[] images() {
|
||||
return setup.images();
|
||||
}
|
||||
|
||||
boolean resolve(ShaderManager.ProgramObject graphics, ShaderManager.ProgramObject replay) {
|
||||
if (resolved) return !broken;
|
||||
resolved = true;
|
||||
|
||||
final String[] inputNames = setup.inputNames();
|
||||
inputLocations = new int[inputNames.length];
|
||||
for (int i = 0; i < inputNames.length; i++) {
|
||||
int loc = graphics.resolvedAttribLocations.getInt(inputNames[i]);
|
||||
if (loc < 0) loc = graphics.attribLocationBindings.getInt(inputNames[i]);
|
||||
if (loc < 0) loc = setup.inputLocations()[i];
|
||||
inputLocations[i] = loc;
|
||||
}
|
||||
|
||||
final IntArrayList sources = new IntArrayList();
|
||||
final IntArrayList targets = new IntArrayList();
|
||||
final List<String> samplerNames = new ArrayList<>();
|
||||
for (int target = 0; target < replay.uniformSlotCount; target++) {
|
||||
final String name = replay.locationName[target];
|
||||
if (name == null || name.startsWith("_vg_")) continue;
|
||||
if (replay.locationKind[target] == ShaderManager.ProgramObject.LOCATION_KIND_SAMPLER) {
|
||||
samplerNames.add(name);
|
||||
continue;
|
||||
}
|
||||
if (replay.locationKind[target] != ShaderManager.ProgramObject.LOCATION_KIND_PLAIN) continue;
|
||||
final int source = graphics.nameToLocation.getInt(name);
|
||||
if (source < 0) continue;
|
||||
sources.add(source);
|
||||
targets.add(target);
|
||||
}
|
||||
uniformSources = sources.toIntArray();
|
||||
uniformTargets = targets.toIntArray();
|
||||
lastHashes = new long[uniformSources.length];
|
||||
resolveSamplers(graphics, replay, samplerNames);
|
||||
|
||||
final IntArrayList storage = new IntArrayList();
|
||||
for (int slot : replay.computeBindingMap.roSsboGlSlots()) {
|
||||
final boolean vertexBuffer = slot >= setup.vertexBufferBinding() && slot < setup.vertexBufferBinding() + setup.vertexBufferCount();
|
||||
if (!vertexBuffer && slot != setup.indexBufferBinding()) storage.add(slot);
|
||||
}
|
||||
packStorageSlots = storage.toIntArray();
|
||||
packRwStorageSlots = replay.computeBindingMap.rwSsboGlSlots().clone();
|
||||
final IntArrayList blocks = new IntArrayList();
|
||||
final boolean[] isDefault = replay.computeBindingMap.uboIsDefaultBlock();
|
||||
for (int i = 0; i < replay.computeBindingMap.uboGlSlots().length; i++) {
|
||||
if (i >= isDefault.length || !isDefault[i]) blocks.add(i);
|
||||
}
|
||||
uniformBlockIndices = blocks.toIntArray();
|
||||
uniformBlockCount = replay.computeBindingMap.uboGlSlots().length;
|
||||
|
||||
locAttr = replay.nameToLocation.getInt("_vg_attr");
|
||||
locAttrDefault = replay.nameToLocation.getInt("_vg_attrDefault");
|
||||
locDraw = replay.nameToLocation.getInt("_vg_draw");
|
||||
locIndex = replay.nameToLocation.getInt("_vg_index");
|
||||
locRestart = replay.nameToLocation.getInt("_vg_restart");
|
||||
locInvocationBase = replay.nameToLocation.getInt("_vg_invocationBase");
|
||||
if (locDraw < 0 || locInvocationBase < 0) {
|
||||
broken = true;
|
||||
LOG.warn("Vertex write replay program {} is missing its dispatch uniforms; its writes are dropped", replayProgram);
|
||||
}
|
||||
return !broken;
|
||||
}
|
||||
|
||||
private void resolveSamplers(ShaderManager.ProgramObject graphics, ShaderManager.ProgramObject replay, List<String> names) {
|
||||
final IntArrayList units = new IntArrayList();
|
||||
final List<IntSupplier> sources = new ArrayList<>();
|
||||
final IntArrayList objects = new IntArrayList();
|
||||
final IntArrayList ownTextureIndices = new IntArrayList();
|
||||
final List<String> ownTextureNames = new ArrayList<>();
|
||||
final List<String> pipelineNames = Arrays.asList(setup.samplerNames());
|
||||
for (String name : names) {
|
||||
int unit = graphics.samplerTextureUnits.getInt(name);
|
||||
if (unit < 0) {
|
||||
final int i = pipelineNames.indexOf(name);
|
||||
if (i < 0) continue;
|
||||
if (setup.samplerTextures()[i] != null && setup.samplerUnits()[i] < 0) {
|
||||
ownTextureNames.add(name);
|
||||
ownTextureIndices.add(i);
|
||||
continue;
|
||||
}
|
||||
unit = setup.samplerUnits()[i];
|
||||
if (unit < 0) continue;
|
||||
}
|
||||
replay.samplerTextureUnits.put(name, unit);
|
||||
if (!units.contains(unit)) {
|
||||
units.add(unit);
|
||||
sources.add(null);
|
||||
objects.add(SAMPLER_OBJECT_OF_DRAW);
|
||||
}
|
||||
}
|
||||
int free = TEXTURE_UNITS - 1;
|
||||
for (int k = 0; k < ownTextureNames.size(); k++) {
|
||||
while (free >= 0 && units.contains(free)) free--;
|
||||
if (free < 0) break;
|
||||
final int i = ownTextureIndices.getInt(k);
|
||||
replay.samplerTextureUnits.put(ownTextureNames.get(k), free);
|
||||
units.add(free);
|
||||
sources.add(setup.samplerTextures()[i]);
|
||||
objects.add(setup.samplerObjects()[i]);
|
||||
}
|
||||
samplerUnits = units.toIntArray();
|
||||
samplerSources = sources.toArray(new IntSupplier[0]);
|
||||
samplerObjects = objects.toIntArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Capture {
|
||||
Replay replay;
|
||||
long[] buffers = new long[0];
|
||||
int bufferCount;
|
||||
long indexBuffer;
|
||||
float[] attr = new float[0];
|
||||
float[] attrDefault = new float[0];
|
||||
int[] samplerTextures = new int[0];
|
||||
long[] samplerHandles = new long[0];
|
||||
int[] imageTextures = new int[0];
|
||||
long[] packStorage = new long[0];
|
||||
long[] packRwStorage = new long[0];
|
||||
ByteBuffer[] uniformBlocks = new ByteBuffer[0];
|
||||
int first, count, instances, baseVertex, indexSize, indexOffset, indexCount;
|
||||
boolean restart;
|
||||
int restartIndex;
|
||||
}
|
||||
|
||||
private final ShaderManager shaderManager;
|
||||
private final ResourceManager resourceManager;
|
||||
private final PipelineApplier pipelineApplier;
|
||||
private final ComputeBinder computeBinder;
|
||||
private final Supplier<ContextState> state;
|
||||
|
||||
private final ArrayList<Capture> captures = new ArrayList<>();
|
||||
private int pending;
|
||||
private float[] uniformValues = new float[4096];
|
||||
private int[] uniformLengths = new int[256];
|
||||
private int uniformValueCount;
|
||||
private int uniformCount;
|
||||
private boolean flushing;
|
||||
private final long[] handlesByGlSlot = new long[ContextState.MAX_INDEXED_BUFFERS];
|
||||
private final long[] rwHandlesByGlSlot = new long[ContextState.MAX_INDEXED_BUFFERS];
|
||||
private final long[] samplerOverrides = new long[TEXTURE_UNITS];
|
||||
private final int[] savedImages = new int[ContextState.MAX_IMAGE_UNITS];
|
||||
private final int[] savedLevels = new int[ContextState.MAX_IMAGE_UNITS];
|
||||
private final int[] savedAccess = new int[ContextState.MAX_IMAGE_UNITS];
|
||||
private final int[] savedTextures = new int[TEXTURE_UNITS];
|
||||
private int uniformCursor;
|
||||
private int valueCursor;
|
||||
private final InvocationDispatch.IntUniformWriter invocationBaseWriter = (st, location, value) -> putIntVector(st, location, 1, value, 0, 0, 0);
|
||||
private final Set<String> warned = new HashSet<>();
|
||||
|
||||
public VertexWriteReplayer(ShaderManager shaderManager, ResourceManager resourceManager, PipelineApplier pipelineApplier, ComputeBinder computeBinder, Supplier<ContextState> state) {
|
||||
this.shaderManager = shaderManager;
|
||||
this.resourceManager = resourceManager;
|
||||
this.pipelineApplier = pipelineApplier;
|
||||
this.computeBinder = computeBinder;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public void setReplay(int graphicsProgram, VertexWriteReplaySetup setup) {
|
||||
final ShaderManager.ProgramObject prog = shaderManager.getProgram(graphicsProgram);
|
||||
if (prog == null) return;
|
||||
if (setup == null) {
|
||||
prog.vertexWriteReplay = null;
|
||||
return;
|
||||
}
|
||||
if (setup.images().length > ContextState.MAX_IMAGE_UNITS) {
|
||||
warnOnce("images:" + setup.replayProgram(), "Vertex write replay program " + setup.replayProgram() + " uses " + setup.images().length + " images, more than the " + ContextState.MAX_IMAGE_UNITS + " image units; its writes are dropped");
|
||||
prog.vertexWriteReplay = null;
|
||||
return;
|
||||
}
|
||||
prog.vertexWriteReplay = new Replay(setup);
|
||||
}
|
||||
|
||||
public void ensureImageUsage(Replay replay) {
|
||||
final IntSupplier[] images = replay.images();
|
||||
final int written = replay.setup.writtenImageCount();
|
||||
for (int i = 0; i < images.length; i++) {
|
||||
final int texture = images[i].getAsInt();
|
||||
if (texture != 0) resourceManager.ensureTextureUsage(texture, SDLGPURenderBackend.imageUsageForAccess(imageAccess(i, written)));
|
||||
}
|
||||
}
|
||||
|
||||
public void capture(ContextState st, int first, int count, int instances, int indexType, long indexOffset, int baseVertex) {
|
||||
final ShaderManager.ProgramObject graphics = st.boundProgramObj;
|
||||
final Replay replay = graphics.vertexWriteReplay;
|
||||
final ShaderManager.ProgramObject twin = shaderManager.getProgram(replay.replayProgram);
|
||||
if (twin == null || !twin.linked || twin.sdlComputePipeline == 0) return;
|
||||
if (!replay.resolve(graphics, twin)) return;
|
||||
if (count <= 0 || instances <= 0) return;
|
||||
if (pending == MAX_CAPTURES) {
|
||||
warnOnce("captures", "More than " + MAX_CAPTURES + " draws to replay vertex image writes for in one render pass; the rest are dropped");
|
||||
return;
|
||||
}
|
||||
|
||||
final IntSupplier[] images = replay.images();
|
||||
final Capture c = acquire();
|
||||
c.replay = replay;
|
||||
if (c.buffers.length != replay.setup.vertexBufferCount()) c.buffers = new long[replay.setup.vertexBufferCount()];
|
||||
c.bufferCount = 0;
|
||||
if (!captureInputs(st, replay, c)) return;
|
||||
|
||||
if (indexType != 0) {
|
||||
c.indexBuffer = resourceManager.getBufferHandle(st.currentVao.elementBuffer);
|
||||
c.indexSize = indexType == GL11.GL_UNSIGNED_BYTE ? 1 : indexType == GL11.GL_UNSIGNED_SHORT ? 2 : 4;
|
||||
c.indexOffset = (int) indexOffset;
|
||||
if (c.indexBuffer == 0) return;
|
||||
final long indexBytes = resourceManager.getBufferSize(st.currentVao.elementBuffer);
|
||||
c.indexCount = indexBytes <= 0 ? -1 : (int) Math.min(Math.max(0L, indexBytes - indexOffset) / c.indexSize, Integer.MAX_VALUE);
|
||||
c.restart = st.primitiveRestartEnabled && indexType != GL11.GL_UNSIGNED_BYTE;
|
||||
c.restartIndex = indexType == GL11.GL_UNSIGNED_SHORT ? st.primitiveRestartSentinel & 0xFFFF : st.primitiveRestartSentinel;
|
||||
} else {
|
||||
c.indexBuffer = 0;
|
||||
c.indexSize = 0;
|
||||
c.indexOffset = 0;
|
||||
c.indexCount = 0;
|
||||
c.restart = false;
|
||||
}
|
||||
c.first = first;
|
||||
c.count = count;
|
||||
c.instances = instances;
|
||||
c.baseVertex = baseVertex;
|
||||
|
||||
if (c.samplerTextures.length != replay.samplerUnits.length) {
|
||||
c.samplerTextures = new int[replay.samplerUnits.length];
|
||||
c.samplerHandles = new long[replay.samplerUnits.length];
|
||||
}
|
||||
for (int i = 0; i < replay.samplerUnits.length; i++) {
|
||||
final int unit = replay.samplerUnits[i];
|
||||
final IntSupplier source = replay.samplerSources[i];
|
||||
c.samplerTextures[i] = source != null ? source.getAsInt() : st.boundTextures[unit];
|
||||
final int samplerObject = replay.samplerObjects[i] != SAMPLER_OBJECT_OF_DRAW ? replay.samplerObjects[i] : st.boundSamplerObjects[unit];
|
||||
c.samplerHandles[i] = computeBinder.samplerHandle(samplerObject, c.samplerTextures[i]);
|
||||
}
|
||||
if (c.imageTextures.length != images.length) c.imageTextures = new int[images.length];
|
||||
for (int i = 0; i < images.length; i++) c.imageTextures[i] = images[i].getAsInt();
|
||||
if (c.packStorage.length != replay.packStorageSlots.length) c.packStorage = new long[replay.packStorageSlots.length];
|
||||
for (int i = 0; i < replay.packStorageSlots.length; i++) {
|
||||
c.packStorage[i] = resourceManager.getBufferHandle(st.boundSsboByIndex[replay.packStorageSlots[i]]);
|
||||
}
|
||||
if (c.packRwStorage.length != replay.packRwStorageSlots.length) c.packRwStorage = new long[replay.packRwStorageSlots.length];
|
||||
for (int i = 0; i < replay.packRwStorageSlots.length; i++) {
|
||||
c.packRwStorage[i] = resourceManager.getBufferHandle(st.boundSsboByIndex[replay.packRwStorageSlots[i]]);
|
||||
}
|
||||
captureUniformBlocks(st, twin, replay, c);
|
||||
|
||||
captureUniforms(st, graphics, replay);
|
||||
pending++;
|
||||
}
|
||||
|
||||
private boolean captureInputs(ContextState st, Replay replay, Capture c) {
|
||||
final int n = replay.inputLocations.length;
|
||||
if (c.attr.length < n * 4) {
|
||||
c.attr = new float[n * 4];
|
||||
c.attrDefault = new float[n * 4];
|
||||
}
|
||||
final ContextState.VAOState vao = st.currentVao;
|
||||
for (int k = 0; k < n; k++) {
|
||||
final int loc = replay.inputLocations[k];
|
||||
int x = 0, y = 0, z = 0, w = 0;
|
||||
if (loc >= 0 && loc < ContextState.MAX_VERTEX_ATTRIBS) {
|
||||
System.arraycopy(st.attribDefaults, loc * 4, c.attrDefault, k * 4, 4);
|
||||
if (vao.attribEnabled[loc]) {
|
||||
final int type = componentType(vao.attribType[loc]);
|
||||
final int binding = vao.attribBinding[loc];
|
||||
final int buffer = vao.bindingBuffer[binding];
|
||||
final long handle = type >= 0 ? resourceManager.getBufferHandle(buffer) : 0;
|
||||
if (type < 0) warnOnce("type:" + vao.attribType[loc], "Vertex write replay cannot read vertex attribute type 0x" + Integer.toHexString(vao.attribType[loc]) + "; that input reads its default value");
|
||||
if (handle != 0) {
|
||||
final int slot = bufferSlot(c, handle);
|
||||
if (slot < 0) {
|
||||
warnOnce("buffers", "A draw reads more than " + c.buffers.length + " vertex buffers; its vertex image writes are dropped");
|
||||
return false;
|
||||
}
|
||||
final boolean bgra = vao.attribSize[loc] == GL12.GL_BGRA;
|
||||
final int size = type == PACKED_10F_11F_11F ? 3 : bgra ? 4 : vao.attribSize[loc];
|
||||
final int elementBytes = type >= FIRST_PACKED_TYPE ? 4 : size * COMPONENT_SIZE[type];
|
||||
final int stride = vao.bindingStride[binding] != 0 ? vao.bindingStride[binding] : elementBytes;
|
||||
final long offset = vao.bindingOffset[binding] + vao.attribRelativeOffset[loc];
|
||||
final int divisor = Math.min(vao.bindingDivisor[binding], MAX_DIVISOR);
|
||||
final long bufferSize = resourceManager.getBufferSize(buffer);
|
||||
final boolean noElement = bufferSize > 0 && bufferSize < offset + elementBytes;
|
||||
x = slot | type << 4 | size << 8 | (vao.attribNormalized[loc] ? 0x800 : 0) | (vao.attribIsInteger[loc] ? 0x1000 : 0)
|
||||
| ENABLED | (bgra ? BGRA : 0) | (noElement ? NO_ELEMENT : 0) | divisor << 16;
|
||||
y = (int) offset;
|
||||
z = stride;
|
||||
w = lastElement(bufferSize, offset, elementBytes, stride);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c.attrDefault[k * 4] = 0f;
|
||||
c.attrDefault[k * 4 + 1] = 0f;
|
||||
c.attrDefault[k * 4 + 2] = 0f;
|
||||
c.attrDefault[k * 4 + 3] = 1f;
|
||||
}
|
||||
c.attr[k * 4] = Float.intBitsToFloat(x);
|
||||
c.attr[k * 4 + 1] = Float.intBitsToFloat(y);
|
||||
c.attr[k * 4 + 2] = Float.intBitsToFloat(z);
|
||||
c.attr[k * 4 + 3] = Float.intBitsToFloat(w);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int lastElement(long bufferSize, long offset, int elementBytes, int stride) {
|
||||
if (bufferSize <= 0) return -1;
|
||||
final long room = bufferSize - offset - elementBytes;
|
||||
if (room < 0 || stride <= 0) return 0;
|
||||
return (int) Math.min(room / stride, 0xFFFFFFFFL);
|
||||
}
|
||||
|
||||
private static int bufferSlot(Capture c, long handle) {
|
||||
for (int i = 0; i < c.bufferCount; i++) {
|
||||
if (c.buffers[i] == handle) return i;
|
||||
}
|
||||
if (c.bufferCount == c.buffers.length) return -1;
|
||||
c.buffers[c.bufferCount] = handle;
|
||||
return c.bufferCount++;
|
||||
}
|
||||
|
||||
static int componentType(int glType) {
|
||||
return switch (glType) {
|
||||
case GL11.GL_FLOAT -> 0;
|
||||
case GL30.GL_HALF_FLOAT -> 1;
|
||||
case GL11.GL_BYTE -> 2;
|
||||
case GL11.GL_UNSIGNED_BYTE -> 3;
|
||||
case GL11.GL_SHORT -> 4;
|
||||
case GL11.GL_UNSIGNED_SHORT -> 5;
|
||||
case GL11.GL_INT -> 6;
|
||||
case GL11.GL_UNSIGNED_INT -> 7;
|
||||
case GL41.GL_FIXED -> 8;
|
||||
case GL33.GL_INT_2_10_10_10_REV -> 9;
|
||||
case GL12.GL_UNSIGNED_INT_2_10_10_10_REV -> 10;
|
||||
case GL30.GL_UNSIGNED_INT_10F_11F_11F_REV -> PACKED_10F_11F_11F;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
|
||||
private void captureUniformBlocks(ContextState st, ShaderManager.ProgramObject twin, Replay replay, Capture c) {
|
||||
if (c.uniformBlocks.length != replay.uniformBlockCount) c.uniformBlocks = new ByteBuffer[replay.uniformBlockCount];
|
||||
for (int index : replay.uniformBlockIndices) {
|
||||
final ByteBuffer view = computeBinder.uniformBlockView(st, twin, index);
|
||||
if (view == null) {
|
||||
if (c.uniformBlocks[index] != null) c.uniformBlocks[index].limit(0);
|
||||
continue;
|
||||
}
|
||||
ByteBuffer copy = c.uniformBlocks[index];
|
||||
if (copy == null || copy.capacity() < view.remaining()) copy = c.uniformBlocks[index] = ByteBuffer.allocateDirect(view.remaining()).order(ByteOrder.nativeOrder());
|
||||
copy.clear();
|
||||
copy.put(view);
|
||||
copy.flip();
|
||||
}
|
||||
}
|
||||
|
||||
private void captureUniforms(ContextState st, ShaderManager.ProgramObject graphics, Replay replay) {
|
||||
final UniformStaging us = st.uniformStaging(graphics);
|
||||
final long[] hashes = us.uniformValueHashBySlot;
|
||||
for (int u = 0; u < replay.uniformSources.length; u++) {
|
||||
final int source = replay.uniformSources[u];
|
||||
final float[] value = us.uniformDataBySlot != null && source < us.uniformDataBySlot.length ? us.uniformDataBySlot[source] : null;
|
||||
final long hash = hashes != null && source < hashes.length ? hashes[source] : 0L;
|
||||
if (uniformCount == uniformLengths.length) uniformLengths = Arrays.copyOf(uniformLengths, uniformLengths.length * 2);
|
||||
if (value != null && replay.lastHashesApplied && hash == replay.lastHashes[u]) {
|
||||
uniformLengths[uniformCount++] = UNCHANGED;
|
||||
continue;
|
||||
}
|
||||
replay.lastHashes[u] = hash;
|
||||
final int len = value == null ? 0 : value.length;
|
||||
uniformLengths[uniformCount++] = len;
|
||||
if (len == 0) continue;
|
||||
if (uniformValueCount + len > uniformValues.length) uniformValues = Arrays.copyOf(uniformValues, Math.max(uniformValues.length * 2, uniformValueCount + len));
|
||||
System.arraycopy(value, 0, uniformValues, uniformValueCount, len);
|
||||
uniformValueCount += len;
|
||||
}
|
||||
replay.lastHashesApplied = true;
|
||||
}
|
||||
|
||||
public boolean hasPending() {
|
||||
return pending > 0;
|
||||
}
|
||||
|
||||
public void noteIndirectDraw() {
|
||||
warnOnce("indirect", "An indirect draw used a program whose vertex shader writes images; those writes are dropped");
|
||||
}
|
||||
|
||||
private Capture acquire() {
|
||||
if (pending == captures.size()) captures.add(new Capture());
|
||||
return captures.get(pending);
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
if (pending == 0 || flushing) return;
|
||||
flushing = true;
|
||||
final ContextState st = state.get();
|
||||
final int savedProgram = st.boundProgram;
|
||||
final ShaderManager.ProgramObject savedProgramObj = st.boundProgramObj;
|
||||
try {
|
||||
uniformCursor = 0;
|
||||
valueCursor = 0;
|
||||
int start = 0;
|
||||
while (start < pending) {
|
||||
final Capture first = captures.get(start);
|
||||
final Replay replay = first.replay;
|
||||
int end = start;
|
||||
while (end < pending && captures.get(end).replay == replay && Arrays.equals(captures.get(end).imageTextures, first.imageTextures)
|
||||
&& Arrays.equals(captures.get(end).packRwStorage, first.packRwStorage)) end++;
|
||||
replayRun(st, replay, start, end);
|
||||
start = end;
|
||||
}
|
||||
} finally {
|
||||
st.boundProgram = savedProgram;
|
||||
st.boundProgramObj = savedProgramObj;
|
||||
for (int i = 0; i < pending; i++) captures.get(i).replay = null;
|
||||
pending = 0;
|
||||
uniformCount = 0;
|
||||
uniformValueCount = 0;
|
||||
flushing = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void replayRun(ContextState st, Replay replay, int start, int end) {
|
||||
final ShaderManager.ProgramObject twin = shaderManager.getProgram(replay.replayProgram);
|
||||
if (twin == null || !twin.linked || twin.sdlComputePipeline == 0) {
|
||||
skipUniforms(replay, start, end);
|
||||
return;
|
||||
}
|
||||
|
||||
final Capture first = captures.get(start);
|
||||
final int[] imageTextures = first.imageTextures;
|
||||
final int imageCount = imageTextures.length;
|
||||
final int written = replay.setup.writtenImageCount();
|
||||
System.arraycopy(st.boundStorageTextureByUnit, 0, savedImages, 0, imageCount);
|
||||
System.arraycopy(st.boundStorageTextureLevel, 0, savedLevels, 0, imageCount);
|
||||
System.arraycopy(st.boundStorageTextureAccess, 0, savedAccess, 0, imageCount);
|
||||
for (int i = 0; i < replay.samplerUnits.length; i++) savedTextures[i] = st.boundTextures[replay.samplerUnits[i]];
|
||||
computeBinder.setSamplerOverrides(samplerOverrides);
|
||||
Arrays.fill(rwHandlesByGlSlot, 0L);
|
||||
for (int i = 0; i < replay.packRwStorageSlots.length; i++) rwHandlesByGlSlot[replay.packRwStorageSlots[i]] = first.packRwStorage[i];
|
||||
computeBinder.setReadWriteStorageBufferOverrides(rwHandlesByGlSlot);
|
||||
|
||||
try {
|
||||
for (int i = 0; i < imageCount; i++) {
|
||||
st.boundStorageTextureByUnit[i] = imageTextures[i];
|
||||
st.boundStorageTextureLevel[i] = 0;
|
||||
st.boundStorageTextureAccess[i] = imageAccess(i, written);
|
||||
}
|
||||
applySamplerTextures(st, replay, captures.get(start));
|
||||
|
||||
st.boundProgram = replay.replayProgram;
|
||||
st.boundProgramObj = twin;
|
||||
final long pass = computeBinder.beginComputePassWithoutStorageBuffers(st);
|
||||
if (pass == 0) {
|
||||
for (int i = start; i < end; i++) applyUniforms(st, replay);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for (int i = start; i < end; i++) {
|
||||
final Capture c = captures.get(i);
|
||||
if (i > start && applySamplerTextures(st, replay, c)) computeBinder.rebindSamplers(pass, st);
|
||||
dispatch(st, pass, replay, c);
|
||||
}
|
||||
} finally {
|
||||
computeBinder.endBatchedComputeDispatch(pass);
|
||||
}
|
||||
} finally {
|
||||
computeBinder.setReadWriteStorageBufferOverrides(null);
|
||||
computeBinder.setUniformBlockOverrides(null);
|
||||
System.arraycopy(savedImages, 0, st.boundStorageTextureByUnit, 0, imageCount);
|
||||
System.arraycopy(savedLevels, 0, st.boundStorageTextureLevel, 0, imageCount);
|
||||
System.arraycopy(savedAccess, 0, st.boundStorageTextureAccess, 0, imageCount);
|
||||
for (int i = 0; i < replay.samplerUnits.length; i++) {
|
||||
st.boundTextures[replay.samplerUnits[i]] = savedTextures[i];
|
||||
samplerOverrides[replay.samplerUnits[i]] = 0L;
|
||||
}
|
||||
computeBinder.setSamplerOverrides(null);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean applySamplerTextures(ContextState st, Replay replay, Capture c) {
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < replay.samplerUnits.length; i++) {
|
||||
final int unit = replay.samplerUnits[i];
|
||||
if (st.boundTextures[unit] != c.samplerTextures[i] || samplerOverrides[unit] != c.samplerHandles[i]) {
|
||||
st.boundTextures[unit] = c.samplerTextures[i];
|
||||
samplerOverrides[unit] = c.samplerHandles[i];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
private void skipUniforms(Replay replay, int start, int end) {
|
||||
replay.lastHashesApplied = false;
|
||||
for (int i = start; i < end; i++) {
|
||||
for (int u = 0; u < replay.uniformSources.length; u++) valueCursor += Math.max(0, uniformLengths[uniformCursor++]);
|
||||
}
|
||||
}
|
||||
|
||||
private void dispatch(ContextState st, long pass, Replay replay, Capture c) {
|
||||
Arrays.fill(handlesByGlSlot, 0L);
|
||||
for (int i = 0; i < replay.packStorageSlots.length; i++) handlesByGlSlot[replay.packStorageSlots[i]] = c.packStorage[i];
|
||||
System.arraycopy(c.buffers, 0, handlesByGlSlot, replay.setup.vertexBufferBinding(), c.bufferCount);
|
||||
handlesByGlSlot[replay.setup.indexBufferBinding()] = c.indexBuffer;
|
||||
final boolean bound = computeBinder.bindRoStorageBufferHandles(pass, st, handlesByGlSlot);
|
||||
|
||||
applyUniforms(st, replay);
|
||||
if (!bound) return;
|
||||
|
||||
final int inputs = replay.inputLocations.length;
|
||||
putFloats(st, replay.locAttr, c.attr, inputs * 4);
|
||||
putFloats(st, replay.locAttrDefault, c.attrDefault, inputs * 4);
|
||||
putIntVector(st, replay.locDraw, 4, c.first, c.count, c.instances, c.baseVertex);
|
||||
putIntVector(st, replay.locIndex, 3, c.indexSize, c.indexOffset, c.indexCount, 0);
|
||||
putIntVector(st, replay.locRestart, 2, c.restart ? 1 : 0, c.restartIndex, 0, 0);
|
||||
computeBinder.setUniformBlockOverrides(c.uniformBlocks);
|
||||
|
||||
InvocationDispatch.dispatch(computeBinder, st, pass, (long) c.count * c.instances, replay.locInvocationBase, invocationBaseWriter);
|
||||
}
|
||||
|
||||
private void putFloats(ContextState st, int location, float[] source, int len) {
|
||||
if (location < 0 || len == 0) return;
|
||||
final float[] value = pipelineApplier.reuseOrAlloc(st, location, len);
|
||||
if (value == null) return;
|
||||
System.arraycopy(source, 0, value, 0, len);
|
||||
pipelineApplier.putUniform(st, location, value);
|
||||
}
|
||||
|
||||
private void applyUniforms(ContextState st, Replay replay) {
|
||||
for (int u = 0; u < replay.uniformSources.length; u++) {
|
||||
final int len = uniformLengths[uniformCursor++];
|
||||
if (len <= 0) continue;
|
||||
final float[] value = pipelineApplier.reuseOrAlloc(st, replay.uniformTargets[u], len);
|
||||
if (value != null) {
|
||||
System.arraycopy(uniformValues, valueCursor, value, 0, len);
|
||||
pipelineApplier.putUniform(st, replay.uniformTargets[u], value);
|
||||
}
|
||||
valueCursor += len;
|
||||
}
|
||||
}
|
||||
|
||||
private static int imageAccess(int index, int writtenImageCount) {
|
||||
return index < writtenImageCount ? GL15.GL_READ_WRITE : GL15.GL_READ_ONLY;
|
||||
}
|
||||
|
||||
private void putIntVector(ContextState st, int location, int count, int x, int y, int z, int w) {
|
||||
if (location < 0) return;
|
||||
final float[] value = pipelineApplier.reuseOrAlloc(st, location, count);
|
||||
if (value == null) return;
|
||||
value[0] = Float.intBitsToFloat(x);
|
||||
if (count > 1) value[1] = Float.intBitsToFloat(y);
|
||||
if (count > 2) value[2] = Float.intBitsToFloat(z);
|
||||
if (count > 3) value[3] = Float.intBitsToFloat(w);
|
||||
pipelineApplier.putUniform(st, location, value);
|
||||
}
|
||||
|
||||
private void warnOnce(String key, String message) {
|
||||
if (warned.add(key)) LOG.warn(message);
|
||||
}
|
||||
}
|
||||
+2
-8
@@ -4,8 +4,7 @@ import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState;
|
||||
|
||||
public final class VoxelizationDispatcher {
|
||||
static final int MAX_GROUPS_PER_DISPATCH = 65535;
|
||||
private static final int WORKGROUP_SIZE = 64;
|
||||
private static final InvocationDispatch.IntUniformWriter UNIFORM_1I = (st, location, value) -> GLStateManager.glUniform1i(location, value);
|
||||
|
||||
private final ComputeDispatchSink computeBinder;
|
||||
|
||||
@@ -25,12 +24,7 @@ public final class VoxelizationDispatcher {
|
||||
if (locBase >= 0) GLStateManager.glUniform1i(locBase, rangeBase);
|
||||
if (locCount >= 0) GLStateManager.glUniform1i(locCount, rangeCount);
|
||||
if (locTotal >= 0) GLStateManager.glUniform1i(locTotal, vertexTotal);
|
||||
final int groups = (vertexTotal + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE;
|
||||
for (int g0 = 0; g0 < groups; g0 += MAX_GROUPS_PER_DISPATCH) {
|
||||
if (locInvBase >= 0) GLStateManager.glUniform1i(locInvBase, g0 * WORKGROUP_SIZE);
|
||||
computeBinder.pushPendingComputeUniforms(st);
|
||||
computeBinder.dispatchInBatch(pass, Math.min(MAX_GROUPS_PER_DISPATCH, groups - g0), 1, 1);
|
||||
}
|
||||
InvocationDispatch.dispatch(computeBinder, st, pass, vertexTotal, locInvBase, UNIFORM_1I);
|
||||
}
|
||||
|
||||
public void endBatch(long pass) {
|
||||
|
||||
@@ -5,7 +5,9 @@ import com.gtnewhorizons.angelica.glsm.backend.BackendOptions;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.MoltenVK;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VSyncMode;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.RenderBackend;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.compute.ImageAtomicsProbe;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.util.DebugMessageRelay;
|
||||
import it.unimi.dsi.fastutil.longs.Long2BooleanOpenHashMap;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.lwjgl.sdl.SDLError;
|
||||
@@ -49,6 +51,9 @@ public final class Device {
|
||||
private String driverName;
|
||||
private boolean fencePollEnabled;
|
||||
private boolean fenceQueryInverted;
|
||||
private int defaultMslVersion;
|
||||
private ImageAtomicsProbe imageAtomicsProbe;
|
||||
private final Long2BooleanOpenHashMap imageAtomicsBySize = new Long2BooleanOpenHashMap();
|
||||
private final FenceReleaser fenceReleaser = new FenceReleaser(fence -> device == 0 || FenceWait.isSignaled(this, fence), fence -> { if (device != 0) SDL_ReleaseGPUFence(device, fence); });
|
||||
private String deviceName;
|
||||
private String driverVersion;
|
||||
@@ -153,10 +158,36 @@ public final class Device {
|
||||
}
|
||||
|
||||
probeFences(ver);
|
||||
readMetalLanguageVersion();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void readMetalLanguageVersion() {
|
||||
if (!isMetal(driverName)) return;
|
||||
defaultMslVersion = ImageAtomicsProbe.defaultMslVersion();
|
||||
if (metalTextureAtomics()) {
|
||||
LOG.info("Metal image atomics: MSL 3.1 available, checked per image size");
|
||||
} else {
|
||||
LOG.info("Metal image atomics: unavailable, this Java runtime compiles Metal shaders as MSL {}.{} and they need 3.1. A runtime built with the macOS 14 SDK or newer enables them.", defaultMslVersion >>> 16, defaultMslVersion & 0xFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean metalTextureAtomics() {
|
||||
return defaultMslVersion >= ImageAtomicsProbe.MTL_LANGUAGE_VERSION_3_1;
|
||||
}
|
||||
|
||||
public boolean supportsImageAtomics(int width, int height, int depth) {
|
||||
if (!metalTextureAtomics() || width <= 0 || height <= 0 || depth > 1) return false;
|
||||
final long key = (long) width << 32 | height;
|
||||
if (imageAtomicsBySize.containsKey(key)) return imageAtomicsBySize.get(key);
|
||||
if (imageAtomicsProbe == null) imageAtomicsProbe = new ImageAtomicsProbe(device);
|
||||
final boolean ok = imageAtomicsProbe.run(width, height);
|
||||
if (ok) LOG.info("Metal image atomics on {}x{}: ok", width, height);
|
||||
imageAtomicsBySize.put(key, ok);
|
||||
return ok;
|
||||
}
|
||||
|
||||
private void probeFences(int ver) {
|
||||
fencePollEnabled = FenceWait.pollEnabled(driverName, ver);
|
||||
final boolean expectInverted = isMetal(driverName) && ver == SDLVersion.SDL_VERSIONNUM(3, 4, 14);
|
||||
@@ -256,11 +287,15 @@ public final class Device {
|
||||
claimed = false;
|
||||
}
|
||||
fenceReleaser.releaseAll();
|
||||
if (imageAtomicsProbe != null) imageAtomicsProbe.release();
|
||||
imageAtomicsProbe = null;
|
||||
SDL_DestroyGPUDevice(device);
|
||||
device = 0;
|
||||
driverName = null;
|
||||
fencePollEnabled = false;
|
||||
fenceQueryInverted = false;
|
||||
defaultMslVersion = 0;
|
||||
imageAtomicsBySize.clear();
|
||||
deviceName = null;
|
||||
driverVersion = null;
|
||||
driverInfo = null;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.gtnewhorizons.angelica.sdlgpu.frame;
|
||||
import com.gtnewhorizons.angelica.glsm.GLContextState;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.SDLGPURenderBackend;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.pipeline.PipelineCache;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.MappedRange;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.resource.PackState;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.LogicOpVariant;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.ShaderManager;
|
||||
@@ -314,12 +315,7 @@ public final class ContextState {
|
||||
public final long[] computeBatchRwTexs = new long[MAX_IMAGE_UNITS];
|
||||
public final int[] computeBatchRwTexLevels = new int[MAX_IMAGE_UNITS];
|
||||
|
||||
public int mappedBufferGlId;
|
||||
public ByteBuffer mappedStagingBuffer;
|
||||
public long mappedOffset;
|
||||
public long mappedLength;
|
||||
public boolean mappedInvalidate;
|
||||
public int mappedAccessFlags;
|
||||
public final MappedRange mappingScratch = new MappedRange();
|
||||
|
||||
public final SDL_GPUTextureSamplerBinding.Buffer fragSamplerBindings = SDL_GPUTextureSamplerBinding.calloc(MAX_SAMPLERS);
|
||||
public final SDL_GPUTextureSamplerBinding.Buffer vertSamplerBindings = SDL_GPUTextureSamplerBinding.calloc(MAX_SAMPLERS);
|
||||
|
||||
@@ -381,11 +381,14 @@ public final class FrameManager {
|
||||
|
||||
|
||||
private volatile Runnable preRenderPassHook;
|
||||
private volatile Runnable renderPassEndHook;
|
||||
private volatile Runnable beforeSubmit;
|
||||
private volatile Runnable afterPresent;
|
||||
|
||||
public void setPreRenderPassHook(Runnable hook) { this.preRenderPassHook = hook; }
|
||||
|
||||
public void setRenderPassEndHook(Runnable hook) { this.renderPassEndHook = hook; }
|
||||
|
||||
public void noteComputePassBegun() {
|
||||
final FrameState f = frame();
|
||||
f.computePassesThisFrame++;
|
||||
@@ -472,6 +475,8 @@ public final class FrameManager {
|
||||
f.currentColorTarget = 0;
|
||||
f.currentDepthTarget = 0;
|
||||
f.activeLayoutHash = 0;
|
||||
final Runnable hook = renderPassEndHook;
|
||||
if (hook != null) hook.run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -173,8 +173,9 @@ public final class FFPTrace {
|
||||
sb.append(" | ffpUbo=buffer").append(glId).append(" noStaging");
|
||||
return;
|
||||
}
|
||||
final int base = st.uboRangeOffset[binding];
|
||||
sb.append(" | ffpUbo=buffer").append(glId).append(" base=").append(base);
|
||||
final int rangeOffset = st.uboRangeOffset[binding];
|
||||
final int base = pm != null ? (int) pm.stagingIndex(rangeOffset) : rangeOffset;
|
||||
sb.append(" | ffpUbo=buffer").append(glId).append(" base=").append(rangeOffset);
|
||||
appendUboFloat(buf, base, FFPUniformBlock.ALPHA_REF, " u_AlphaRef=");
|
||||
appendUboMatrix(buf, base, FFPUniformBlock.MVP_MATRIX, " u_MVPMatrix=");
|
||||
appendUboMatrix(buf, base, FFPUniformBlock.LIGHTMAP_TEXTURE_MATRIX, " u_LightmapTextureMatrix=");
|
||||
|
||||
+2
-2
@@ -698,8 +698,8 @@ public final class PipelineApplier {
|
||||
final int offset = st.uboRangeOffset[binding];
|
||||
final PersistentMapping pm = resourceManager.getPersistentMapping(glId);
|
||||
if (pm != null) {
|
||||
if (offset < 0 || offset + size > pm.staging.capacity()) return 0L;
|
||||
return MemoryUtil.memAddress0(pm.staging) + offset;
|
||||
if (!pm.covers(offset, size)) return 0L;
|
||||
return MemoryUtil.memAddress0(pm.staging) + pm.stagingIndex(offset);
|
||||
}
|
||||
final ByteBuffer shadow = resourceManager.getUboShadow(glId);
|
||||
if (shadow != null) {
|
||||
|
||||
+4
-7
@@ -8,16 +8,13 @@ public final class ByteRegionCopy {
|
||||
private ByteRegionCopy() {}
|
||||
|
||||
public static void copyByteRegion(ByteBuffer src, int srcOff, ByteBuffer dst, int dstOff, int len) {
|
||||
if ((srcOff | dstOff | len) < 0 || srcOff + (long) len > src.capacity() || dstOff + (long) len > dst.capacity()) {
|
||||
throw new IllegalStateException("copyByteRegion out of bounds: len=" + len + " srcOff=" + srcOff + " src.capacity=" + src.capacity() + " dstOff=" + dstOff + " dst.capacity=" + dst.capacity());
|
||||
}
|
||||
if (src.isDirect() && dst.isDirect()) {
|
||||
MemoryUtil.memCopy(MemoryUtil.memAddress(src, srcOff), MemoryUtil.memAddress(dst, dstOff), len);
|
||||
return;
|
||||
}
|
||||
if (src.hasArray() && dst.hasArray()) {
|
||||
System.arraycopy(src.array(), src.arrayOffset() + srcOff, dst.array(), dst.arrayOffset() + dstOff, len);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < len; i++) {
|
||||
dst.put(dstOff + i, src.get(srcOff + i));
|
||||
}
|
||||
dst.put(dstOff, src, srcOff, len);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.resource;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public final class MappedRange {
|
||||
public int glId;
|
||||
public ByteBuffer staging;
|
||||
public long offset;
|
||||
public long length;
|
||||
public boolean invalidate;
|
||||
public int accessFlags;
|
||||
}
|
||||
+35
-21
@@ -33,11 +33,14 @@ public final class PersistentBufferSync {
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
public void onPersistentBufferWrite(int glId, long offset, long size) {
|
||||
public boolean onPersistentBufferWrite(int glId, long offset, long size) {
|
||||
final PersistentMapping pm = resourceManager.getPersistentMapping(glId);
|
||||
if (pm != null && pm.markDirty(offset, size)) {
|
||||
if (pm == null || size == 0) return true;
|
||||
if (offset < 0 || size < 0 || offset > pm.length || size > pm.length - offset) return false;
|
||||
if (pm.markDirty(offset, size)) {
|
||||
resourceManager.trackPersistentDirty();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void uploadDirtyPersistentRegion() { processDirtyPersistentRegions(false); }
|
||||
@@ -59,30 +62,41 @@ public final class PersistentBufferSync {
|
||||
for (int i = 0; i < n; i++) {
|
||||
final PersistentMapping pm = vals.get(i);
|
||||
if (!pm.isDirty()) continue;
|
||||
final long gpuHandle = resourceManager.getBufferHandle(keys.getInt(i));
|
||||
if (gpuHandle == 0) continue;
|
||||
final long claimed = pm.claimDirty();
|
||||
if (PersistentMapping.isClean(claimed)) continue;
|
||||
final long off = PersistentMapping.rangeOffset(claimed);
|
||||
final long size = PersistentMapping.rangeSize(claimed);
|
||||
if (Tracy.ENABLED) frameManager.notePersistentDrain();
|
||||
if (defer) {
|
||||
final long seq = sink.nextSeq();
|
||||
pm.lastEnqueuedSeq = seq;
|
||||
sink.enqueue(TransferThread.StagingReadUpload.acquire(pm.staging, off, size, gpuHandle, off, seq, false));
|
||||
enqueued++;
|
||||
} else {
|
||||
pm.staging.position((int) off).limit((int) (off + size));
|
||||
resourceManager.uploadToBuffer(frameManager.ensureCopyPass(), pm.staging, gpuHandle, off, false);
|
||||
pm.staging.clear();
|
||||
final int glId = keys.getInt(i);
|
||||
long copyPass = 0L;
|
||||
if (!defer) {
|
||||
if (resourceManager.getBufferHandle(glId) == 0) continue;
|
||||
copyPass = frameManager.ensureCopyPass();
|
||||
}
|
||||
resourceManager.beginPersistentDrain();
|
||||
try {
|
||||
if (resourceManager.getPersistentMapping(glId) != pm) continue;
|
||||
final long gpuHandle = resourceManager.getBufferHandle(glId);
|
||||
if (gpuHandle == 0) continue;
|
||||
final long claimed = pm.claimDirty();
|
||||
if (PersistentMapping.isClean(claimed)) continue;
|
||||
resourceManager.clearPersistentDirty();
|
||||
final long off = PersistentMapping.rangeOffset(claimed);
|
||||
final long size = PersistentMapping.rangeSize(claimed);
|
||||
if (Tracy.ENABLED) frameManager.notePersistentDrain();
|
||||
if (defer) {
|
||||
final long seq = sink.nextSeq();
|
||||
pm.lastEnqueuedSeq = seq;
|
||||
sink.enqueue(TransferThread.StagingReadUpload.acquire(pm.staging, off, size, gpuHandle, pm.offset + off, seq, false));
|
||||
enqueued++;
|
||||
} else {
|
||||
resourceManager.uploadRangeToBuffer(copyPass, pm.staging, off, size, gpuHandle, pm.offset + off, false);
|
||||
}
|
||||
} finally {
|
||||
resourceManager.endPersistentDrain();
|
||||
}
|
||||
resourceManager.clearPersistentDirty();
|
||||
}
|
||||
return enqueued;
|
||||
}
|
||||
|
||||
public static void mirrorPersistentCopy(ByteBuffer srcStaging, long srcOffset, ByteBuffer dstStaging, long dstOffset, long size) {
|
||||
ByteRegionCopy.copyByteRegion(srcStaging, (int) srcOffset, dstStaging, (int) dstOffset, (int) size);
|
||||
public static void mirrorPersistentCopy(PersistentMapping src, long readOffset, PersistentMapping dst, long writeOffset, long size) {
|
||||
if (!src.covers(readOffset, size) || !dst.covers(writeOffset, size)) return;
|
||||
ByteRegionCopy.copyByteRegion(src.staging, (int) src.stagingIndex(readOffset), dst.staging, (int) dst.stagingIndex(writeOffset), (int) size);
|
||||
}
|
||||
|
||||
public void mirrorEboShadow(int glId, ByteBuffer src, int dstOffset, int len) {
|
||||
|
||||
+6
@@ -24,6 +24,12 @@ public final class PersistentMapping {
|
||||
public static long rangeSize(long packed) { return (packed & 0xFFFFFFFFL) - (packed >>> 32); }
|
||||
static long packRange(long min, long max) { return (min << 32) | (max & 0xFFFFFFFFL); }
|
||||
|
||||
public long stagingIndex(long bufferOffset) { return bufferOffset - offset; }
|
||||
|
||||
public boolean covers(long bufferOffset, long size) {
|
||||
return size >= 0 && bufferOffset >= offset && bufferOffset - offset <= length && size <= length - (bufferOffset - offset);
|
||||
}
|
||||
|
||||
public boolean isDirty() { return dirtyRange.get() != CLEAN; }
|
||||
|
||||
public long claimDirty() { return dirtyRange.getAndSet(CLEAN); }
|
||||
|
||||
+207
-44
@@ -31,6 +31,7 @@ import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -96,6 +97,9 @@ public final class ResourceManager {
|
||||
|
||||
private final Int2ObjectOpenHashMap<ContextState.VAOState> vaoStates = new Int2ObjectOpenHashMap<>();
|
||||
private final Int2ObjectOpenHashMap<PersistentMapping> persistentMappings = new Int2ObjectOpenHashMap<>();
|
||||
private final Int2ObjectOpenHashMap<MappedRange> plainMappings = new Int2ObjectOpenHashMap<>();
|
||||
private MappedRange[] plainMappingPool = new MappedRange[4];
|
||||
private int plainMappingPoolCount;
|
||||
private final Int2ObjectOpenHashMap<ByteBuffer> uboShadows = new Int2ObjectOpenHashMap<>();
|
||||
private final Int2ObjectOpenHashMap<ByteBuffer> eboShadows = new Int2ObjectOpenHashMap<>();
|
||||
private final IntOpenHashSet eboShadowWanted = new IntOpenHashSet();
|
||||
@@ -244,6 +248,7 @@ public final class ResourceManager {
|
||||
}
|
||||
|
||||
private long createTextureCore(int glId, int glTarget, int sdlFormat, int glFormat, int width, int height, int depth, int levels, int usage) {
|
||||
if (getTextureHandle(glId) != 0) beforeTextureReplaced();
|
||||
usage = reconcileUsageAndWarn(glId, usage);
|
||||
final int sdlType = mapTextureType(glTarget);
|
||||
if (!SDL_GPUTextureSupportsFormat(device.getDevice(), sdlFormat, sdlType, usage)) {
|
||||
@@ -305,7 +310,19 @@ public final class ResourceManager {
|
||||
return sdlFormat;
|
||||
}
|
||||
|
||||
private Runnable textureReplaceHook;
|
||||
|
||||
public void setTextureReplaceHook(Runnable hook) {
|
||||
textureReplaceHook = hook;
|
||||
}
|
||||
|
||||
private void beforeTextureReplaced() {
|
||||
final Runnable hook = textureReplaceHook;
|
||||
if (hook != null) hook.run();
|
||||
}
|
||||
|
||||
public void deleteTexture(int glId) {
|
||||
beforeTextureReplaced();
|
||||
final long handle;
|
||||
wLock.lock();
|
||||
try {
|
||||
@@ -336,6 +353,7 @@ public final class ResourceManager {
|
||||
}
|
||||
|
||||
public void releaseTextureHandleForRealloc(int glId) {
|
||||
beforeTextureReplaced();
|
||||
final long handle;
|
||||
wLock.lock();
|
||||
try {
|
||||
@@ -752,6 +770,7 @@ public final class ResourceManager {
|
||||
|
||||
public void deleteBuffer(int glId) {
|
||||
final ByteBuffer droppedPboStaging;
|
||||
final ByteBuffer droppedPlainStaging;
|
||||
final PersistentMapping droppedPm;
|
||||
wLock.lock();
|
||||
try {
|
||||
@@ -763,6 +782,7 @@ public final class ResourceManager {
|
||||
undefinedContentBuffers.remove(glId);
|
||||
droppedPm = persistentMappings.remove(glId);
|
||||
if (droppedPm != null) mappingsVersion++;
|
||||
droppedPlainStaging = removePlainMappingLocked(glId);
|
||||
droppedPboStaging = pboStagingData.remove(glId);
|
||||
deleteUboShadow(glId);
|
||||
deleteEboShadow(glId);
|
||||
@@ -777,8 +797,11 @@ public final class ResourceManager {
|
||||
if (droppedPboStaging != null) {
|
||||
memFree(droppedPboStaging);
|
||||
}
|
||||
if (droppedPlainStaging != null) {
|
||||
memFree(droppedPlainStaging);
|
||||
}
|
||||
untrackPersistentDirty(droppedPm);
|
||||
releasePersistentStaging(droppedPm);
|
||||
releasePersistentStaging(droppedPm, glId, "delete");
|
||||
}
|
||||
|
||||
public void flushDeferredReleases() {
|
||||
@@ -953,10 +976,81 @@ public final class ResourceManager {
|
||||
try { vaoStates.remove(glId); } finally { wLock.unlock(); }
|
||||
}
|
||||
|
||||
public void putPersistentMapping(int bufferGlId, PersistentMapping m) {
|
||||
public boolean putPersistentMappingIfAbsent(int glId, PersistentMapping pm) {
|
||||
wLock.lock();
|
||||
try { persistentMappings.put(bufferGlId, m); mappingsVersion++; } finally { wLock.unlock(); }
|
||||
try {
|
||||
if (plainMappings.containsKey(glId) || persistentMappings.putIfAbsent(glId, pm) != null) return false;
|
||||
mappingsVersion++;
|
||||
return true;
|
||||
} finally { wLock.unlock(); }
|
||||
}
|
||||
|
||||
public boolean putPlainMappingIfAbsent(int glId, ByteBuffer staging, long offset, long length, boolean invalidate, int accessFlags) {
|
||||
wLock.lock();
|
||||
try {
|
||||
if (plainMappings.containsKey(glId) || persistentMappings.containsKey(glId)) return false;
|
||||
final MappedRange m;
|
||||
if (plainMappingPoolCount > 0) {
|
||||
m = plainMappingPool[--plainMappingPoolCount];
|
||||
plainMappingPool[plainMappingPoolCount] = null;
|
||||
} else {
|
||||
m = new MappedRange();
|
||||
}
|
||||
m.glId = glId;
|
||||
m.staging = staging;
|
||||
m.offset = offset;
|
||||
m.length = length;
|
||||
m.invalidate = invalidate;
|
||||
m.accessFlags = accessFlags;
|
||||
plainMappings.put(glId, m);
|
||||
return true;
|
||||
} finally { wLock.unlock(); }
|
||||
}
|
||||
|
||||
public boolean takePlainMapping(int glId, MappedRange out) {
|
||||
wLock.lock();
|
||||
try {
|
||||
final MappedRange m = plainMappings.get(glId);
|
||||
if (m == null) return false;
|
||||
copyMappedRange(m, out);
|
||||
removePlainMappingLocked(glId);
|
||||
return true;
|
||||
} finally { wLock.unlock(); }
|
||||
}
|
||||
|
||||
public ByteBuffer takePlainStaging(int glId) {
|
||||
wLock.lock();
|
||||
try { return removePlainMappingLocked(glId); } finally { wLock.unlock(); }
|
||||
}
|
||||
|
||||
public boolean peekPlainMapping(int glId, MappedRange out) {
|
||||
if (fastRead()) return copyMappedRange(plainMappings.get(glId), out);
|
||||
rLock.lock();
|
||||
try { return copyMappedRange(plainMappings.get(glId), out); } finally { rLock.unlock(); }
|
||||
}
|
||||
|
||||
private static boolean copyMappedRange(MappedRange m, MappedRange out) {
|
||||
if (m == null) return false;
|
||||
out.glId = m.glId;
|
||||
out.staging = m.staging;
|
||||
out.offset = m.offset;
|
||||
out.length = m.length;
|
||||
out.invalidate = m.invalidate;
|
||||
out.accessFlags = m.accessFlags;
|
||||
return true;
|
||||
}
|
||||
|
||||
private ByteBuffer removePlainMappingLocked(int glId) {
|
||||
final MappedRange m = plainMappings.remove(glId);
|
||||
if (m == null) return null;
|
||||
final ByteBuffer staging = m.staging;
|
||||
m.staging = null;
|
||||
if (plainMappingPoolCount == plainMappingPool.length) plainMappingPool = Arrays.copyOf(plainMappingPool, plainMappingPoolCount * 2);
|
||||
plainMappingPool[plainMappingPoolCount++] = m;
|
||||
return staging;
|
||||
}
|
||||
public void beginPersistentDrain() { rLock.lock(); }
|
||||
public void endPersistentDrain() { rLock.unlock(); }
|
||||
public PersistentMapping getPersistentMapping(int bufferGlId) {
|
||||
if (fastRead()) return persistentMappings.get(bufferGlId);
|
||||
rLock.lock();
|
||||
@@ -965,17 +1059,13 @@ public final class ResourceManager {
|
||||
public PersistentMapping removePersistentMapping(int bufferGlId) {
|
||||
final PersistentMapping dropped;
|
||||
wLock.lock();
|
||||
try { dropped = persistentMappings.remove(bufferGlId); mappingsVersion++; } finally { wLock.unlock(); }
|
||||
try {
|
||||
dropped = persistentMappings.remove(bufferGlId);
|
||||
if (dropped != null) mappingsVersion++;
|
||||
} finally { wLock.unlock(); }
|
||||
untrackPersistentDirty(dropped);
|
||||
return dropped;
|
||||
}
|
||||
public PersistentMapping swapPersistentMapping(int bufferGlId, PersistentMapping fresh) {
|
||||
final PersistentMapping prior;
|
||||
wLock.lock();
|
||||
try { prior = persistentMappings.put(bufferGlId, fresh); mappingsVersion++; } finally { wLock.unlock(); }
|
||||
untrackPersistentDirty(prior);
|
||||
return prior;
|
||||
}
|
||||
|
||||
private volatile int mappingsVersion;
|
||||
|
||||
@@ -989,8 +1079,9 @@ public final class ResourceManager {
|
||||
if (pm != null && !PersistentMapping.isClean(pm.claimDirty())) persistentDirtyCount.decrementAndGet();
|
||||
}
|
||||
|
||||
public void releasePersistentStaging(PersistentMapping pm) {
|
||||
public void releasePersistentStaging(PersistentMapping pm, int glId, String cause) {
|
||||
if (pm == null || pm.staging == null) return;
|
||||
if (LOG.isDebugEnabled()) LOG.debug("Persistent mapping released: buffer {} ({} KiB) via {}", glId, pm.length >> 10, cause);
|
||||
final TransferThread tt = transferThread;
|
||||
final long seq = pm.lastEnqueuedSeq;
|
||||
if (tt != null && seq > tt.getSubmittedSeq()) tt.freeAfterSeq(pm.staging, seq);
|
||||
@@ -1001,24 +1092,20 @@ public final class ResourceManager {
|
||||
rLock.lock();
|
||||
try {
|
||||
final PersistentMapping srcPm = persistentMappings.get(srcGlId);
|
||||
if (srcPm == null) return false;
|
||||
final ByteBuffer slice = srcPm.staging.duplicate();
|
||||
slice.position((int) readOffset).limit((int) (readOffset + size));
|
||||
uploadToBuffer(copyPass, slice, dstHandle, writeOffset, false);
|
||||
if (srcPm == null || !srcPm.covers(readOffset, size)) return false;
|
||||
uploadRangeToBuffer(copyPass, srcPm.staging, srcPm.stagingIndex(readOffset), size, dstHandle, writeOffset, false);
|
||||
final PersistentMapping dstPm = persistentMappings.get(dstGlId);
|
||||
if (dstPm != null) {
|
||||
PersistentBufferSync.mirrorPersistentCopy(srcPm.staging, readOffset, dstPm.staging, writeOffset, size);
|
||||
}
|
||||
if (dstPm != null) PersistentBufferSync.mirrorPersistentCopy(srcPm, readOffset, dstPm, writeOffset, size);
|
||||
return true;
|
||||
} finally { rLock.unlock(); }
|
||||
}
|
||||
|
||||
public boolean enqueuePersistentCopyDeferred(int srcGlId, int dstGlId, long seq,
|
||||
public boolean enqueuePersistentCopyDeferred(int srcGlId, int dstGlId, long seq, long readOffset, long size,
|
||||
BiConsumer<PersistentMapping, PersistentMapping> inLock) {
|
||||
rLock.lock();
|
||||
try {
|
||||
final PersistentMapping srcPm = persistentMappings.get(srcGlId);
|
||||
if (srcPm == null) return false;
|
||||
if (srcPm == null || !srcPm.covers(readOffset, size)) return false;
|
||||
final PersistentMapping dstPm = persistentMappings.get(dstGlId);
|
||||
srcPm.lastEnqueuedSeq = seq;
|
||||
inLock.accept(srcPm, dstPm);
|
||||
@@ -1192,7 +1279,7 @@ public final class ResourceManager {
|
||||
public void mirrorArrayShadowFull(int glId, ByteBuffer data) {
|
||||
final int len = data.remaining();
|
||||
final ByteBuffer shadow = getOrAllocArrayShadow(glId, len);
|
||||
copyShadowRegion(data, data.position(), shadow, 0, len);
|
||||
ByteRegionCopy.copyByteRegion(data, data.position(), shadow, 0, len);
|
||||
shadow.position(0).limit(len);
|
||||
}
|
||||
|
||||
@@ -1206,7 +1293,7 @@ public final class ResourceManager {
|
||||
removeArrayShadowLocked(glId);
|
||||
return;
|
||||
}
|
||||
copyShadowRegion(data, data.position(), shadow, dstOffset, len);
|
||||
ByteRegionCopy.copyByteRegion(data, data.position(), shadow, dstOffset, len);
|
||||
} finally { wLock.unlock(); }
|
||||
}
|
||||
|
||||
@@ -1226,18 +1313,6 @@ public final class ResourceManager {
|
||||
arrayShadowWanted.remove(glId);
|
||||
}
|
||||
|
||||
private static void copyShadowRegion(ByteBuffer src, int srcOff, ByteBuffer dst, int dstOff, int len) {
|
||||
if (src.isDirect() && dst.isDirect()) {
|
||||
MemoryUtil.memCopy(MemoryUtil.memAddress(src) + srcOff, MemoryUtil.memAddress(dst) + dstOff, len);
|
||||
return;
|
||||
}
|
||||
final ByteBuffer s = src.duplicate();
|
||||
s.position(srcOff).limit(srcOff + len);
|
||||
final ByteBuffer d = dst.duplicate();
|
||||
d.position(dstOff);
|
||||
d.put(s);
|
||||
}
|
||||
|
||||
public EBOSplitScanner.EboSplit[] getOrScanSplits(int glId, int indexType, int sentinel) {
|
||||
if (!hasEboShadow(glId)) return null;
|
||||
final long version = getEboShadowVersion(glId);
|
||||
@@ -1502,10 +1577,10 @@ public final class ResourceManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static final int COPY_CALLSITE_UPLOAD_BUFFER = 1;
|
||||
public static final int COPY_CALLSITE_UPLOAD_TEX_BATCH = 2;
|
||||
public static final int COPY_CALLSITE_UPLOAD_TEX_DIRECT = 3;
|
||||
public static final int COPY_CALLSITE_ARENA = 4;
|
||||
public static final int COPY_CALLSITE_FILL_BUFFER = 5;
|
||||
|
||||
public boolean arenaUpload(ByteBuffer data, long dstHandle, long dstOffset, boolean cycle) {
|
||||
final FrameManager.FrameState f = frameManager.frame();
|
||||
@@ -1600,7 +1675,13 @@ public final class ResourceManager {
|
||||
}
|
||||
|
||||
public void uploadToBuffer(long copyPass, ByteBuffer data, long gpuBuffer, long offset, boolean cycle) {
|
||||
final long size = data.remaining();
|
||||
uploadRangeToBuffer(copyPass, data, data.position(), data.remaining(), gpuBuffer, offset, cycle);
|
||||
}
|
||||
|
||||
public void uploadRangeToBuffer(long copyPass, ByteBuffer src, long srcOff, long size, long gpuBuffer, long dstOff, boolean cycle) {
|
||||
if (srcOff < 0 || size < 0 || srcOff + size > src.capacity()) {
|
||||
throw new IllegalStateException("uploadRangeToBuffer: bad source range srcOff=" + srcOff + " size=" + size + " src.capacity=" + src.capacity());
|
||||
}
|
||||
final long xfer = acquireTransferBuffer(size);
|
||||
if (xfer == 0) return;
|
||||
|
||||
@@ -1609,28 +1690,102 @@ public final class ResourceManager {
|
||||
if (mapped == null || mapped.capacity() < size) {
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
returnTransferBuffer(xfer, size);
|
||||
throw new IllegalStateException("uploadToBuffer: bad mapping size=" + size + " mapSize=" + mapSize + " mapped=" + (mapped == null ? "null" : "cap=" + mapped.capacity()));
|
||||
throw new IllegalStateException("uploadRangeToBuffer: bad mapping size=" + size + " mapSize=" + mapSize + " mapped=" + (mapped == null ? "null" : "cap=" + mapped.capacity()));
|
||||
}
|
||||
if (src.isDirect()) {
|
||||
MemoryUtil.memCopy(MemoryUtil.memAddress0(src) + srcOff, MemoryUtil.memAddress0(mapped), size);
|
||||
} else {
|
||||
mapped.put(0, src, (int) srcOff, (int) size);
|
||||
}
|
||||
copyMappedFromData(mapped, data, (int) size, COPY_CALLSITE_UPLOAD_BUFFER);
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
|
||||
try (var stack = stackPush()) {
|
||||
final SDL_GPUTransferBufferLocation src = SDL_GPUTransferBufferLocation.calloc(stack)
|
||||
final SDL_GPUTransferBufferLocation srcLoc = SDL_GPUTransferBufferLocation.calloc(stack)
|
||||
.transfer_buffer(xfer)
|
||||
.offset(0);
|
||||
|
||||
final SDL_GPUBufferRegion dst = SDL_GPUBufferRegion.calloc(stack)
|
||||
.buffer(gpuBuffer)
|
||||
.offset((int) offset)
|
||||
.offset((int) dstOff)
|
||||
.size((int) size);
|
||||
|
||||
SDL_UploadToGPUBuffer(copyPass, src, dst, cycle);
|
||||
SDL_UploadToGPUBuffer(copyPass, srcLoc, dst, cycle);
|
||||
}
|
||||
|
||||
frameManager.recordUploadCommands(size, 1);
|
||||
returnTransferBuffer(xfer, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills {@code size} bytes of a buffer by staging {@code pattern} once and uploading it repeatedly. Staging every
|
||||
* byte instead maps a transfer buffer per chunk, which stalls for seconds on the hundreds of megabytes some shader
|
||||
* packs allocate as storage buffers.
|
||||
*/
|
||||
public void fillBuffer(long copyPass, ByteBuffer pattern, long gpuBuffer, long offset, long size) {
|
||||
final int chunk = pattern.remaining();
|
||||
if (chunk <= 0 || size <= 0) return;
|
||||
final long xfer = acquireTransferBuffer(chunk);
|
||||
if (xfer == 0) return;
|
||||
try {
|
||||
final ByteBuffer mapped = SDL_MapGPUTransferBuffer(device.getDevice(), xfer, true, getTransferBufferMapSize(chunk));
|
||||
if (mapped == null || mapped.capacity() < chunk) {
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
throw new IllegalStateException("fillBuffer: bad mapping size=" + chunk + " mapped=" + (mapped == null ? "null" : "cap=" + mapped.capacity()));
|
||||
}
|
||||
copyMappedFromData(mapped, pattern, chunk, COPY_CALLSITE_FILL_BUFFER);
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
|
||||
int uploads = 0;
|
||||
try (var stack = stackPush()) {
|
||||
final SDL_GPUTransferBufferLocation src = SDL_GPUTransferBufferLocation.calloc(stack).transfer_buffer(xfer).offset(0);
|
||||
final SDL_GPUBufferRegion dst = SDL_GPUBufferRegion.calloc(stack).buffer(gpuBuffer);
|
||||
for (long done = 0; done < size; done += chunk) {
|
||||
dst.offset((int) (offset + done)).size((int) Math.min(chunk, size - done));
|
||||
SDL_UploadToGPUBuffer(copyPass, src, dst, false);
|
||||
uploads++;
|
||||
}
|
||||
}
|
||||
frameManager.recordUploadCommands(size, uploads);
|
||||
} finally {
|
||||
returnTransferBuffer(xfer, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean zeroTexture3D(long copyPass, long gpuTexture, int w, int h, int d, int level, int bytesPerTexel) {
|
||||
final long sliceBytes = (long) w * h * bytesPerTexel;
|
||||
if (sliceBytes <= 0 || sliceBytes > Integer.MAX_VALUE) return false;
|
||||
final long xfer = acquireTransferBuffer(sliceBytes);
|
||||
if (xfer == 0) return false;
|
||||
try {
|
||||
final ByteBuffer mapped = SDL_MapGPUTransferBuffer(device.getDevice(), xfer, true, getTransferBufferMapSize(sliceBytes));
|
||||
if (mapped == null || mapped.capacity() < sliceBytes) {
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
return false;
|
||||
}
|
||||
MemoryUtil.memSet(MemoryUtil.memAddress(mapped), 0, sliceBytes);
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
|
||||
flushBatchedUploads(copyPass);
|
||||
try (var stack = stackPush()) {
|
||||
final SDL_GPUTextureTransferInfo src = SDL_GPUTextureTransferInfo.calloc(stack).transfer_buffer(xfer).offset(0);
|
||||
final SDL_GPUTextureRegion dst = SDL_GPUTextureRegion.calloc(stack)
|
||||
.texture(gpuTexture)
|
||||
.mip_level(level)
|
||||
.x(0).y(0)
|
||||
.w(w).h(h).d(1);
|
||||
for (int z = 0; z < d; z++) {
|
||||
dst.z(z);
|
||||
SDL_UploadToGPUTexture(copyPass, src, dst, false);
|
||||
}
|
||||
}
|
||||
markTextureContentDefined(gpuTexture);
|
||||
frameManager.recordUploadCommands(sliceBytes * d, d);
|
||||
return true;
|
||||
} finally {
|
||||
returnTransferBuffer(xfer, sliceBytes);
|
||||
}
|
||||
}
|
||||
|
||||
public static final int BATCH_SEGMENT_CAPACITY = 4 * 1024 * 1024;
|
||||
|
||||
public void uploadToTexture(long copyPass, ByteBuffer data, long gpuTexture, int x, int y, int w, int h, int level) {
|
||||
@@ -2506,12 +2661,20 @@ public final class ResourceManager {
|
||||
arrayShadowWanted.clear();
|
||||
for (ByteBuffer bb : pboStagingData.values()) memFree(bb);
|
||||
pboStagingData.clear();
|
||||
for (PersistentMapping pm : persistentMappings.values()) {
|
||||
for (var e : persistentMappings.int2ObjectEntrySet()) {
|
||||
final PersistentMapping pm = e.getValue();
|
||||
untrackPersistentDirty(pm);
|
||||
releasePersistentStaging(pm);
|
||||
releasePersistentStaging(pm, e.getIntKey(), "shutdown");
|
||||
}
|
||||
persistentMappings.clear();
|
||||
mappingsVersion++;
|
||||
for (MappedRange m : plainMappings.values()) {
|
||||
if (m.staging != null) memFree(m.staging);
|
||||
m.staging = null;
|
||||
}
|
||||
plainMappings.clear();
|
||||
Arrays.fill(plainMappingPool, null);
|
||||
plainMappingPoolCount = 0;
|
||||
}
|
||||
|
||||
public record TextureMeta(int glTarget, int glFormat, int sdlFormat, int width, int height, int depth, int levels, int usage) {}
|
||||
|
||||
+9
-5
@@ -329,11 +329,15 @@ public final class TransferThread {
|
||||
final ByteBuffer mapped = SDL_MapGPUTransferBuffer(device.getDevice(), xfer, true, mapSize);
|
||||
if (mapped != null) {
|
||||
final ByteBuffer src = sr.stagingBuffer;
|
||||
final int prevPos = src.position();
|
||||
final int prevLim = src.limit();
|
||||
src.limit((int) (sr.srcOffset + size)).position((int) sr.srcOffset);
|
||||
mapped.put(src);
|
||||
src.limit(prevLim).position(prevPos);
|
||||
if (sr.srcOffset < 0 || sr.srcOffset + size > src.capacity() || size > mapped.capacity()) {
|
||||
LOG.error("StagingReadUpload out of bounds: srcOffset={} size={} src.capacity={} mapped.capacity={}; upload dropped", sr.srcOffset, size, src.capacity(), mapped.capacity());
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
resourceManager.returnTransferBufferThreadSafe(xfer, size);
|
||||
retireSeq(sr.seq);
|
||||
StagingReadUpload.release(sr);
|
||||
return;
|
||||
}
|
||||
MemoryUtil.memCopy(MemoryUtil.memAddress0(src) + sr.srcOffset, MemoryUtil.memAddress0(mapped), size);
|
||||
}
|
||||
SDL_UnmapGPUTransferBuffer(device.getDevice(), xfer);
|
||||
recordBufferUpload(xfer, size, sr.dstGpuBuffer, sr.dstOffset, sr.seq, sr.cycle);
|
||||
|
||||
@@ -43,8 +43,13 @@ public final class SamplerBinder {
|
||||
}
|
||||
|
||||
public long getSamplerForUnit(ContextState st, int glUnit, int glTexId) {
|
||||
if (glUnit >= 0 && glUnit < st.boundSamplerObjects.length && st.boundSamplerObjects[glUnit] != 0) {
|
||||
final TextureSamplerState ss = resourceManager.getSamplerObject(st.boundSamplerObjects[glUnit]);
|
||||
final int samplerObject = glUnit >= 0 && glUnit < st.boundSamplerObjects.length ? st.boundSamplerObjects[glUnit] : 0;
|
||||
return getSampler(samplerObject, glTexId);
|
||||
}
|
||||
|
||||
public long getSampler(int samplerObject, int glTexId) {
|
||||
if (samplerObject != 0) {
|
||||
final TextureSamplerState ss = resourceManager.getSamplerObject(samplerObject);
|
||||
if (ss != null) return getOrCreateSdlSampler(ss);
|
||||
}
|
||||
if (glTexId != 0) return getSamplerForTexture(glTexId);
|
||||
|
||||
@@ -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<Edit> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+398
@@ -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<String> DERIVATIVES = Set.of(
|
||||
"dFdx", "dFdy", "fwidth",
|
||||
"dFdxFine", "dFdyFine", "fwidthFine",
|
||||
"dFdxCoarse", "dFdyCoarse", "fwidthCoarse");
|
||||
|
||||
private static final Map<String, Set<Integer>> 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<String> declared, Set<String> written, Set<String> called) {
|
||||
Effects() {
|
||||
this(new HashSet<>(), new HashSet<>(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
public static void collectEdits(ShaderIndex index, List<Edit> edits) {
|
||||
final List<DerivativeCall> 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<GLSLParser.Selection_statementContext, Effects> ifEffects = new HashMap<>();
|
||||
final Map<GLSLParser.Selection_statementContext, StringBuilder> declarations = new LinkedHashMap<>();
|
||||
final Map<String, String> 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<GLSLParser.Selection_statementContext, StringBuilder> 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<String, Set<Integer>> outParams = new HashMap<>();
|
||||
final Set<String> names = new HashSet<>();
|
||||
final Map<GLSLParser.Postfix_expressionContext, String> calleeNames = new HashMap<>();
|
||||
private final List<GLSLParser.Function_definitionContext> definitions = new ArrayList<>();
|
||||
private @Nullable Map<String, Set<String>> 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<GLSLParser.Parameter_declarationContext> 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<String> globalWrites(String function) {
|
||||
if (globalWrites == null) globalWrites = summarize();
|
||||
return globalWrites.getOrDefault(function, Set.of());
|
||||
}
|
||||
|
||||
private Map<String, Set<String>> summarize() {
|
||||
final Map<String, Set<String>> writes = new HashMap<>();
|
||||
final Map<String, Set<String>> 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<String, Set<String>> e : callees.entrySet()) {
|
||||
final Set<String> into = writes.get(e.getKey());
|
||||
for (String callee : e.getValue()) {
|
||||
final Set<String> 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<GLSLParser.Parameter_declarationContext> 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<GLSLParser.Selection_statementContext, Effects> ifEffects) {
|
||||
final List<GLSLParser.Selection_statementContext> 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<String> 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<String> 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<Integer> positions = functions.outParams.get(callee);
|
||||
if (positions == null) positions = BUILTIN_OUT_PARAMS.get(callee);
|
||||
if (positions != null && p.function_call_parameters() != null) {
|
||||
final List<GLSLParser.Assignment_expressionContext> 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<Edit> edits) {
|
||||
final List<Edit> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<GLSLParser.Typeless_declarationContext> 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);
|
||||
|
||||
+13
-45
@@ -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<Edit> out) {
|
||||
public static void collectEdits(ShaderIndex index, PerFrameUniformBlock perFrame, PerFrameUniformBlock perPass, List<Edit> 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<String, String> memberTypes = new LinkedHashMap<>();
|
||||
@@ -51,9 +47,12 @@ public final class PerFrameBlockInjector {
|
||||
final List<Edit> stripEdits = new ArrayList<>();
|
||||
final IntArrayList stripBlocks = new IntArrayList();
|
||||
final Set<String> shadowed = new HashSet<>();
|
||||
classifyDeclarations(root, memberTypes, memberBlock, stripEdits, stripBlocks, shadowed);
|
||||
classifyDeclarations(index, memberTypes, memberBlock, stripEdits, stripBlocks, shadowed);
|
||||
|
||||
final Set<String> referenced = referencedMembers(root, memberTypes.keySet(), shadowed);
|
||||
final Set<String> 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<String, String> memberTypes, Object2IntOpenHashMap<String> memberBlock, List<Edit> stripEdits, IntArrayList stripBlocks, Set<String> 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<String, String> memberTypes, Object2IntOpenHashMap<String> memberBlock, List<Edit> stripEdits, IntArrayList stripBlocks, Set<String> shadowed) {
|
||||
for (GLSLParser.DeclarationContext ctx : index.globalDeclarations()) {
|
||||
final List<String> 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<String> referencedMembers(GLSLParser.Translation_unitContext root, Set<String> memberNames, Set<String> shadowed) {
|
||||
final Set<String> 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<String> 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<String> 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;
|
||||
|
||||
+54
-70
@@ -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<Edit> edits) {
|
||||
record SamplerDecl(String name, int start, int stop) {}
|
||||
private record SamplerDecl(List<String> names, int start, int stop) {}
|
||||
|
||||
public static void collectEdits(ShaderIndex index, List<Edit> edits) {
|
||||
final List<SamplerDecl> 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<String, Integer> 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<String> directiveRefs = GlslTransformUtils.identifiersInDirectiveText(source);
|
||||
|
||||
Set<String> 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<String> 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<SamplerDecl> 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<SamplerDecl> 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<String> 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");
|
||||
}
|
||||
|
||||
+150
-54
@@ -9,8 +9,10 @@ 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;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.device.Device;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.cross.CrossCompileCache;
|
||||
@@ -53,6 +55,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.IntConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.lwjgl.sdl.SDLGPU.*;
|
||||
import static org.lwjgl.system.MemoryStack.*;
|
||||
@@ -64,6 +67,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrays;
|
||||
import it.unimi.dsi.fastutil.ints.IntIterator;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||
@@ -234,7 +238,35 @@ public final class ShaderManager {
|
||||
final int shaderKind = shaderKindFor(obj);
|
||||
final int glType = obj.type;
|
||||
final String finalSrc = obj.source;
|
||||
obj.spirvFuture = AngelicaWorkers.submit(() -> compileStageAsync(finalSrc, shaderKind, glType, shader));
|
||||
final QueuedCompile compile = new QueuedCompile(() -> compileStageAsync(finalSrc, shaderKind, glType, shader));
|
||||
AngelicaWorkers.run(compile);
|
||||
obj.queuedCompile = compile;
|
||||
obj.spirvFuture = compile.result;
|
||||
}
|
||||
|
||||
private static final class QueuedCompile implements Runnable {
|
||||
private final AtomicBoolean claimed = new AtomicBoolean();
|
||||
private final Supplier<AsyncCompile> work;
|
||||
final CompletableFuture<AsyncCompile> result = new CompletableFuture<>();
|
||||
|
||||
QueuedCompile(Supplier<AsyncCompile> work) {
|
||||
this.work = work;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!claimed.compareAndSet(false, true)) return;
|
||||
try {
|
||||
result.complete(work.get());
|
||||
} catch (Throwable t) {
|
||||
result.completeExceptionally(t);
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops the compile if no thread has started it; false when it is running or done. */
|
||||
boolean cancel() {
|
||||
return claimed.compareAndSet(false, true);
|
||||
}
|
||||
}
|
||||
|
||||
private static AsyncCompile compileStageAsync(String src, int shaderKind, int glType, int shader) {
|
||||
@@ -285,40 +317,40 @@ public final class ShaderManager {
|
||||
LOG.warn("glsl-transformation-lib parse failed for '{}': {}", debugName, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
final List<Edit> 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<Edit> 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) {
|
||||
@@ -377,6 +409,10 @@ public final class ShaderManager {
|
||||
final SpirvCompiler.Result r;
|
||||
final boolean alreadyRemapped;
|
||||
if (obj.spirvFuture != null) {
|
||||
if (obj.queuedCompile != null) {
|
||||
obj.queuedCompile.run();
|
||||
obj.queuedCompile = null;
|
||||
}
|
||||
final AsyncCompile compiled = obj.spirvFuture.join();
|
||||
obj.spirvFuture = null;
|
||||
r = compiled.result();
|
||||
@@ -431,6 +467,9 @@ public final class ShaderManager {
|
||||
final CompletableFuture<AsyncCompile> future = obj.spirvFuture;
|
||||
if (future == null) return;
|
||||
obj.spirvFuture = null;
|
||||
final QueuedCompile queued = obj.queuedCompile;
|
||||
obj.queuedCompile = null;
|
||||
if (queued != null && queued.cancel()) return;
|
||||
future.thenAccept(ShaderManager::freeSpirvResult);
|
||||
}
|
||||
|
||||
@@ -567,7 +606,7 @@ public final class ShaderManager {
|
||||
registerExtraNames(prog, vs.reflection);
|
||||
registerExtraNames(prog, fs.reflection);
|
||||
|
||||
for (final String name : prog.allSamplerNames) ensureLocation(prog, name);
|
||||
registerSamplerLocations(prog, vs.reflection, fs.reflection);
|
||||
for (final String name : prog.allImageNames) ensureLocation(prog, name);
|
||||
|
||||
prog.buildUniformSlotArrays();
|
||||
@@ -611,7 +650,7 @@ public final class ShaderManager {
|
||||
|
||||
applyUboMembers(prog, refl, false);
|
||||
registerExtraNames(prog, refl);
|
||||
for (final String name : prog.allSamplerNames) ensureLocation(prog, name);
|
||||
registerSamplerLocations(prog, refl);
|
||||
for (final String name : prog.allImageNames) ensureLocation(prog, name);
|
||||
prog.buildUniformSlotArrays();
|
||||
|
||||
@@ -820,8 +859,52 @@ public final class ShaderManager {
|
||||
return built;
|
||||
}
|
||||
|
||||
private static ShaderDiskCache.Key variantKey(String vertexSource, List<UscaledRetype.Attrib> attribs) {
|
||||
final ShaderDiskCache.Key key = ShaderDiskCache.key("sdl-vertex-variant").str(SpirvCompiler.toolchainId()).str(CrossCompileUtil.spvcId())
|
||||
.str(vertexSource).i(attribs.size());
|
||||
for (UscaledRetype.Attrib a : attribs) {
|
||||
key.str(a.name()).i(a.location()).i(a.declVecSize()).i(a.boundVecSize()).i(a.signed() ? 1 : 0);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
private VertexVariant buildVertexVariant(int program, ProgramObject prog, List<UscaledRetype.Attrib> attribs) {
|
||||
final String preprocessed = SpirvCompiler.preprocess(prog.vertexSource, Shaderc.shaderc_vertex_shader, "variant" + program, SpirvCompiler.Options.vulkanForced460Core());
|
||||
final ShaderDiskCache.Key diskKey = ShaderDiskCache.isEnabled() ? variantKey(prog.vertexSource, attribs) : null;
|
||||
final ShaderStageSerializer.Stage cached = diskKey != null ? loadStage(diskKey) : null;
|
||||
final ShaderStageSerializer.Stage stage = cached != null ? cached : compileVertexVariant(program, prog.vertexSource, attribs, diskKey);
|
||||
if (stage == null) return null;
|
||||
|
||||
final StageReflection refl = stage.reflection();
|
||||
if (!sameVaryingLayout(prog.vertexReflection.vsOutputs(), refl.vsOutputs())) {
|
||||
LOG.error("Vertex attribute conversion: program {} variant changed VS output locations; the fragment stage is patched against the base layout. Falling back to normalized coercion.", program);
|
||||
return null;
|
||||
}
|
||||
final VertexVariant v = new VertexVariant();
|
||||
v.spirv = ShaderCacheIO.toNative(stage.spirv());
|
||||
|
||||
final Object2IntOpenHashMap<String> bindings = new Object2IntOpenHashMap<>(prog.attribLocationBindings);
|
||||
bindings.defaultReturnValue(-1);
|
||||
for (UscaledRetype.Attrib a : attribs) bindings.put(a.name() + UscaledRetype.SUFFIX, a.location());
|
||||
v.inputMask = patchAttribLocations(v.spirv, refl, bindings, v.inputVecSize, v.inputBaseType, null, null);
|
||||
if (v.inputMask != prog.vertexInputMask) {
|
||||
memFree(v.spirv);
|
||||
LOG.error("Vertex attribute conversion: program {} variant resolved inputs to 0x{} but the program binds 0x{}; a renamed input landed on the wrong location. Falling back to normalized coercion.",
|
||||
program, Integer.toHexString(v.inputMask), Integer.toHexString(prog.vertexInputMask));
|
||||
return null;
|
||||
}
|
||||
v.sdlShader = createSDLShader(v.spirv, SDL_GPU_SHADERSTAGE_VERTEX, prog.vertexResources.numSamplers(), prog.vertexResources.numUBOs(), prog.vertexResources.numStorageBuffers(), prog.vertexResources.numStorageTextures());
|
||||
if (v.sdlShader == 0) {
|
||||
memFree(v.spirv);
|
||||
LOG.error("Vertex attribute conversion: program {} variant shader creation failed: {}", program, SDLError.SDL_GetError());
|
||||
return null;
|
||||
}
|
||||
LOG.info("Vertex attribute conversion: program {} converted {}", program, attribs);
|
||||
if (SystemProperties.dumpShaders()) dumpVariantSource(program, stage.source());
|
||||
return v;
|
||||
}
|
||||
|
||||
private static ShaderStageSerializer.Stage compileVertexVariant(int program, String vertexSource, List<UscaledRetype.Attrib> attribs, ShaderDiskCache.Key diskKey) {
|
||||
final String preprocessed = SpirvCompiler.preprocess(vertexSource, Shaderc.shaderc_vertex_shader, "variant" + program, SpirvCompiler.Options.vulkanForced460Core());
|
||||
if (preprocessed == null) {
|
||||
LOG.warn("Vertex attribute conversion: program {} preprocessing failed; falling back to normalized coercion, values will be scaled", program);
|
||||
return null;
|
||||
@@ -839,32 +922,11 @@ public final class ShaderManager {
|
||||
try {
|
||||
remapSpirvForSDLGPU(r.spirv(), GL20.GL_VERTEX_SHADER);
|
||||
final StageReflection refl = reflectStage(r.spirv(), true);
|
||||
if (!sameVaryingLayout(prog.vertexReflection.vsOutputs(), refl.vsOutputs())) {
|
||||
LOG.error("Vertex attribute conversion: program {} variant changed VS output locations; the fragment stage is patched against the base layout. Falling back to normalized coercion.", program);
|
||||
return null;
|
||||
final byte[] heap = ShaderCacheIO.toHeap(r.spirv());
|
||||
if (diskKey != null && refl != StageReflection.EMPTY) {
|
||||
ShaderDiskCache.put(diskKey, ShaderStageSerializer.encode(heap, refl, GraphicsBindingMap.EMPTY, null, src));
|
||||
}
|
||||
final VertexVariant v = new VertexVariant();
|
||||
v.spirv = copyBuffer(r.spirv());
|
||||
|
||||
final Object2IntOpenHashMap<String> bindings = new Object2IntOpenHashMap<>(prog.attribLocationBindings);
|
||||
bindings.defaultReturnValue(-1);
|
||||
for (UscaledRetype.Attrib a : attribs) bindings.put(a.name() + UscaledRetype.SUFFIX, a.location());
|
||||
v.inputMask = patchAttribLocations(v.spirv, refl, bindings, v.inputVecSize, v.inputBaseType, null, null);
|
||||
if (v.inputMask != prog.vertexInputMask) {
|
||||
memFree(v.spirv);
|
||||
LOG.error("Vertex attribute conversion: program {} variant resolved inputs to 0x{} but the program binds 0x{}; a renamed input landed on the wrong location. Falling back to normalized coercion.",
|
||||
program, Integer.toHexString(v.inputMask), Integer.toHexString(prog.vertexInputMask));
|
||||
return null;
|
||||
}
|
||||
v.sdlShader = createSDLShader(v.spirv, SDL_GPU_SHADERSTAGE_VERTEX, prog.vertexResources.numSamplers(), prog.vertexResources.numUBOs(), prog.vertexResources.numStorageBuffers(), prog.vertexResources.numStorageTextures());
|
||||
if (v.sdlShader == 0) {
|
||||
memFree(v.spirv);
|
||||
LOG.error("Vertex attribute conversion: program {} variant shader creation failed: {}", program, SDLError.SDL_GetError());
|
||||
return null;
|
||||
}
|
||||
LOG.info("Vertex attribute conversion: program {} converted {}", program, attribs);
|
||||
if (SystemProperties.dumpShaders()) dumpVariantSource(program, src);
|
||||
return v;
|
||||
return new ShaderStageSerializer.Stage(heap, refl, GraphicsBindingMap.EMPTY, Set.of(), src);
|
||||
} finally {
|
||||
memFree(r.spirv());
|
||||
}
|
||||
@@ -1101,7 +1163,7 @@ public final class ShaderManager {
|
||||
return new CrossCompiled(SDL_GPU_SHADERFORMAT_SPIRV, spirv, "main", false);
|
||||
}
|
||||
if (device.supportsMsl()) {
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, glShaderType);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, glShaderType, device.metalTextureAtomics());
|
||||
return new CrossCompiled(SDL_GPU_SHADERFORMAT_MSL, out.code(), out.entrypoint(), true);
|
||||
}
|
||||
if (device.supportsDxbc()) {
|
||||
@@ -1476,6 +1538,7 @@ public final class ShaderManager {
|
||||
public boolean deletePending;
|
||||
public int attachCount;
|
||||
public CompletableFuture<AsyncCompile> spirvFuture;
|
||||
QueuedCompile queuedCompile;
|
||||
public StageReflection reflection = StageReflection.EMPTY;
|
||||
public GraphicsBindingMap graphicsBindingMap = GraphicsBindingMap.EMPTY;
|
||||
public Set<String> boolUniforms = Set.of();
|
||||
@@ -1520,6 +1583,7 @@ public final class ShaderManager {
|
||||
public record StageReflection(
|
||||
ResourceCounts counts,
|
||||
List<String> samplerNames, // SAMPLED_IMAGE, sorted by binding (post-remap)
|
||||
List<String> unusedSamplerNames, // declared samplers the entry point never reads
|
||||
List<String> extraUniformNames, // SEPARATE_SAMPLERS - registered in nameToLocation only
|
||||
List<String> storageImageNames, // SEPARATE_IMAGE + STORAGE_IMAGE (RO + RW), sorted by binding (post-remap)
|
||||
int uboSize, // binding-0 UBO total size in bytes; 0 if absent
|
||||
@@ -1535,7 +1599,7 @@ public final class ShaderManager {
|
||||
BlockReflection[] blocks
|
||||
) {
|
||||
public static final StageReflection EMPTY = new StageReflection(
|
||||
ResourceCounts.EMPTY, List.of(), List.of(), List.of(), 0, List.of(), List.of(), List.of(), List.of(),
|
||||
ResourceCounts.EMPTY, List.of(), List.of(), List.of(), List.of(), 0, List.of(), List.of(), List.of(), List.of(),
|
||||
-1, 0, 0, 0, 0, BlockReflection.emptyBlocks());
|
||||
}
|
||||
|
||||
@@ -1739,6 +1803,7 @@ public final class ShaderManager {
|
||||
return new StageReflection(
|
||||
new ResourceCounts(samplerNames.size(), numUBOs, numStorageBuffers, numStorageTextures),
|
||||
samplerNames,
|
||||
unusedSamplerNames(compiler, resources, stack),
|
||||
extraNames,
|
||||
storageImageNames,
|
||||
uboSize,
|
||||
@@ -1822,6 +1887,36 @@ public final class ShaderManager {
|
||||
});
|
||||
}
|
||||
|
||||
private static List<String> unusedSamplerNames(long compiler, long resources, MemoryStack stack) {
|
||||
final PointerBuffer pActive = stack.mallocPointer(1);
|
||||
final PointerBuffer pActiveResources = stack.mallocPointer(1);
|
||||
if (Spvc.spvc_compiler_get_active_interface_variables(compiler, pActive) != Spvc.SPVC_SUCCESS
|
||||
|| Spvc.spvc_compiler_create_shader_resources_for_active_variables(compiler, pActiveResources, pActive.get(0)) != Spvc.SPVC_SUCCESS) {
|
||||
return List.of();
|
||||
}
|
||||
final IntOpenHashSet active = new IntOpenHashSet();
|
||||
forEachResource(pActiveResources.get(0), Spvc.SPVC_RESOURCE_TYPE_SAMPLED_IMAGE, stack, (i, r) -> active.add(r.id()));
|
||||
final List<String> unused = new ArrayList<>();
|
||||
forEachResource(resources, Spvc.SPVC_RESOURCE_TYPE_SAMPLED_IMAGE, stack, (i, r) -> {
|
||||
if (active.contains(r.id())) return;
|
||||
final String name = Spvc.spvc_compiler_get_name(compiler, r.id());
|
||||
if (name != null && !name.isEmpty()) unused.add(name);
|
||||
});
|
||||
return unused;
|
||||
}
|
||||
|
||||
private static void registerSamplerLocations(ProgramObject prog, StageReflection... stages) {
|
||||
final Set<String> read = new HashSet<>();
|
||||
for (StageReflection stage : stages) {
|
||||
for (String name : stage.samplerNames()) {
|
||||
if (!stage.unusedSamplerNames().contains(name)) read.add(name);
|
||||
}
|
||||
}
|
||||
for (String name : prog.allSamplerNames) {
|
||||
if (read.contains(name)) ensureLocation(prog, name);
|
||||
}
|
||||
}
|
||||
|
||||
private static void collectResourceNames(long resources, long compiler, int resourceType, List<String> out, MemoryStack stack) {
|
||||
forEachResource(resources, resourceType, stack, (i, r) -> {
|
||||
final String name = r.nameString();
|
||||
@@ -1985,6 +2080,7 @@ public final class ShaderManager {
|
||||
public int vertexShader;
|
||||
public int vertexUboSize;
|
||||
public long sdlComputePipeline;
|
||||
public VertexWriteReplayer.Replay vertexWriteReplay;
|
||||
public long sdlFragmentShader;
|
||||
public long sdlVertexShader;
|
||||
public long lastComputeCb;
|
||||
|
||||
+3
-1
@@ -102,6 +102,7 @@ final class ShaderStageSerializer {
|
||||
out.writeInt(r.counts().numStorageBuffers());
|
||||
out.writeInt(r.counts().numStorageTextures());
|
||||
writeStrList(out, r.samplerNames());
|
||||
writeStrList(out, r.unusedSamplerNames());
|
||||
writeStrList(out, r.extraUniformNames());
|
||||
writeStrList(out, r.storageImageNames());
|
||||
out.writeInt(r.uboSize());
|
||||
@@ -139,6 +140,7 @@ final class ShaderStageSerializer {
|
||||
private static StageReflection readReflection(ByteBuffer in) {
|
||||
final ResourceCounts counts = new ResourceCounts(in.getInt(), in.getInt(), in.getInt(), in.getInt());
|
||||
final List<String> samplerNames = readStrList(in);
|
||||
final List<String> unusedSamplerNames = readStrList(in);
|
||||
final List<String> extraNames = readStrList(in);
|
||||
final List<String> storageImageNames = readStrList(in);
|
||||
final int uboSize = in.getInt();
|
||||
@@ -156,7 +158,7 @@ final class ShaderStageSerializer {
|
||||
for (int b = 0; b < blockCount; b++) {
|
||||
blocks[b] = new BlockReflection(in.getInt(), in.getInt(), readList(in, ShaderStageSerializer::readMember), in.get() != 0);
|
||||
}
|
||||
return new StageReflection(counts, samplerNames, extraNames, storageImageNames, uboSize, uboMembers, vsInputs, vsOutputs, fsInputs,
|
||||
return new StageReflection(counts, samplerNames, unusedSamplerNames, extraNames, storageImageNames, uboSize, uboMembers, vsInputs, vsOutputs, fsInputs,
|
||||
maxOutputLocation, roBuf, rwBuf, roTex, rwTex, blocks);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
+17
-6
@@ -150,18 +150,30 @@ public final class CrossCompileUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean usesImageAtomics(ByteBuffer spirv) {
|
||||
final IntBuffer w = spirv.asIntBuffer();
|
||||
for (int i = SPIRV_HEADER_WORDS; i < w.limit(); i = nextInstruction(w, i)) {
|
||||
if ((w.get(i) & 0xFFFF) == Spv.SpvOpImageTexelPointer) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final int SPIRV_HEADER_WORDS = 5; // magic, version, generator, bound, schema
|
||||
|
||||
private static int nextInstruction(IntBuffer w, int i) {
|
||||
final int len = w.get(i) >>> 16;
|
||||
return len == 0 ? w.limit() : i + len;
|
||||
}
|
||||
|
||||
public static int[] parseLocalSize(ByteBuffer spirv) {
|
||||
final IntBuffer w = spirv.asIntBuffer();
|
||||
final int wordCount = w.limit();
|
||||
int i = 5; // 5-word SPIR-V header (magic, version, generator, bound, schema)
|
||||
int lx = 1, ly = 1, lz = 1;
|
||||
boolean foundLiteral = false;
|
||||
boolean sawExecutionModeId = false;
|
||||
while (i < wordCount) {
|
||||
for (int i = SPIRV_HEADER_WORDS; i < w.limit(); i = nextInstruction(w, i)) {
|
||||
final int word0 = w.get(i);
|
||||
final int opcode = word0 & 0xFFFF;
|
||||
final int instrLen = (word0 >>> 16) & 0xFFFF;
|
||||
if (instrLen == 0) break;
|
||||
final int instrLen = word0 >>> 16;
|
||||
if (opcode == 16 && instrLen >= 6) { // SpvOpExecutionMode
|
||||
final int mode = w.get(i + 2);
|
||||
if (mode == 17) { // SpvExecutionModeLocalSize
|
||||
@@ -173,7 +185,6 @@ public final class CrossCompileUtil {
|
||||
} else if (opcode == 331) { // SpvOpExecutionModeId
|
||||
sawExecutionModeId = true;
|
||||
}
|
||||
i += instrLen;
|
||||
}
|
||||
if (!foundLiteral && sawExecutionModeId) {
|
||||
LOG.warn("SPIR-V uses OpExecutionModeId (spec-constant LocalSizeId); falling back to {{1,1,1}} - not supported by parseLocalSize");
|
||||
|
||||
+10
-5
@@ -26,7 +26,11 @@ public final class MslCrossCompile {
|
||||
private static final Logger LOG = LogManager.getLogger("Angelica-SDLGPU");
|
||||
private static final String BACKEND = "MSL";
|
||||
|
||||
private static final CrossCompileCache CACHE = new CrossCompileCache("msl", CrossCompileUtil::spvcId, MslCrossCompile::crossCompileUncached);
|
||||
private static final int SPVC_MSL_2_2 = mslVersion(2, 2, 0);
|
||||
public static final int SPVC_MSL_3_1 = mslVersion(3, 1, 0);
|
||||
|
||||
private static final CrossCompileCache CACHE = new CrossCompileCache("msl", CrossCompileUtil::spvcId, (spirv, glShaderType) -> compileAtVersion(spirv, glShaderType, SPVC_MSL_2_2));
|
||||
private static final CrossCompileCache CACHE_3_1 = new CrossCompileCache("msl31", CrossCompileUtil::spvcId, (spirv, glShaderType) -> compileAtVersion(spirv, glShaderType, SPVC_MSL_3_1));
|
||||
|
||||
@FunctionalInterface
|
||||
private interface BufferSlotFn { public int slot(int set, int binding); }
|
||||
@@ -36,8 +40,9 @@ public final class MslCrossCompile {
|
||||
|
||||
private MslCrossCompile() {}
|
||||
|
||||
public static Output compile(ByteBuffer spirv, int glShaderType) {
|
||||
return CACHE.compile(spirv, glShaderType);
|
||||
public static Output compile(ByteBuffer spirv, int glShaderType, boolean textureAtomics) {
|
||||
final boolean atomics = textureAtomics && glShaderType == GL43.GL_COMPUTE_SHADER && CrossCompileUtil.usesImageAtomics(spirv);
|
||||
return (atomics ? CACHE_3_1 : CACHE).compile(spirv, glShaderType);
|
||||
}
|
||||
|
||||
private static int executionModelFor(int glShaderType) {
|
||||
@@ -47,7 +52,7 @@ public final class MslCrossCompile {
|
||||
throw new UnsupportedOperationException("Unsupported shader type for MSL cross-compile: 0x" + Integer.toHexString(glShaderType));
|
||||
}
|
||||
|
||||
private static Output crossCompileUncached(ByteBuffer spirv, int glShaderType) {
|
||||
public static Output compileAtVersion(ByteBuffer spirv, int glShaderType, int version) {
|
||||
final int executionModel = executionModelFor(glShaderType);
|
||||
final int dumpId = SystemProperties.dumpShaders() ? CrossCompileUtil.SHADER_DUMP_COUNTER.getAndIncrement() : -1;
|
||||
if (dumpId >= 0) CrossCompileUtil.dumpSpirv(spirv, dumpId, glShaderType);
|
||||
@@ -77,7 +82,7 @@ public final class MslCrossCompile {
|
||||
throw CrossCompileUtil.spvcError(ctx, BACKEND, "create_compiler_options");
|
||||
}
|
||||
final long opts = pOpts.get(0);
|
||||
Spvc.spvc_compiler_options_set_uint(opts, Spvc.SPVC_COMPILER_OPTION_MSL_VERSION, mslVersion(2, 2, 0));
|
||||
Spvc.spvc_compiler_options_set_uint(opts, Spvc.SPVC_COMPILER_OPTION_MSL_VERSION, version);
|
||||
Spvc.spvc_compiler_options_set_uint(opts, Spvc.SPVC_COMPILER_OPTION_MSL_PLATFORM, Spvc.SPVC_MSL_PLATFORM_MACOS);
|
||||
if (Spvc.spvc_compiler_install_compiler_options(compiler, opts) != Spvc.SPVC_SUCCESS) {
|
||||
throw CrossCompileUtil.spvcError(ctx, BACKEND, "install_compiler_options");
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#version 460 core
|
||||
layout(local_size_x = 64) in;
|
||||
layout(binding = 0, r32i) uniform iimage2D counter;
|
||||
|
||||
void main() {
|
||||
imageAtomicAdd(counter, ivec2(0), 1);
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.compute;
|
||||
|
||||
import com.gtnewhorizons.angelica.sdlgpu.SdlTestRig;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.lwjgl.sdl.SDLGPU.SDL_GetGPUDeviceDriver;
|
||||
|
||||
class ImageAtomicsGpuTest {
|
||||
|
||||
private static SdlTestRig rig;
|
||||
private static ImageAtomicsProbe probe;
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() throws Exception {
|
||||
rig = SdlTestRig.acquireRealDevice();
|
||||
assumeTrue("metal".equalsIgnoreCase(SDL_GetGPUDeviceDriver(rig.sdlHandle)), "native Metal only");
|
||||
assumeTrue(ImageAtomicsProbe.defaultMslVersion() >= ImageAtomicsProbe.MTL_LANGUAGE_VERSION_3_1, "this process compiles Metal source below MSL 3.1");
|
||||
probe = new ImageAtomicsProbe(rig.sdlHandle);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void tearDown() {
|
||||
if (probe != null) probe.release();
|
||||
SdlTestRig.releaseRealDevice();
|
||||
}
|
||||
|
||||
@Test
|
||||
void atomicsCountEveryInvocationOnAnImageTheSizeOfEuphoriasEndCrystalMap() {
|
||||
assertTrue(probe.run(40, 10));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -77,9 +77,9 @@ class VoxelizationDispatcherTest {
|
||||
void dispatchRegion_splitsAboveMaxGroups() {
|
||||
final RecordingSink sink = new RecordingSink();
|
||||
final VoxelizationDispatcher d = new VoxelizationDispatcher(sink);
|
||||
region(d, 1, VoxelizationDispatcher.MAX_GROUPS_PER_DISPATCH * 64 + 1, new ContextState());
|
||||
region(d, 1, InvocationDispatch.MAX_GROUPS_PER_DISPATCH * 64 + 1, new ContextState());
|
||||
assertEquals(2, sink.dispatches.size());
|
||||
assertEquals(VoxelizationDispatcher.MAX_GROUPS_PER_DISPATCH, sink.dispatches.get(0)[0]);
|
||||
assertEquals(InvocationDispatch.MAX_GROUPS_PER_DISPATCH, sink.dispatches.get(0)[0]);
|
||||
assertEquals(1, sink.dispatches.get(1)[0]);
|
||||
assertEquals(2, sink.uniformPushCount);
|
||||
}
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.device;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.testutil.Reflect;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.SdlTestRig;
|
||||
import it.unimi.dsi.fastutil.longs.Long2BooleanOpenHashMap;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
class DeviceImageAtomicsGpuTest {
|
||||
|
||||
private Device device;
|
||||
|
||||
@BeforeAll
|
||||
static void initSdl() {
|
||||
SdlTestRig.initSdlVideo();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (device != null) device.destroyDevice();
|
||||
}
|
||||
|
||||
private Long2BooleanOpenHashMap verdicts() {
|
||||
return Reflect.get(device, "imageAtomicsBySize");
|
||||
}
|
||||
|
||||
@Test
|
||||
void verdictsAreCachedPerSizeAndDroppedWithTheDevice() {
|
||||
device = new Device();
|
||||
assumeTrue(device.createDevice(), "no SDL GPU device on this machine");
|
||||
assumeTrue(device.metalTextureAtomics(), "needs native Metal compiling at MSL 3.1 or newer");
|
||||
|
||||
assertFalse(device.supportsImageAtomics(40, 10, 2));
|
||||
assertFalse(device.supportsImageAtomics(0, 10, 1));
|
||||
assertFalse(device.supportsImageAtomics(40, -1, 1));
|
||||
assertEquals(0, verdicts().size());
|
||||
|
||||
assertNull(Reflect.get(device, "imageAtomicsProbe"));
|
||||
|
||||
assertTrue(device.supportsImageAtomics(40, 10, 1));
|
||||
assertFalse(device.supportsImageAtomics(64, 64, 1));
|
||||
assertEquals(2, verdicts().size());
|
||||
final Object probe = Reflect.get(device, "imageAtomicsProbe");
|
||||
assertTrue(device.supportsImageAtomics(40, 10, 1));
|
||||
assertFalse(device.supportsImageAtomics(64, 64, 1));
|
||||
assertEquals(2, verdicts().size());
|
||||
assertEquals(probe, Reflect.get(device, "imageAtomicsProbe"));
|
||||
|
||||
device.destroyDevice();
|
||||
assertFalse(device.metalTextureAtomics());
|
||||
assertFalse(device.supportsImageAtomics(40, 10, 1));
|
||||
assertEquals(0, verdicts().size());
|
||||
assertNull(Reflect.get(device, "imageAtomicsProbe"));
|
||||
}
|
||||
}
|
||||
+393
@@ -0,0 +1,393 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.glsm;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.opengl.GL31;
|
||||
import org.lwjgl.opengl.GL43;
|
||||
import org.lwjgl.opengl.GL44;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
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.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@Tag("glsm-sdl")
|
||||
@ExtendWith(GlsmSdlFrameExtension.class)
|
||||
class GlsmSdlBufferMappingLifetimeTest {
|
||||
|
||||
private static final int SIZE = 4096;
|
||||
private static final int PERSISTENT_STORAGE = GL44.GL_MAP_PERSISTENT_BIT | GL30.GL_MAP_WRITE_BIT | GL44.GL_MAP_COHERENT_BIT | GL44.GL_CLIENT_STORAGE_BIT;
|
||||
private static final int PERSISTENT_MAP = GL44.GL_MAP_PERSISTENT_BIT | GL30.GL_MAP_WRITE_BIT | GL44.GL_MAP_COHERENT_BIT;
|
||||
private static final int PLAIN_MAP = GL30.GL_MAP_WRITE_BIT | GL30.GL_MAP_INVALIDATE_BUFFER_BIT;
|
||||
|
||||
@BeforeAll
|
||||
static void boot() {
|
||||
GlsmSdlHeadlessRig.boot();
|
||||
}
|
||||
|
||||
private static void fill(ByteBuffer buf, int seed) {
|
||||
for (int i = 0; i < buf.capacity(); i++) buf.put(i, (byte) (seed * 37 + i * 7 + 1));
|
||||
}
|
||||
|
||||
private static void assertPattern(ByteBuffer actual, int seed) {
|
||||
for (int i = 0; i < actual.capacity(); i++) {
|
||||
assertEquals((byte) (seed * 37 + i * 7 + 1), actual.get(i), "byte " + i + " of pattern " + seed);
|
||||
}
|
||||
}
|
||||
|
||||
private static ByteBuffer readBack(int target, int size) {
|
||||
final ByteBuffer out = MemoryUtil.memAlloc(size);
|
||||
GLStateManager.glGetBufferSubData(target, 0, out);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static ByteBuffer mapPersistent(int target, int id) {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferStorage(target, SIZE, PERSISTENT_STORAGE);
|
||||
return GLStateManager.glMapBufferRange(target, 0, SIZE, PERSISTENT_MAP);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bufferDataOnPersistentlyMappedBufferIsRefusedAndMappingSurvives() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
ByteBuffer out = null;
|
||||
try {
|
||||
final ByteBuffer mapped = mapPersistent(target, id);
|
||||
assertNotNull(mapped);
|
||||
GLStateManager.glBufferData(target, SIZE * 2L, GL15.GL_STATIC_DRAW);
|
||||
fill(mapped, 1);
|
||||
GLStateManager.glFlushMappedBufferRange(target, 0, SIZE);
|
||||
out = readBack(target, SIZE);
|
||||
assertPattern(out, 1);
|
||||
} finally {
|
||||
if (out != null) MemoryUtil.memFree(out);
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistentMapWithoutPersistentStorageReturnsNull() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferData(target, (long) SIZE, GL15.GL_STATIC_DRAW);
|
||||
assertNull(GLStateManager.glMapBufferRange(target, 0, SIZE, PERSISTENT_MAP));
|
||||
} finally {
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondPersistentMapReturnsNullAndFirstStillRoundTrips() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
ByteBuffer out = null;
|
||||
try {
|
||||
final ByteBuffer first = mapPersistent(target, id);
|
||||
assertNotNull(first);
|
||||
assertNull(GLStateManager.glMapBufferRange(target, 0, SIZE, PERSISTENT_MAP));
|
||||
fill(first, 2);
|
||||
GLStateManager.glFlushMappedBufferRange(target, 0, SIZE);
|
||||
out = readBack(target, SIZE);
|
||||
assertPattern(out, 2);
|
||||
} finally {
|
||||
if (out != null) MemoryUtil.memFree(out);
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoBuffersMappedNonPersistentlyKeepTheirOwnData() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int a = GLStateManager.glGenBuffers();
|
||||
final int b = GLStateManager.glGenBuffers();
|
||||
ByteBuffer outA = null;
|
||||
ByteBuffer outB = null;
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, a);
|
||||
GLStateManager.glBufferData(target, SIZE, GL15.GL_DYNAMIC_DRAW);
|
||||
GLStateManager.glBindBuffer(target, b);
|
||||
GLStateManager.glBufferData(target, SIZE, GL15.GL_DYNAMIC_DRAW);
|
||||
|
||||
GLStateManager.glBindBuffer(target, a);
|
||||
final ByteBuffer mapA = GLStateManager.glMapBufferRange(target, 0, SIZE, PLAIN_MAP);
|
||||
assertNotNull(mapA);
|
||||
GLStateManager.glBindBuffer(target, b);
|
||||
final ByteBuffer mapB = GLStateManager.glMapBufferRange(target, 0, SIZE, PLAIN_MAP);
|
||||
assertNotNull(mapB);
|
||||
fill(mapA, 3);
|
||||
fill(mapB, 4);
|
||||
|
||||
GLStateManager.glBindBuffer(target, a);
|
||||
assertTrue(GLStateManager.glUnmapBuffer(target));
|
||||
GLStateManager.glBindBuffer(target, b);
|
||||
assertTrue(GLStateManager.glUnmapBuffer(target));
|
||||
|
||||
GLStateManager.glBindBuffer(target, a);
|
||||
outA = readBack(target, SIZE);
|
||||
assertPattern(outA, 3);
|
||||
GLStateManager.glBindBuffer(target, b);
|
||||
outB = readBack(target, SIZE);
|
||||
assertPattern(outB, 4);
|
||||
} finally {
|
||||
if (outA != null) MemoryUtil.memFree(outA);
|
||||
if (outB != null) MemoryUtil.memFree(outB);
|
||||
GLStateManager.glDeleteBuffers(a);
|
||||
GLStateManager.glDeleteBuffers(b);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void unmapOfUnmappedBufferReturnsFalseAndLeavesOtherMappingIntact() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int mappedId = GLStateManager.glGenBuffers();
|
||||
final int idle = GLStateManager.glGenBuffers();
|
||||
ByteBuffer out = null;
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, mappedId);
|
||||
GLStateManager.glBufferData(target, SIZE, GL15.GL_DYNAMIC_DRAW);
|
||||
final ByteBuffer map = GLStateManager.glMapBufferRange(target, 0, SIZE, PLAIN_MAP);
|
||||
assertNotNull(map);
|
||||
fill(map, 5);
|
||||
|
||||
GLStateManager.glBindBuffer(target, idle);
|
||||
GLStateManager.glBufferData(target, SIZE, GL15.GL_DYNAMIC_DRAW);
|
||||
assertFalse(GLStateManager.glUnmapBuffer(target));
|
||||
|
||||
GLStateManager.glBindBuffer(target, mappedId);
|
||||
assertTrue(GLStateManager.glUnmapBuffer(target));
|
||||
out = readBack(target, SIZE);
|
||||
assertPattern(out, 5);
|
||||
} finally {
|
||||
if (out != null) MemoryUtil.memFree(out);
|
||||
GLStateManager.glDeleteBuffers(mappedId);
|
||||
GLStateManager.glDeleteBuffers(idle);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void outOfRangeUniformSubDataIsIgnored() {
|
||||
final int ubo = GLStateManager.glGenBuffers();
|
||||
final ByteBuffer data = MemoryUtil.memAlloc(256);
|
||||
final ByteBuffer big = MemoryUtil.memAlloc(64);
|
||||
try {
|
||||
fill(data, 6);
|
||||
fill(big, 7);
|
||||
GLStateManager.glBindBuffer(GL31.GL_UNIFORM_BUFFER, ubo);
|
||||
GLStateManager.glBufferData(GL31.GL_UNIFORM_BUFFER, data, GL15.GL_DYNAMIC_DRAW);
|
||||
assertDoesNotThrow(() -> {
|
||||
big.clear();
|
||||
GLStateManager.glBufferSubData(GL31.GL_UNIFORM_BUFFER, 1024, big);
|
||||
big.clear();
|
||||
GLStateManager.glBufferSubData(GL31.GL_UNIFORM_BUFFER, 224, big);
|
||||
big.clear();
|
||||
GLStateManager.glBufferSubData(GL31.GL_UNIFORM_BUFFER, -8, big);
|
||||
});
|
||||
} finally {
|
||||
MemoryUtil.memFree(data);
|
||||
MemoryUtil.memFree(big);
|
||||
GLStateManager.glDeleteBuffers(ubo);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void outOfRangeCopyBufferSubDataIsIgnored() {
|
||||
final int src = GLStateManager.glGenBuffers();
|
||||
final int dst = GLStateManager.glGenBuffers();
|
||||
final ByteBuffer data = MemoryUtil.memAlloc(256);
|
||||
ByteBuffer outDst = null;
|
||||
try {
|
||||
fill(data, 6);
|
||||
GLStateManager.glBindBuffer(GL31.GL_COPY_READ_BUFFER, src);
|
||||
GLStateManager.glBufferData(GL31.GL_COPY_READ_BUFFER, data, GL15.GL_STATIC_DRAW);
|
||||
GLStateManager.glBindBuffer(GL31.GL_COPY_WRITE_BUFFER, dst);
|
||||
data.clear();
|
||||
GLStateManager.glBufferData(GL31.GL_COPY_WRITE_BUFFER, data, GL15.GL_STATIC_DRAW);
|
||||
GLStateManager.glCopyBufferSubData(GL31.GL_COPY_READ_BUFFER, GL31.GL_COPY_WRITE_BUFFER, 200, 0, 128);
|
||||
GLStateManager.glCopyBufferSubData(GL31.GL_COPY_READ_BUFFER, GL31.GL_COPY_WRITE_BUFFER, 0, 200, 128);
|
||||
GLStateManager.glCopyBufferSubData(GL31.GL_COPY_READ_BUFFER, GL31.GL_COPY_WRITE_BUFFER, -4, 0, 16);
|
||||
outDst = readBack(GL31.GL_COPY_WRITE_BUFFER, 256);
|
||||
assertPattern(outDst, 6);
|
||||
} finally {
|
||||
if (outDst != null) MemoryUtil.memFree(outDst);
|
||||
MemoryUtil.memFree(data);
|
||||
GLStateManager.glDeleteBuffers(src);
|
||||
GLStateManager.glDeleteBuffers(dst);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void outOfRangeClearBufferSubDataIsIgnored() {
|
||||
final int clearStart = 64;
|
||||
final int clearEnd = 96;
|
||||
final int ssbo = GLStateManager.glGenBuffers();
|
||||
final ByteBuffer data = MemoryUtil.memAlloc(256);
|
||||
ByteBuffer out = null;
|
||||
try {
|
||||
fill(data, 8);
|
||||
GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, ssbo);
|
||||
GLStateManager.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER, data, GL15.GL_DYNAMIC_DRAW);
|
||||
GLStateManager.glClearBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, GL30.GL_R8, clearStart, clearEnd - clearStart, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, null);
|
||||
GLStateManager.glClearBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, GL30.GL_R8, 200, 128, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, null);
|
||||
GLStateManager.glClearBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, GL30.GL_R8, -4, 16, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, null);
|
||||
GLStateManager.glClearBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, GL30.GL_R8, 1024, 16, GL11.GL_RED, GL11.GL_UNSIGNED_BYTE, null);
|
||||
GlsmSdlHeadlessRig.endFrame();
|
||||
GlsmSdlHeadlessRig.beginFrame();
|
||||
out = readBack(GL43.GL_SHADER_STORAGE_BUFFER, 256);
|
||||
for (int i = 0; i < 256; i++) {
|
||||
final boolean cleared = i >= clearStart && i < clearEnd;
|
||||
assertEquals(cleared ? 0 : data.get(i), out.get(i), cleared ? "in-range clear did not zero byte " + i : "out-of-range clear touched byte " + i);
|
||||
}
|
||||
} finally {
|
||||
if (out != null) MemoryUtil.memFree(out);
|
||||
MemoryUtil.memFree(data);
|
||||
GLStateManager.glDeleteBuffers(ssbo);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondBufferStorageIsRefused() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferStorage(target, SIZE, PERSISTENT_STORAGE);
|
||||
GLStateManager.glBufferStorage(target, SIZE * 2L, PERSISTENT_STORAGE);
|
||||
assertEquals(GL11.GL_TRUE, GLStateManager.glGetBufferParameteri(target, GL44.GL_BUFFER_IMMUTABLE_STORAGE));
|
||||
assertEquals(SIZE, GLStateManager.glGetBufferParameteri(target, GL15.GL_BUFFER_SIZE));
|
||||
} finally {
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void namedBufferDataOnImmutableStorageIsRefused() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferStorage(target, SIZE, PERSISTENT_STORAGE);
|
||||
GLStateManager.glNamedBufferData(id, SIZE * 2L, GL15.GL_STATIC_DRAW);
|
||||
assertEquals(SIZE, GLStateManager.glGetBufferParameteri(target, GL15.GL_BUFFER_SIZE));
|
||||
} finally {
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void bufferStorageWithNullDataMarksBufferImmutable() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferStorage(target, (ByteBuffer) null, PERSISTENT_STORAGE);
|
||||
assertEquals(GL11.GL_TRUE, GLStateManager.glGetBufferParameteri(target, GL44.GL_BUFFER_IMMUTABLE_STORAGE));
|
||||
} finally {
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertBufferDataUnmapsPlainMapping(int target) {
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferData(target, SIZE, GL15.GL_DYNAMIC_DRAW);
|
||||
assertNotNull(GLStateManager.glMapBufferRange(target, 0, SIZE, PLAIN_MAP));
|
||||
GLStateManager.glBufferData(target, SIZE, GL15.GL_DYNAMIC_DRAW);
|
||||
assertNotNull(GLStateManager.glMapBufferRange(target, 0, SIZE, PLAIN_MAP));
|
||||
assertTrue(GLStateManager.glUnmapBuffer(target));
|
||||
} finally {
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void bufferDataOnMappedArrayBufferUnmapsIt() {
|
||||
assertBufferDataUnmapsPlainMapping(GL15.GL_ARRAY_BUFFER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bufferDataOnMappedUniformBufferUnmapsIt() {
|
||||
assertBufferDataUnmapsPlainMapping(GL31.GL_UNIFORM_BUFFER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void outOfRangeFlushOfPersistentMappingIsIgnored() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
ByteBuffer out = null;
|
||||
try {
|
||||
final ByteBuffer mapped = mapPersistent(target, id);
|
||||
assertNotNull(mapped);
|
||||
GLStateManager.glFlushMappedBufferRange(target, SIZE, 64);
|
||||
GLStateManager.glFlushMappedBufferRange(target, SIZE - 16, 64);
|
||||
GLStateManager.glFlushMappedBufferRange(target, -8, 16);
|
||||
fill(mapped, 9);
|
||||
GLStateManager.glFlushMappedBufferRange(target, 0, SIZE);
|
||||
out = readBack(target, SIZE);
|
||||
assertPattern(out, 9);
|
||||
} finally {
|
||||
if (out != null) MemoryUtil.memFree(out);
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistentMappingAtNonZeroOffsetRoundTrips() {
|
||||
final int target = GL15.GL_ARRAY_BUFFER;
|
||||
final int id = GLStateManager.glGenBuffers();
|
||||
final ByteBuffer out = MemoryUtil.memAlloc(SIZE);
|
||||
try {
|
||||
GLStateManager.glBindBuffer(target, id);
|
||||
GLStateManager.glBufferStorage(target, SIZE * 2L, PERSISTENT_STORAGE);
|
||||
final ByteBuffer mapped = GLStateManager.glMapBufferRange(target, SIZE, SIZE, PERSISTENT_MAP);
|
||||
assertNotNull(mapped);
|
||||
fill(mapped, 10);
|
||||
GLStateManager.glFlushMappedBufferRange(target, 0, SIZE);
|
||||
GLStateManager.glGetBufferSubData(target, SIZE, out);
|
||||
assertPattern(out, 10);
|
||||
} finally {
|
||||
MemoryUtil.memFree(out);
|
||||
GLStateManager.glDeleteBuffers(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyFromPersistentMappingShorterThanBufferReadsGpuContents() {
|
||||
final int src = GLStateManager.glGenBuffers();
|
||||
final int dst = GLStateManager.glGenBuffers();
|
||||
final ByteBuffer data = MemoryUtil.memAlloc(SIZE * 2);
|
||||
ByteBuffer out = null;
|
||||
try {
|
||||
fill(data, 11);
|
||||
GLStateManager.glBindBuffer(GL31.GL_COPY_READ_BUFFER, src);
|
||||
GLStateManager.glBufferStorage(GL31.GL_COPY_READ_BUFFER, data, PERSISTENT_STORAGE);
|
||||
assertNotNull(GLStateManager.glMapBufferRange(GL31.GL_COPY_READ_BUFFER, 0, SIZE, PERSISTENT_MAP));
|
||||
GLStateManager.glBindBuffer(GL31.GL_COPY_WRITE_BUFFER, dst);
|
||||
GLStateManager.glBufferData(GL31.GL_COPY_WRITE_BUFFER, SIZE, GL15.GL_STATIC_DRAW);
|
||||
GLStateManager.glCopyBufferSubData(GL31.GL_COPY_READ_BUFFER, GL31.GL_COPY_WRITE_BUFFER, SIZE, 0, 256);
|
||||
out = readBack(GL31.GL_COPY_WRITE_BUFFER, 256);
|
||||
for (int i = 0; i < 256; i++) {
|
||||
assertEquals(data.get(SIZE + i), out.get(i), "byte " + i);
|
||||
}
|
||||
} finally {
|
||||
if (out != null) MemoryUtil.memFree(out);
|
||||
MemoryUtil.memFree(data);
|
||||
GLStateManager.glDeleteBuffers(src);
|
||||
GLStateManager.glDeleteBuffers(dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import org.lwjgl.opengl.GL43;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@@ -45,6 +47,30 @@ class GlsmSdlReadbackTest {
|
||||
GLStateManager.glDeleteTextures(texture);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBufferSubDataSeesSameFrameStorageBufferWrite() {
|
||||
final int size = 64;
|
||||
final int ssbo = GLStateManager.glGenBuffers();
|
||||
GLStateManager.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, ssbo);
|
||||
|
||||
final ByteBuffer initial = BufferUtils.createByteBuffer(size);
|
||||
final ByteBuffer update = BufferUtils.createByteBuffer(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
initial.put(i, (byte) 0x11);
|
||||
update.put(i, (byte) (i + 1));
|
||||
}
|
||||
GLStateManager.glBufferData(GL43.GL_SHADER_STORAGE_BUFFER, initial, GL15.GL_DYNAMIC_DRAW);
|
||||
GLStateManager.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0, update);
|
||||
|
||||
final ByteBuffer dst = BufferUtils.createByteBuffer(size);
|
||||
GLStateManager.glGetBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, 0, dst);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
assertEquals((byte) (i + 1), dst.get(i), "byte " + i + " should hold the same-frame sub-data write");
|
||||
}
|
||||
GLStateManager.glDeleteBuffers(ssbo);
|
||||
}
|
||||
|
||||
@Test
|
||||
void readPixelsSeesPriorDraw() {
|
||||
GlsmSdlHeadlessRig.bindTarget();
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ class AbsoluteCopyRegionTest {
|
||||
for (int i = 0; i < 32; i++) src.put(i, (byte) i);
|
||||
src.position(7).limit(24);
|
||||
dst.position(9).limit(28);
|
||||
PersistentBufferSync.mirrorPersistentCopy(src, 4, dst, 16, 8);
|
||||
PersistentBufferSync.mirrorPersistentCopy(new PersistentMapping(src, 0L, 32L, 0), 4, new PersistentMapping(dst, 0L, 32L, 0), 16, 8);
|
||||
for (int i = 0; i < 8; i++) assertEquals((byte) (4 + i), dst.get(16 + i));
|
||||
assertEquals(7, src.position());
|
||||
assertEquals(24, src.limit());
|
||||
|
||||
+13
-11
@@ -40,7 +40,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(64);
|
||||
try {
|
||||
rm.putPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, 64, 0);
|
||||
|
||||
assertFalse(rm.hasDirtyPersistentRegions(), "a mapping with no writes is not dirty");
|
||||
@@ -70,7 +70,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(64);
|
||||
try {
|
||||
rm.putPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, 64, 0);
|
||||
|
||||
sync.onPersistentBufferWrite(1, 0, 16);
|
||||
@@ -95,7 +95,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
try {
|
||||
final int atStart = rm.getMappingsVersion();
|
||||
|
||||
rm.putPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
final int afterPut = rm.getMappingsVersion();
|
||||
assertNotEquals(atStart, afterPut, "adding a mapping must move the version");
|
||||
|
||||
@@ -107,7 +107,8 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
sync.enqueueDirtyPersistentRegions();
|
||||
assertEquals(afterPut, rm.getMappingsVersion(), "dirtying and draining is not a mapping change");
|
||||
|
||||
rm.swapPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.removePersistentMapping(1);
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
final int afterSwap = rm.getMappingsVersion();
|
||||
assertNotEquals(afterPut, afterSwap, "swapping a mapping must move the version");
|
||||
|
||||
@@ -129,7 +130,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(256);
|
||||
try {
|
||||
rm.putPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, 256, 0);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
@@ -156,13 +157,14 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
final ByteBuffer first = MemoryUtil.memCalloc(64);
|
||||
final ByteBuffer second = MemoryUtil.memCalloc(64);
|
||||
try {
|
||||
rm.putPersistentMapping(1, new PersistentMapping(first, 0L, first.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(first, 0L, first.capacity(), 0));
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, 64, 0);
|
||||
sync.onPersistentBufferWrite(1, 0, 16);
|
||||
sync.enqueueDirtyPersistentRegions();
|
||||
assertEquals(1, sink.enqueued);
|
||||
|
||||
rm.swapPersistentMapping(1, new PersistentMapping(second, 0L, second.capacity(), 0));
|
||||
rm.removePersistentMapping(1);
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(second, 0L, second.capacity(), 0));
|
||||
sync.onPersistentBufferWrite(1, 0, 16);
|
||||
sync.enqueueDirtyPersistentRegions();
|
||||
assertEquals(2, sink.enqueued, "the drain must see the replacement, not a stale snapshot");
|
||||
@@ -184,7 +186,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(4096);
|
||||
try {
|
||||
rm.putPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, 4096, 0);
|
||||
|
||||
final int writers = 6;
|
||||
@@ -235,7 +237,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(64);
|
||||
try {
|
||||
final PersistentMapping pm = new PersistentMapping(staging, 0L, staging.capacity(), 0);
|
||||
rm.putPersistentMapping(1, pm);
|
||||
rm.putPersistentMappingIfAbsent(1, pm);
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, 64, 0);
|
||||
|
||||
final long[] stampAtEnqueue = {-1L};
|
||||
@@ -267,7 +269,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
final int seedCount = 100;
|
||||
for (int i = 0; i < seedCount; i++) {
|
||||
final ByteBuffer b = MemoryUtil.memAlloc(64);
|
||||
rm.putPersistentMapping(i, new PersistentMapping(b, 0L, b.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(i, new PersistentMapping(b, 0L, b.capacity(), 0));
|
||||
}
|
||||
|
||||
final AtomicBoolean stop = new AtomicBoolean(false);
|
||||
@@ -279,7 +281,7 @@ class PersistentBufferSyncDirtyGateTest {
|
||||
int next = seedCount;
|
||||
while (!stop.get()) {
|
||||
final ByteBuffer b = MemoryUtil.memAlloc(64);
|
||||
rm.putPersistentMapping(next, new PersistentMapping(b, 0L, b.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(next, new PersistentMapping(b, 0L, b.capacity(), 0));
|
||||
rm.removePersistentMapping(next - 50);
|
||||
next++;
|
||||
if (next > seedCount + 10000) next = seedCount;
|
||||
|
||||
+2
-2
@@ -37,13 +37,13 @@ class PersistentDrainBoundaryTest {
|
||||
final ResourceManager rm;
|
||||
final PersistentBufferSync sync;
|
||||
final RecordingSink sink = new RecordingSink();
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(4096);
|
||||
final ByteBuffer staging = MemoryUtil.memCalloc(16384);
|
||||
|
||||
Fixture() {
|
||||
final SdlTestRig rig = SdlTestRig.create();
|
||||
rm = rig.resourceManager;
|
||||
sync = new PersistentBufferSync(rig.frameManager, rm, sink);
|
||||
rm.putPersistentMapping(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
rm.putPersistentMappingIfAbsent(1, new PersistentMapping(staging, 0L, staging.capacity(), 0));
|
||||
SdlReflect.recordBuffer(rm, 1, 0xCAFEBABEL, staging.capacity(), 0);
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -18,14 +18,14 @@ class ReleasePersistentStagingTest {
|
||||
@Test
|
||||
void nullPmAndNullStagingNoOp() {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
rm.releasePersistentStaging(null);
|
||||
rm.releasePersistentStaging(new PersistentMapping(null, 0L, 0L, 0));
|
||||
rm.releasePersistentStaging(null, 7, "unmap");
|
||||
rm.releasePersistentStaging(new PersistentMapping(null, 0L, 0L, 0), 7, "unmap");
|
||||
}
|
||||
|
||||
@Test
|
||||
void noTransferThreadFreesInline() {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
rm.releasePersistentStaging(new PersistentMapping(MemoryUtil.memAlloc(24), 0L, 24L, 0));
|
||||
rm.releasePersistentStaging(new PersistentMapping(MemoryUtil.memAlloc(24), 0L, 24L, 0), 7, "unmap");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,7 +39,7 @@ class ReleasePersistentStagingTest {
|
||||
try {
|
||||
final PersistentMapping pm = new PersistentMapping(MemoryUtil.memAlloc(24), 0L, 24L, 0);
|
||||
pm.lastEnqueuedSeq = 0L;
|
||||
rm.releasePersistentStaging(pm);
|
||||
rm.releasePersistentStaging(pm, 7, "unmap");
|
||||
assertEquals(0, SdlReflect.pendingFreeCount(tt));
|
||||
} finally { shutdown(tt); }
|
||||
}
|
||||
@@ -55,7 +55,7 @@ class ReleasePersistentStagingTest {
|
||||
try {
|
||||
final PersistentMapping pm = new PersistentMapping(MemoryUtil.memAlloc(24), 0L, 24L, 0);
|
||||
pm.lastEnqueuedSeq = 50L;
|
||||
rm.releasePersistentStaging(pm);
|
||||
rm.releasePersistentStaging(pm, 7, "unmap");
|
||||
assertEquals(1, SdlReflect.pendingFreeCount(tt));
|
||||
} finally { shutdown(tt); }
|
||||
}
|
||||
@@ -64,7 +64,7 @@ class ReleasePersistentStagingTest {
|
||||
void deleteBufferReleasesPersistentStaging() {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
SdlReflect.recordBuffer(rm, 7, 0xDEADBEEFL, 64, 0);
|
||||
rm.putPersistentMapping(7, new PersistentMapping(MemoryUtil.memCalloc(64), 0L, 64L, 0));
|
||||
rm.putPersistentMappingIfAbsent(7, new PersistentMapping(MemoryUtil.memCalloc(64), 0L, 64L, 0));
|
||||
|
||||
rm.deleteBuffer(7);
|
||||
|
||||
@@ -84,7 +84,7 @@ class ReleasePersistentStagingTest {
|
||||
final PersistentMapping pm = new PersistentMapping(staging, 0L, staging.capacity(), 0);
|
||||
pm.lastEnqueuedSeq = 99L;
|
||||
SdlReflect.recordBuffer(rm, 7, 0xDEADBEEFL, 64, 0);
|
||||
rm.putPersistentMapping(7, pm);
|
||||
rm.putPersistentMappingIfAbsent(7, pm);
|
||||
|
||||
rm.deleteBuffer(7);
|
||||
|
||||
|
||||
+97
@@ -3,7 +3,9 @@ package com.gtnewhorizons.angelica.sdlgpu.resource;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.SdlTestRig;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.frame.ContextState;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -159,4 +161,99 @@ class ResourceManagerConcurrencyTest {
|
||||
|
||||
assertTrue(created.get() >= threads, "each thread should have created at least one FBO");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putPlainMappingIfAbsent_oneWinnerAcrossThreads() throws Exception {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
final int glId = 9001;
|
||||
final int n = 8;
|
||||
final AtomicInteger winners = new AtomicInteger();
|
||||
|
||||
race(n, slot -> {
|
||||
final ByteBuffer staging = MemoryUtil.memAlloc(16);
|
||||
if (rm.putPlainMappingIfAbsent(glId, staging, 0L, 16L, false, slot)) winners.incrementAndGet();
|
||||
else MemoryUtil.memFree(staging);
|
||||
});
|
||||
|
||||
assertEquals(1, winners.get(), "exactly one thread may map the buffer");
|
||||
final ByteBuffer staging = rm.takePlainStaging(glId);
|
||||
assertTrue(staging != null, "the winning mapping must still be registered");
|
||||
MemoryUtil.memFree(staging);
|
||||
assertNull(rm.takePlainStaging(glId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void plainMappingTakenFromAnotherThread() throws Exception {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
final int glId = 9002;
|
||||
final ByteBuffer staging = MemoryUtil.memAlloc(32);
|
||||
try {
|
||||
assertTrue(rm.putPlainMappingIfAbsent(glId, staging, 8L, 32L, true, 0x2A));
|
||||
final MappedRange peeked = new MappedRange();
|
||||
final MappedRange taken = new MappedRange();
|
||||
final AtomicBoolean peekedOk = new AtomicBoolean();
|
||||
final AtomicBoolean takenOk = new AtomicBoolean();
|
||||
final AtomicBoolean secondTake = new AtomicBoolean(true);
|
||||
|
||||
race(1, slot -> {
|
||||
peekedOk.set(rm.peekPlainMapping(glId, peeked));
|
||||
takenOk.set(rm.takePlainMapping(glId, taken));
|
||||
secondTake.set(rm.takePlainMapping(glId, new MappedRange()));
|
||||
});
|
||||
|
||||
assertTrue(peekedOk.get(), "peek from another thread must see the mapping");
|
||||
assertSame(staging, peeked.staging);
|
||||
assertTrue(takenOk.get(), "take from another thread must succeed");
|
||||
assertSame(staging, taken.staging);
|
||||
assertEquals(glId, taken.glId);
|
||||
assertEquals(8L, taken.offset);
|
||||
assertEquals(32L, taken.length);
|
||||
assertTrue(taken.invalidate);
|
||||
assertEquals(0x2A, taken.accessFlags);
|
||||
assertFalse(secondTake.get(), "a taken mapping must be gone");
|
||||
assertFalse(rm.peekPlainMapping(glId, new MappedRange()));
|
||||
} finally {
|
||||
MemoryUtil.memFree(staging);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteBufferFromAnotherThreadDropsPlainMapping() throws Exception {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
final int glId = 9003;
|
||||
assertTrue(rm.putPlainMappingIfAbsent(glId, MemoryUtil.memAlloc(16), 0L, 16L, false, 0));
|
||||
|
||||
race(1, slot -> rm.deleteBuffer(glId));
|
||||
|
||||
assertFalse(rm.peekPlainMapping(glId, new MappedRange()));
|
||||
assertFalse(rm.takePlainMapping(glId, new MappedRange()));
|
||||
final ByteBuffer again = MemoryUtil.memAlloc(16);
|
||||
assertTrue(rm.putPlainMappingIfAbsent(glId, again, 0L, 16L, false, 0), "a deleted buffer's id must be mappable again");
|
||||
MemoryUtil.memFree(rm.takePlainStaging(glId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistentAndPlainMappingsExcludeEachOther() {
|
||||
final ResourceManager rm = SdlTestRig.resourceManager();
|
||||
final int plainFirst = 9004;
|
||||
final int persistentFirst = 9005;
|
||||
final ByteBuffer plainStaging = MemoryUtil.memAlloc(16);
|
||||
final ByteBuffer persistentStaging = MemoryUtil.memAlloc(16);
|
||||
final ByteBuffer refused = MemoryUtil.memAlloc(16);
|
||||
try {
|
||||
assertTrue(rm.putPlainMappingIfAbsent(plainFirst, plainStaging, 0L, 16L, false, 0));
|
||||
assertFalse(rm.putPersistentMappingIfAbsent(plainFirst, new PersistentMapping(refused, 0L, 16L, 0)), "persistent map of a plain-mapped buffer must be refused");
|
||||
assertNull(rm.getPersistentMapping(plainFirst));
|
||||
|
||||
assertTrue(rm.putPersistentMappingIfAbsent(persistentFirst, new PersistentMapping(persistentStaging, 0L, 16L, 0)));
|
||||
assertFalse(rm.putPlainMappingIfAbsent(persistentFirst, refused, 0L, 16L, false, 0), "plain map of a persistently mapped buffer must be refused");
|
||||
assertFalse(rm.peekPlainMapping(persistentFirst, new MappedRange()));
|
||||
} finally {
|
||||
rm.takePlainStaging(plainFirst);
|
||||
rm.removePersistentMapping(persistentFirst);
|
||||
MemoryUtil.memFree(plainStaging);
|
||||
MemoryUtil.memFree(persistentStaging);
|
||||
MemoryUtil.memFree(refused);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -36,7 +36,7 @@ class AttribLocationPatchTest {
|
||||
inputs[i] = new ShaderManager.VsInput(names[i], i, i, i < VEC_SIZES.length ? VEC_SIZES[i] : 4, i < BASE_TYPES.length ? BASE_TYPES[i] : Spvc.SPVC_BASETYPE_FP32);
|
||||
}
|
||||
final ShaderManager.StageReflection e = ShaderManager.StageReflection.EMPTY;
|
||||
return new ShaderManager.StageReflection(e.counts(), e.samplerNames(), e.extraUniformNames(),
|
||||
return new ShaderManager.StageReflection(e.counts(), e.samplerNames(), e.unusedSamplerNames(), e.extraUniformNames(),
|
||||
e.storageImageNames(), e.uboSize(), e.uboMembers(), List.of(inputs), e.vsOutputs(), e.fsInputs(),
|
||||
e.maxOutputLocation(), e.numReadonlyStorageBuffers(), e.numReadwriteStorageBuffers(),
|
||||
e.numReadonlyStorageTextures(), e.numReadwriteStorageTextures(), e.blocks());
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ class AttribLocationReflectionTest {
|
||||
|
||||
private static StageReflection reflectionWith(VsInput... inputs) {
|
||||
return new StageReflection(
|
||||
ShaderManager.ResourceCounts.EMPTY, List.of(), List.of(), List.of(), 0, List.of(),
|
||||
ShaderManager.ResourceCounts.EMPTY, List.of(), List.of(), List.of(), List.of(), 0, List.of(),
|
||||
List.of(inputs), List.of(), List.of(), -1, 0, 0, 0, 0, ShaderManager.BlockReflection.emptyBlocks());
|
||||
}
|
||||
|
||||
|
||||
+179
@@ -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<Edit> 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);
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -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<GLSLParser.Translation_unitContext, List<Edit>> collect) {
|
||||
private static String edit(String source, BiConsumer<ShaderIndex, List<Edit>> collect) {
|
||||
final GLSLParser.Translation_unitContext root;
|
||||
try {
|
||||
root = GlslTransformUtils.parseFullQuiet(source);
|
||||
@@ -50,7 +51,7 @@ final class ShaderTransformChain {
|
||||
return source;
|
||||
}
|
||||
final List<Edit> edits = new ArrayList<>();
|
||||
collect.accept(root, edits);
|
||||
collect.accept(new ShaderIndex(root, source), edits);
|
||||
return edits.isEmpty() ? source : GlslVulkanPreprocess.applyEdits(source, edits);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -73,7 +73,7 @@ class StageDiskCacheTest {
|
||||
|
||||
private static byte[] poison(String marker) {
|
||||
final ShaderManager.StageReflection reflection = new ShaderManager.StageReflection(
|
||||
ShaderManager.ResourceCounts.EMPTY, List.of(marker), List.of(), List.of(), 0, List.of(), List.of(), List.of(), List.of(),
|
||||
ShaderManager.ResourceCounts.EMPTY, List.of(marker), List.of(), List.of(), List.of(), 0, List.of(), List.of(), List.of(), List.of(),
|
||||
-1, 0, 0, 0, 0, ShaderManager.BlockReflection.emptyBlocks());
|
||||
return ShaderStageSerializer.encode(POISON_SPIRV, reflection, ShaderManager.GraphicsBindingMap.EMPTY, Set.of("poison_bool"), "poison_source");
|
||||
}
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.gtnewhorizons.angelica.sdlgpu.shader;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.shader.SpirvCompiler;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
import org.lwjgl.util.shaderc.Shaderc;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
class UnusedSamplerReflectionTest {
|
||||
|
||||
private static final String FRAGMENT = """
|
||||
#version 330 core
|
||||
uniform sampler2D tex;
|
||||
uniform sampler2D normals;
|
||||
uniform sampler2D specular;
|
||||
in vec2 uv;
|
||||
out vec4 fragColor;
|
||||
float highlight(float d) {
|
||||
float specular = d * d;
|
||||
return specular;
|
||||
}
|
||||
void main() {
|
||||
fragColor = texture(tex, uv) * highlight(uv.x);
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void samplersOnlyShadowedOrDeclaredAreUnused() {
|
||||
final SpirvCompiler.Result r = SpirvCompiler.compile(FRAGMENT, Shaderc.shaderc_fragment_shader, "test", SpirvCompiler.Options.vulkanRelaxed());
|
||||
if (r.spirv() == null) fail("Compilation failed: " + r.error());
|
||||
final ByteBuffer spirv = r.spirv();
|
||||
try {
|
||||
final ShaderManager.StageReflection refl = ShaderManager.reflectStage(spirv, false);
|
||||
assertEquals(List.of("normals", "specular"), refl.unusedSamplerNames().stream().sorted().toList(),
|
||||
"declared samplers: " + refl.samplerNames());
|
||||
} finally {
|
||||
MemoryUtil.memFree(spirv);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -57,7 +57,7 @@ class FragmentStorageTextureSlotTest {
|
||||
final ByteBuffer spirv = r.spirv();
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER, false);
|
||||
try {
|
||||
final String msl = StandardCharsets.UTF_8.decode(out.code().duplicate()).toString();
|
||||
final boolean[] seen = new boolean[samplerCount];
|
||||
@@ -136,7 +136,7 @@ class FragmentStorageTextureSlotTest {
|
||||
final ByteBuffer spirv = Reflect.get(obj, "spirv");
|
||||
final ShaderManager.StageReflection refl = Reflect.get(obj, "reflection");
|
||||
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER, false);
|
||||
try {
|
||||
final String msl = StandardCharsets.UTF_8.decode(out.code().duplicate()).toString();
|
||||
final int samplerCount = refl.counts().numSamplers();
|
||||
|
||||
+102
-12
@@ -8,6 +8,7 @@ import com.gtnewhorizons.angelica.glsm.testutil.Reflect;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.ShaderManager;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.SpirvTestShaders;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.cross.CrossCompileCache;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.shader.cross.CrossCompileUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -52,7 +53,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(SpirvTestShaders.VERTEX_GLSL, Shaderc.shaderc_vertex_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertTrue(msl.length() > 0, "MSL output non-empty");
|
||||
@@ -75,7 +76,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(SpirvTestShaders.FRAGMENT_GLSL, Shaderc.shaderc_fragment_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertTrue(msl.contains("fragment "), "MSL should contain fragment stage qualifier:\n" + msl);
|
||||
@@ -114,7 +115,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(SpirvTestShaders.LDS_MULTI_MIP_GLSL, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(spirv);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertTrue(msl.contains("kernel "), "Compute MSL should contain kernel stage qualifier:\n" + msl);
|
||||
@@ -146,7 +147,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(glsl, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(spirv);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertTrue(msl.contains("kernel "), "compute MSL should contain kernel stage qualifier:\n" + msl);
|
||||
@@ -167,7 +168,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(CHUNK_CULL_GLSL, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(spirv);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertTrue(msl.contains("kernel "), "Compute MSL should contain kernel stage qualifier:\n" + msl);
|
||||
@@ -202,7 +203,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(VERTEX_WRITES_IMAGE_GLSL, Shaderc.shaderc_vertex_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertFalse(msl.contains("vertex void main0"),
|
||||
@@ -236,7 +237,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(pre.rewrittenSource(), Shaderc.shaderc_fragment_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertTrue(msl.contains("angelica_renamed_try"), "the local must keep a renamed identifier:\n" + msl);
|
||||
@@ -255,7 +256,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(SpirvTestShaders.VERTEX_GLSL, Shaderc.shaderc_vertex_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER, false);
|
||||
MemoryUtil.memFree(out.code());
|
||||
} finally {
|
||||
MemoryUtil.memFree(spirv);
|
||||
@@ -381,7 +382,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(glsl, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(spirv);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER, false);
|
||||
try {
|
||||
final String msl = decode(out.code());
|
||||
assertUniqueBufferSlots(msl);
|
||||
@@ -436,7 +437,7 @@ class MslCrossCompileTest {
|
||||
final ByteBuffer spirv = compile(glsl, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(spirv);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER, false);
|
||||
try {
|
||||
assertTrue(decode(out.code()).contains("kernel "), name + " should produce compute MSL");
|
||||
} finally {
|
||||
@@ -456,10 +457,10 @@ class MslCrossCompileTest {
|
||||
try {
|
||||
ShaderManager.remapSpirvForSDLGPU(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final byte[] heap = ShaderCacheIO.toHeap(spirv);
|
||||
MemoryUtil.memFree(MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER).code());
|
||||
MemoryUtil.memFree(MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER, false).code());
|
||||
Reflect.<Map<?, ?>>get(cache(), "cache").clear();
|
||||
ShaderDiskCache.putBlob(cache().diskKey(heap, GL20.GL_VERTEX_SHADER), "poison_entry", "poison".getBytes(StandardCharsets.UTF_8));
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_VERTEX_SHADER, false);
|
||||
try {
|
||||
assertEquals("poison_entry", out.entrypoint());
|
||||
assertEquals("poison", new String(ShaderCacheIO.toHeap(out.code()), StandardCharsets.UTF_8));
|
||||
@@ -471,6 +472,95 @@ class MslCrossCompileTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static final String IMAGE_ATOMIC_GLSL = """
|
||||
#version 460 core
|
||||
layout(local_size_x = 64) in;
|
||||
layout(binding = 0, r32i) uniform iimage2D counter;
|
||||
void main() {
|
||||
imageAtomicAdd(counter, ivec2(0, 0), 1);
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void imageAtomicsUseTheAuxBufferWithoutTextureAtomics() {
|
||||
assertTrue(computeMsl(IMAGE_ATOMIC_GLSL, false).contains("counter_atomic"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void imageAtomicsCompileToTextureAtomicsWhenAvailable() {
|
||||
final String msl = computeMsl(IMAGE_ATOMIC_GLSL, true);
|
||||
assertFalse(msl.contains("counter_atomic"), msl);
|
||||
assertTrue(msl.contains("counter.atomic_fetch_add("), msl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void textureAtomicsDoNotChangeShadersWithoutImageAtomics() {
|
||||
final String base = computeMsl(CHUNK_CULL_GLSL, false);
|
||||
Reflect.<Map<?, ?>>get(cache(), "cache").clear();
|
||||
assertEquals(base, computeMsl(CHUNK_CULL_GLSL, true));
|
||||
}
|
||||
|
||||
private static final String FRAGMENT_IMAGE_ATOMIC_GLSL = """
|
||||
#version 460 core
|
||||
layout(binding = 0, r32i) uniform iimage2D counter;
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
void main() {
|
||||
fragColor = vec4(imageAtomicAdd(counter, ivec2(0, 0), 1));
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void textureAtomicsLeaveRasterStagesAtMsl22() {
|
||||
final ByteBuffer spirv = compile(FRAGMENT_IMAGE_ATOMIC_GLSL, Shaderc.shaderc_fragment_shader);
|
||||
try {
|
||||
assertTrue(CrossCompileUtil.usesImageAtomics(spirv));
|
||||
final String base = fragmentMsl(spirv, false);
|
||||
Reflect.<Map<?, ?>>get(cache(), "cache").clear();
|
||||
final String atomics = fragmentMsl(spirv, true);
|
||||
assertEquals(base, atomics);
|
||||
assertFalse(atomics.contains("counter.atomic_fetch_add("), atomics);
|
||||
} finally {
|
||||
MemoryUtil.memFree(spirv);
|
||||
}
|
||||
}
|
||||
|
||||
private static String fragmentMsl(ByteBuffer spirv, boolean textureAtomics) {
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL20.GL_FRAGMENT_SHADER, textureAtomics);
|
||||
try {
|
||||
return decode(out.code());
|
||||
} finally {
|
||||
MemoryUtil.memFree(out.code());
|
||||
}
|
||||
}
|
||||
|
||||
private static String computeMsl(String glsl, boolean textureAtomics) {
|
||||
final ByteBuffer spirv = compile(glsl, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
ShaderManager.remapSpirvForComputeSDLGPU(spirv);
|
||||
final CrossCompileCache.Output out = MslCrossCompile.compile(spirv, GL43.GL_COMPUTE_SHADER, textureAtomics);
|
||||
try {
|
||||
return decode(out.code());
|
||||
} finally {
|
||||
MemoryUtil.memFree(out.code());
|
||||
}
|
||||
} finally {
|
||||
MemoryUtil.memFree(spirv);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyShadersWithImageAtomicsAreDetected() {
|
||||
final ByteBuffer atomic = compile(IMAGE_ATOMIC_GLSL, Shaderc.shaderc_compute_shader);
|
||||
final ByteBuffer plain = compile(CHUNK_CULL_GLSL, Shaderc.shaderc_compute_shader);
|
||||
try {
|
||||
assertTrue(CrossCompileUtil.usesImageAtomics(atomic));
|
||||
assertFalse(CrossCompileUtil.usesImageAtomics(plain));
|
||||
} finally {
|
||||
MemoryUtil.memFree(atomic);
|
||||
MemoryUtil.memFree(plain);
|
||||
}
|
||||
}
|
||||
|
||||
private static String decode(ByteBuffer buf) {
|
||||
final ByteBuffer slice = buf.duplicate();
|
||||
int len = slice.remaining();
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.gtnewhorizons.angelica.api;
|
||||
|
||||
import com.gtnewhorizons.angelica.config.AngelicaConfig;
|
||||
import net.coderbot.iris.gbuffer_overrides.matching.SpecialCondition;
|
||||
import net.coderbot.iris.layer.GbufferPrograms;
|
||||
import net.coderbot.iris.shaderpack.materialmap.NamespacedId;
|
||||
import net.coderbot.iris.uniforms.CapturedRenderingState;
|
||||
|
||||
public final class EnderDragonBeams {
|
||||
|
||||
private static final NamespacedId CRYSTAL_BEAM = new NamespacedId("minecraft", "end_crystal_beam");
|
||||
private static final NamespacedId DEATH_RAYS = new NamespacedId("minecraft", "dragon_death_rays");
|
||||
|
||||
private EnderDragonBeams() {}
|
||||
|
||||
public static void beginCrystalBeam() {
|
||||
if (!AngelicaConfig.enableIris) return;
|
||||
CapturedRenderingState.INSTANCE.pushCurrentEntityAndItem();
|
||||
CapturedRenderingState.INSTANCE.setCurrentNamedEntity(CRYSTAL_BEAM);
|
||||
}
|
||||
|
||||
public static void endCrystalBeam() {
|
||||
if (!AngelicaConfig.enableIris) return;
|
||||
CapturedRenderingState.INSTANCE.popCurrentEntityAndItem();
|
||||
}
|
||||
|
||||
public static void beginDeathRays() {
|
||||
if (!AngelicaConfig.enableIris) return;
|
||||
GbufferPrograms.setupSpecialRenderCondition(SpecialCondition.LIGHTNING);
|
||||
CapturedRenderingState.INSTANCE.pushCurrentEntityAndItem();
|
||||
CapturedRenderingState.INSTANCE.setCurrentNamedEntity(DEATH_RAYS);
|
||||
}
|
||||
|
||||
public static void endDeathRays() {
|
||||
if (!AngelicaConfig.enableIris) return;
|
||||
CapturedRenderingState.INSTANCE.popCurrentEntityAndItem();
|
||||
GbufferPrograms.teardownSpecialRenderCondition();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.gtnewhorizons.angelica.api;
|
||||
|
||||
public interface EyePassRenderer {
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package com.gtnewhorizons.angelica.client.font;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.vao.IndexBuffer;
|
||||
import com.gtnewhorizon.gtnhlib.util.font.FontRendering;
|
||||
import com.gtnewhorizon.gtnhlib.util.font.GlyphReplacements;
|
||||
import com.gtnewhorizons.angelica.config.AngelicaConfig;
|
||||
import com.gtnewhorizons.angelica.config.FontConfig;
|
||||
import com.gtnewhorizons.angelica.glsm.GLContextState;
|
||||
@@ -393,14 +392,33 @@ public class BatchingFontRenderer {
|
||||
pushQuadIdx();
|
||||
}
|
||||
|
||||
private static final float[] MAX_AA_REACH = {0f, 6f, 8f};
|
||||
|
||||
private void pushTexRect(float x, float y, float w, float h, float itOff, int rgba, float uStart, float vStart, float uSz, float vSz, boolean flipV) {
|
||||
ensureCapacity();
|
||||
|
||||
final float tBu0 = uStart;
|
||||
final float tBu1 = uStart + uSz;
|
||||
final float tBv0 = vStart;
|
||||
final float tBv1 = vStart + vSz;
|
||||
|
||||
if (FontConfig.fontAAMode > 0 && w > 0 && h > 0) {
|
||||
final float margin = MAX_AA_REACH[FontConfig.fontAAMode] * preprocessAAStrength() + 0.5f;
|
||||
final float marginU = margin * (uSz / w);
|
||||
final float marginV = margin * (vSz / h);
|
||||
itOff *= (h + 2 * margin) / h;
|
||||
x -= margin; w += 2 * margin;
|
||||
y -= margin; h += 2 * margin;
|
||||
uStart -= marginU; uSz += 2 * marginU;
|
||||
vStart -= marginV; vSz += 2 * marginV;
|
||||
}
|
||||
|
||||
float vTop = flipV ? vStart + vSz : vStart;
|
||||
float vBot = flipV ? vStart : vStart + vSz;
|
||||
pushVtx(x + itOff, y, rgba, uStart, vTop, uStart, uStart + uSz, vStart, vStart + vSz);
|
||||
pushVtx(x - itOff, y + h, rgba, uStart, vBot, uStart, uStart + uSz, vStart, vStart + vSz);
|
||||
pushVtx(x + itOff + w, y, rgba, uStart + uSz, vTop, uStart, uStart + uSz, vStart, vStart + vSz);
|
||||
pushVtx(x - itOff + w, y + h, rgba, uStart + uSz, vBot, uStart, uStart + uSz, vStart, vStart + vSz);
|
||||
pushVtx(x + itOff, y, rgba, uStart, vTop, tBu0, tBu1, tBv0, tBv1);
|
||||
pushVtx(x - itOff, y + h, rgba, uStart, vBot, tBu0, tBu1, tBv0, tBv1);
|
||||
pushVtx(x + itOff + w, y, rgba, uStart + uSz, vTop, tBu0, tBu1, tBv0, tBv1);
|
||||
pushVtx(x - itOff + w, y + h, rgba, uStart + uSz, vBot, tBu0, tBu1, tBv0, tBv1);
|
||||
pushQuadIdx();
|
||||
}
|
||||
|
||||
@@ -859,6 +877,10 @@ public class BatchingFontRenderer {
|
||||
truncateBatchToWatermark();
|
||||
}
|
||||
|
||||
private static float preprocessAAStrength() {
|
||||
return FontConfig.fontAAStrength / 120.f;
|
||||
}
|
||||
|
||||
private void setupFontDrawState() {
|
||||
GLStateManager.enableTexture();
|
||||
GLStateManager.enableAlphaTest();
|
||||
@@ -873,7 +895,7 @@ public class BatchingFontRenderer {
|
||||
}
|
||||
if (FontConfig.fontAAStrength != fontAAStrengthLast) {
|
||||
fontAAStrengthLast = FontConfig.fontAAStrength;
|
||||
GLStateManager.glUniform1f(AAStrength, FontConfig.fontAAStrength / 120.f);
|
||||
GLStateManager.glUniform1f(AAStrength, preprocessAAStrength());
|
||||
}
|
||||
if (FontConfig.fontBrightness != fontBrightnessLast) {
|
||||
fontBrightnessLast = FontConfig.fontBrightness;
|
||||
@@ -1429,16 +1451,7 @@ public class BatchingFontRenderer {
|
||||
|
||||
if (chr == ColorCodeUtils.ESCAPED_AMPERSAND) { chr = '&'; }
|
||||
|
||||
if (FontConfig.enableCustomFont && FontConfig.enableGlyphReplacements) {
|
||||
final char replacement = GlyphReplacements.getReplacementGlyph(chr);
|
||||
if (replacement != 0) {
|
||||
if (FontProviderCustom.getPrimary().isGlyphAvailable(replacement)
|
||||
|| FontProviderCustom.getFallback().isGlyphAvailable(replacement)
|
||||
) {
|
||||
chr = replacement;
|
||||
}
|
||||
}
|
||||
}
|
||||
chr = FontStrategist.replaceCustomGlyph(this, chr, unicodeFlag);
|
||||
|
||||
// ASCII space, NBSP, NNBSP, decided before obfuscation as vanilla does.
|
||||
final boolean whitespace = chr == ' ' || chr == '\u00A0' || chr == '\u202F';
|
||||
@@ -1451,7 +1464,8 @@ public class BatchingFontRenderer {
|
||||
chr = fontProvider.getRandomReplacement(chr);
|
||||
}
|
||||
|
||||
heightNorth = anchorY + (underlying.FONT_HEIGHT - 1.0f) * (0.5f - glyphScaleY * fontProvider.getYScaleMultiplier() / 2);
|
||||
heightNorth = anchorY + (underlying.FONT_HEIGHT - 1.0f) * (0.5f - glyphScaleY * fontProvider.getYScaleMultiplier() / 2)
|
||||
+ fontProvider.getBaselineShift() * glyphScaleY;
|
||||
float heightSouth = (underlying.FONT_HEIGHT - 1.0f) * glyphScaleY * fontProvider.getYScaleMultiplier();
|
||||
|
||||
visibleCharIndex++;
|
||||
@@ -1623,7 +1637,9 @@ public class BatchingFontRenderer {
|
||||
return 4 * this.getWhitespaceScale();
|
||||
}
|
||||
|
||||
FontProvider fp = FontStrategist.getFontProvider(this, chr, FontConfig.enableCustomFont, underlying.getUnicodeFlag());
|
||||
final boolean unicodeFlag = underlying.getUnicodeFlag();
|
||||
chr = FontStrategist.replaceCustomGlyph(this, chr, unicodeFlag);
|
||||
FontProvider fp = FontStrategist.getFontProvider(this, chr, FontConfig.enableCustomFont, unicodeFlag);
|
||||
|
||||
return fp.getXAdvance(chr) * this.getGlyphScaleX();
|
||||
}
|
||||
|
||||
@@ -20,4 +20,7 @@ public interface FontProvider {
|
||||
float getShadowOffset();
|
||||
int getTexture(char chr);
|
||||
float getYScaleMultiplier();
|
||||
|
||||
/** Vertical offset in font pixels, applied after the glyph is centered on the line. */
|
||||
default float getBaselineShift() { return 0.0f; }
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
package com.gtnewhorizons.angelica.client.font;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.gtnewhorizons.angelica.config.FontConfig;
|
||||
import it.unimi.dsi.fastutil.chars.CharArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import jss.util.RandomXoshiro256StarStar;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.IReloadableResourceManager;
|
||||
import net.minecraft.client.resources.IResource;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* For modern Minecraft's hand-drawn font sheets.
|
||||
*/
|
||||
public final class FontProviderBitmap implements FontProvider {
|
||||
|
||||
private static final ResourceLocation DEFINITION = new ResourceLocation("angelica", "font/modern.json");
|
||||
private static final int DEFAULT_HEIGHT = 8;
|
||||
private static final FontProviderBitmap[] NONE = new FontProviderBitmap[0];
|
||||
|
||||
private static volatile FontProviderBitmap[] loaded;
|
||||
|
||||
public static void registerReloadListener() {
|
||||
((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(manager -> loaded = null);
|
||||
}
|
||||
|
||||
private final ResourceLocation sheet;
|
||||
private final int columns;
|
||||
private final float uSize;
|
||||
private final float vSize;
|
||||
private final float glyphW;
|
||||
private final float yScale;
|
||||
private final float baselineShift;
|
||||
private final char[][] slotPages = new char[256][];
|
||||
private final int[] advances;
|
||||
private final Int2ObjectOpenHashMap<char[]> charsByAdvance = new Int2ObjectOpenHashMap<>();
|
||||
private final RandomXoshiro256StarStar fontRandom = new RandomXoshiro256StarStar();
|
||||
|
||||
public static FontProviderBitmap find(char chr) {
|
||||
for (FontProviderBitmap provider : providers()) {
|
||||
if (provider.slot(chr) >= 0) {
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static FontProviderBitmap[] providers() {
|
||||
FontProviderBitmap[] local = loaded;
|
||||
if (local == null) {
|
||||
final IResourceManager resources = Minecraft.getMinecraft().getResourceManager();
|
||||
if (!resources.getResourceDomains().contains(DEFINITION.getResourceDomain())) {
|
||||
return NONE;
|
||||
}
|
||||
synchronized (FontProviderBitmap.class) {
|
||||
local = loaded;
|
||||
if (local == null) {
|
||||
local = load(resources);
|
||||
loaded = local;
|
||||
}
|
||||
}
|
||||
}
|
||||
return local;
|
||||
}
|
||||
|
||||
private static FontProviderBitmap[] load(IResourceManager resources) {
|
||||
final List<FontProviderBitmap> providers = new ArrayList<>();
|
||||
try (Reader reader = new InputStreamReader(resources.getResource(DEFINITION).getInputStream(), StandardCharsets.UTF_8)) {
|
||||
final JsonArray definitions = new JsonParser().parse(reader).getAsJsonObject().getAsJsonArray("providers");
|
||||
for (JsonElement element : definitions) {
|
||||
final JsonObject definition = element.getAsJsonObject();
|
||||
if (!"bitmap".equals(definition.get("type").getAsString())) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
providers.add(new FontProviderBitmap(definition, resources));
|
||||
} catch (IOException | RuntimeException e) {
|
||||
FontStrategist.LOGGER.warn("Skipping modern font sheet {}", definition.get("file"), e);
|
||||
}
|
||||
}
|
||||
} catch (IOException | RuntimeException e) {
|
||||
FontStrategist.LOGGER.error("Could not read modern font definition {}", DEFINITION, e);
|
||||
}
|
||||
return providers.toArray(NONE);
|
||||
}
|
||||
|
||||
private static ResourceLocation resolveSheet(IResourceManager resources, ResourceLocation sheet) {
|
||||
final ResourceLocation bundled = new ResourceLocation("angelica", sheet.getResourcePath());
|
||||
try {
|
||||
final int vanillaCopies = Minecraft.getMinecraft().mcDefaultResourcePack.resourceExists(sheet) ? 1 : 0;
|
||||
final List<IResource> copies = resources.getAllResources(sheet);
|
||||
for (IResource copy : copies) {
|
||||
copy.getInputStream().close();
|
||||
}
|
||||
return copies.size() > vanillaCopies ? sheet : bundled;
|
||||
} catch (IOException e) {
|
||||
return bundled;
|
||||
}
|
||||
}
|
||||
|
||||
private FontProviderBitmap(JsonObject definition, IResourceManager resources) throws IOException {
|
||||
final ResourceLocation file = new ResourceLocation(definition.get("file").getAsString());
|
||||
this.sheet = resolveSheet(resources, new ResourceLocation(file.getResourceDomain(), "textures/" + file.getResourcePath()));
|
||||
final int height = definition.has("height") ? definition.get("height").getAsInt() : DEFAULT_HEIGHT;
|
||||
final int ascent = definition.get("ascent").getAsInt();
|
||||
final JsonArray rows = definition.getAsJsonArray("chars");
|
||||
|
||||
final BufferedImage image;
|
||||
try (InputStream in = resources.getResource(this.sheet).getInputStream()) {
|
||||
image = ImageIO.read(in);
|
||||
}
|
||||
final String firstRow = rows.get(0).getAsString();
|
||||
this.columns = firstRow.codePointCount(0, firstRow.length());
|
||||
final int cellWidth = image.getWidth() / this.columns;
|
||||
final int cellHeight = image.getHeight() / rows.size();
|
||||
final float scale = (float) height / cellHeight;
|
||||
|
||||
this.uSize = (float) cellWidth / image.getWidth();
|
||||
this.vSize = (float) cellHeight / image.getHeight();
|
||||
this.glyphW = cellWidth * scale + 1.0f;
|
||||
this.yScale = (float) height / DEFAULT_HEIGHT;
|
||||
this.baselineShift = 3.0f + height / 2.0f - ascent;
|
||||
this.advances = new int[this.columns * rows.size()];
|
||||
|
||||
final Int2ObjectOpenHashMap<CharArrayList> byAdvance = new Int2ObjectOpenHashMap<>();
|
||||
for (int row = 0; row < rows.size(); row++) {
|
||||
final int[] codepoints = rows.get(row).getAsString().codePoints().toArray();
|
||||
for (int column = 0; column < codepoints.length; column++) {
|
||||
final int codepoint = codepoints[column];
|
||||
if (codepoint == 0 || codepoint > Character.MAX_VALUE) {
|
||||
continue;
|
||||
}
|
||||
final char chr = (char) codepoint;
|
||||
final int slot = row * this.columns + column;
|
||||
final int ink = inkWidth(image, column * cellWidth, row * cellHeight, cellWidth, cellHeight);
|
||||
this.advances[slot] = (int) (0.5f + ink * scale) + 1;
|
||||
|
||||
char[] page = this.slotPages[chr >>> 8];
|
||||
if (page == null) {
|
||||
page = this.slotPages[chr >>> 8] = new char[256];
|
||||
}
|
||||
page[chr & 0xFF] = (char) (slot + 1);
|
||||
byAdvance.computeIfAbsent(this.advances[slot], k -> new CharArrayList()).add(chr);
|
||||
}
|
||||
}
|
||||
byAdvance.int2ObjectEntrySet().forEach(e -> this.charsByAdvance.put(e.getIntKey(), e.getValue().toCharArray()));
|
||||
}
|
||||
|
||||
private static int inkWidth(BufferedImage image, int cellX, int cellY, int cellWidth, int cellHeight) {
|
||||
for (int x = cellWidth - 1; x >= 0; x--) {
|
||||
for (int y = 0; y < cellHeight; y++) {
|
||||
if ((image.getRGB(cellX + x, cellY + y) >>> 24) != 0) {
|
||||
return x + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int slot(char chr) {
|
||||
final char[] page = this.slotPages[chr >>> 8];
|
||||
return page == null ? -1 : page[chr & 0xFF] - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGlyphAvailable(char chr) {
|
||||
return slot(chr) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getRandomReplacement(char chr) {
|
||||
final int slot = slot(chr);
|
||||
if (slot < 0) {
|
||||
return chr;
|
||||
}
|
||||
final char[] sameWidth = this.charsByAdvance.get(this.advances[slot]);
|
||||
return sameWidth[this.fontRandom.nextInt(sameWidth.length)];
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getUStart(char chr) {
|
||||
return (slot(chr) % this.columns) * this.uSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getVStart(char chr) {
|
||||
return (float) (slot(chr) / this.columns) * this.vSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getXAdvance(char chr) {
|
||||
return this.advances[slot(chr)];
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getGlyphW(char chr) {
|
||||
return this.glyphW;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getUSize(char chr) {
|
||||
return this.uSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getVSize(char chr) {
|
||||
return this.vSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getShadowOffset() {
|
||||
return FontConfig.fontShadowOffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTexture(char chr) {
|
||||
return FontStrategist.getIntFromResourceLocation(this.sheet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getYScaleMultiplier() {
|
||||
return this.yScale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getBaselineShift() {
|
||||
return this.baselineShift;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.gtnewhorizons.angelica.client.font;
|
||||
|
||||
import com.google.common.collect.HashMultiset;
|
||||
import com.gtnewhorizon.gtnhlib.util.font.GlyphReplacements;
|
||||
import com.gtnewhorizons.angelica.config.FontConfig;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.mixins.interfaces.FontRendererAccessor;
|
||||
@@ -166,8 +167,12 @@ public class FontStrategist {
|
||||
if (fp.isGlyphAvailable(chr)) { return fp; }
|
||||
fp = FontProviderCustom.getFallback();
|
||||
if (fp.isGlyphAvailable(chr)) { return fp; }
|
||||
fp = findModernGlyph(me, chr, forceUnicode);
|
||||
if (fp != null) { return fp; }
|
||||
return FontProviderUnicode.get();
|
||||
} else {
|
||||
final FontProvider modern = findModernGlyph(me, chr, forceUnicode);
|
||||
if (modern != null) { return modern; }
|
||||
if (!forceUnicode && FontProviderMC.get(false).isGlyphAvailable(chr)) {
|
||||
return FontProviderMC.get(false);
|
||||
} else {
|
||||
@@ -176,6 +181,23 @@ public class FontStrategist {
|
||||
}
|
||||
}
|
||||
|
||||
public static char replaceCustomGlyph(BatchingFontRenderer me, char chr, boolean forceUnicode) {
|
||||
if (!FontConfig.enableGlyphReplacements || !(FontConfig.enableCustomFont || FontConfig.enableModernFont)) { return chr; }
|
||||
final char original = GlyphReplacements.getReplacementGlyph(chr);
|
||||
if (original == 0) { return chr; }
|
||||
if (FontConfig.enableCustomFont
|
||||
&& (FontProviderCustom.getPrimary().isGlyphAvailable(original) || FontProviderCustom.getFallback().isGlyphAvailable(original))) {
|
||||
return original;
|
||||
}
|
||||
return findModernGlyph(me, original, forceUnicode) != null ? original : chr;
|
||||
}
|
||||
|
||||
// The splash font renderer can only bind its own ascii texture.
|
||||
private static FontProvider findModernGlyph(BatchingFontRenderer me, char chr, boolean forceUnicode) {
|
||||
if (!FontConfig.enableModernFont || forceUnicode || me.isSplash) { return null; }
|
||||
return FontProviderBitmap.find(chr);
|
||||
}
|
||||
|
||||
public static void reloadCustomFontProviders() {
|
||||
FontProviderCustom.getPrimary().setFont(null);
|
||||
FontProviderCustom.getFallback().setFont(null);
|
||||
|
||||
@@ -2,30 +2,49 @@ package com.gtnewhorizons.angelica.client.rendering;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Used to store the block bounds fields in a thread-safe manner, as instance fields don't work correctly
|
||||
* on multiple threads.
|
||||
*/
|
||||
public class ThreadedBlockData {
|
||||
public static final Thread MAIN_THREAD = Thread.currentThread();
|
||||
public static Thread serverThread;
|
||||
public final class ThreadedBlockData {
|
||||
|
||||
public static final Thread MAIN_THREAD = Thread.currentThread();
|
||||
private static Thread SERVER_THREAD;
|
||||
private static final AtomicInteger CURRENT_SERVER_THREAD_ID = new AtomicInteger();
|
||||
|
||||
private final int serverThreadId = Thread.currentThread() == SERVER_THREAD ? CURRENT_SERVER_THREAD_ID.get() : -1;
|
||||
public double minX, minY, minZ, maxX, maxY, maxZ;
|
||||
public final Thread owner = Thread.currentThread();
|
||||
|
||||
public ThreadedBlockData() {}
|
||||
|
||||
public ThreadedBlockData(ThreadedBlockData other) {
|
||||
this.minX = other.minX;
|
||||
this.minY = other.minY;
|
||||
this.minY = other.minY;
|
||||
this.minZ = other.minZ;
|
||||
this.maxX = other.maxX;
|
||||
this.maxY = other.maxY;
|
||||
this.maxZ = other.maxZ;
|
||||
}
|
||||
|
||||
public boolean isCurrent() {
|
||||
return this.serverThreadId == CURRENT_SERVER_THREAD_ID.get();
|
||||
}
|
||||
|
||||
public static ThreadedBlockData get(Block block) {
|
||||
return ((Getter)block).angelica$getThreadData();
|
||||
return ((Getter) block).angelica$getThreadData();
|
||||
}
|
||||
|
||||
public static void setServerThread(Thread t) {
|
||||
ThreadedBlockData.SERVER_THREAD = t;
|
||||
if (t != null) {
|
||||
CURRENT_SERVER_THREAD_ID.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
||||
public static Thread getServerThread() {
|
||||
return SERVER_THREAD;
|
||||
}
|
||||
|
||||
public interface Getter {
|
||||
|
||||
@@ -8,6 +8,10 @@ public class FontConfig {
|
||||
@Config.DefaultBoolean(false)
|
||||
public static boolean enableCustomFont;
|
||||
|
||||
@Config.Comment({"Draw text with modern Minecraft's font sheets"})
|
||||
@Config.DefaultBoolean(true)
|
||||
public static boolean enableModernFont;
|
||||
|
||||
@Config.Comment("Name of the primary custom font. Best not to set it from here.")
|
||||
@Config.DefaultString("(none)")
|
||||
public static String customFontNamePrimary;
|
||||
|
||||
@@ -32,6 +32,7 @@ import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraft.world.chunk.IChunkProvider;
|
||||
import net.minecraftforge.common.DimensionManager;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.lwjgl.opengl.Display;
|
||||
@@ -75,6 +76,9 @@ public final class FlybyRunner {
|
||||
private boolean jfr;
|
||||
private long recordingId;
|
||||
private final AtomicReference<FlybyRequest> pendingRequest = new AtomicReference<>();
|
||||
private final AtomicReference<String> pendingDimensionPlayer = new AtomicReference<>();
|
||||
private boolean dimensionRequested;
|
||||
private volatile boolean dimensionAbandoned;
|
||||
private FlybyRequest activeRequest;
|
||||
private String[] sceneCommands = NO_COMMANDS;
|
||||
private volatile boolean sceneClearRequested;
|
||||
@@ -197,6 +201,8 @@ public final class FlybyRunner {
|
||||
this.waitForTracy = false;
|
||||
this.waitForFocus = false;
|
||||
this.exitWhenDone = false;
|
||||
this.dimensionRequested = false;
|
||||
this.dimensionAbandoned = false;
|
||||
this.jfr = false;
|
||||
this.armed = false;
|
||||
this.fixedOrigin = null;
|
||||
@@ -263,6 +269,7 @@ public final class FlybyRunner {
|
||||
if (this.waitTicks++ % 100 == 0) LOGGER.info("Flyby waiting for window focus");
|
||||
return;
|
||||
}
|
||||
if (this.awaitingDimension(mc, player)) return;
|
||||
this.begin(mc, player);
|
||||
}
|
||||
case PREPARING -> {
|
||||
@@ -331,6 +338,22 @@ public final class FlybyRunner {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean awaitingDimension(Minecraft mc, EntityClientPlayerMP player) {
|
||||
final Integer dimension = SystemProperties.FLYBY_DIMENSION;
|
||||
if (!this.startedFromProperties || dimension == null || player.dimension == dimension || this.dimensionAbandoned) return false;
|
||||
if (mc.getIntegratedServer() == null || !DimensionManager.isDimensionRegistered(dimension)) {
|
||||
LOGGER.warn("Flyby: {}, staying in dimension {}", mc.getIntegratedServer() == null ? "not singleplayer" : "dimension " + dimension + " is not registered", player.dimension);
|
||||
this.dimensionAbandoned = true;
|
||||
return false;
|
||||
}
|
||||
if (!this.dimensionRequested) {
|
||||
this.dimensionRequested = true;
|
||||
LOGGER.info("Flyby: moving from dimension {} to {}", player.dimension, dimension);
|
||||
this.pendingDimensionPlayer.set(player.getCommandSenderName());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void overridePauseOnLostFocus(Minecraft mc) {
|
||||
if (this.pauseOnLostFocusOverridden) return;
|
||||
this.pauseOnLostFocusSaved = mc.gameSettings.pauseOnLostFocus;
|
||||
@@ -600,11 +623,23 @@ public final class FlybyRunner {
|
||||
public void onServerTick(TickEvent.ServerTickEvent event) {
|
||||
if (event.phase != TickEvent.Phase.END) return;
|
||||
final boolean clear = this.sceneClearRequested;
|
||||
if (!clear && this.pendingRequest.get() == null) return;
|
||||
if (!clear && this.pendingRequest.get() == null && this.pendingDimensionPlayer.get() == null) return;
|
||||
|
||||
final MinecraftServer server = MinecraftServer.getServer();
|
||||
if (server == null) return;
|
||||
|
||||
final String traveller = this.pendingDimensionPlayer.getAndSet(null);
|
||||
if (traveller != null) {
|
||||
this.discardWorldChanges(server);
|
||||
final EntityPlayerMP player = server.getConfigurationManager().func_152612_a(traveller);
|
||||
if (player == null) {
|
||||
LOGGER.warn("Flyby: no server player named '{}', staying in the current dimension", traveller);
|
||||
this.dimensionAbandoned = true;
|
||||
} else {
|
||||
server.getConfigurationManager().transferPlayerToDimension(player, SystemProperties.FLYBY_DIMENSION);
|
||||
}
|
||||
}
|
||||
|
||||
if (clear) {
|
||||
this.sceneClearRequested = false;
|
||||
this.clearScene(server);
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.helpers;
|
||||
|
||||
import static com.gtnewhorizons.angelica.client.font.ColorCodeUtils.FORMATTING_CHAR;
|
||||
|
||||
import com.gtnewhorizons.angelica.api.EyePassRenderer;
|
||||
import net.minecraft.client.renderer.entity.RenderDragon;
|
||||
import net.minecraft.client.renderer.entity.RenderEnderman;
|
||||
import net.minecraft.client.renderer.entity.RenderSpider;
|
||||
@@ -15,7 +16,8 @@ public final class RendererLivingEntityHelper {
|
||||
private RendererLivingEntityHelper() {}
|
||||
|
||||
public static boolean hasEyePass(Object renderer) {
|
||||
return renderer instanceof RenderSpider || renderer instanceof RenderEnderman || renderer instanceof RenderDragon;
|
||||
return renderer instanceof RenderSpider || renderer instanceof RenderEnderman || renderer instanceof RenderDragon
|
||||
|| renderer instanceof EyePassRenderer;
|
||||
}
|
||||
|
||||
public static String getUpsideDownName(EntityLivingBase entity) {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.gtnewhorizons.angelica.iris;
|
||||
|
||||
import com.gtnewhorizons.angelica.utils.AngelicaJar;
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.parsing.IrisFunctions;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/**
|
||||
* Prepares for the first shader pack load on a background thread while the game sits at the main menu.
|
||||
*/
|
||||
public final class ShaderWarmup {
|
||||
private static final Logger LOGGER = LogManager.getLogger("Angelica/ShaderWarmup");
|
||||
private static final String[] PACKAGES = { "net/coderbot/iris/", "kroppeb/stareval/" };
|
||||
private static final String SKIPPED_PACKAGE = "net/coderbot/iris/compat/dh/";
|
||||
|
||||
private static boolean started;
|
||||
|
||||
private ShaderWarmup() {}
|
||||
|
||||
/** Main thread only */
|
||||
public static void start() {
|
||||
if (started) return;
|
||||
started = true;
|
||||
final Runnable readSelectedPack = Iris.prepareSelectedShaderPack();
|
||||
final Thread thread = new Thread(() -> {
|
||||
preloadClasses();
|
||||
if (readSelectedPack != null) readSelectedPack.run();
|
||||
}, "Angelica-Shader-Warmup");
|
||||
thread.setDaemon(true);
|
||||
thread.setPriority(Thread.MIN_PRIORITY);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private static void preloadClasses() {
|
||||
final List<String> names;
|
||||
try {
|
||||
names = listClasses();
|
||||
} catch (Exception e) {
|
||||
LOGGER.debug("Could not list shader classes to preload", e);
|
||||
return;
|
||||
}
|
||||
final ClassLoader loader = Iris.class.getClassLoader();
|
||||
for (String name : names) {
|
||||
try {
|
||||
Class.forName(name, false, loader);
|
||||
} catch (Throwable ignored) {
|
||||
// A class that cannot load here fails the same way when the pipeline reaches it
|
||||
}
|
||||
}
|
||||
try {
|
||||
Class.forName(IrisFunctions.class.getName(), true, loader);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> listClasses() throws Exception {
|
||||
final Path source = AngelicaJar.location();
|
||||
final List<String> names = new ArrayList<>();
|
||||
if (source == null) return names;
|
||||
if (Files.isDirectory(source)) {
|
||||
try (Stream<Path> files = Files.walk(source)) {
|
||||
files.forEach(file -> addIfShaderClass(names, source.relativize(file).toString().replace('\\', '/')));
|
||||
}
|
||||
} else {
|
||||
try (ZipFile jar = new ZipFile(source.toFile())) {
|
||||
final Enumeration<? extends ZipEntry> entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
addIfShaderClass(names, entries.nextElement().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(names);
|
||||
return names;
|
||||
}
|
||||
|
||||
private static void addIfShaderClass(List<String> names, String entry) {
|
||||
if (!entry.endsWith(".class") || entry.startsWith(SKIPPED_PACKAGE)) return;
|
||||
for (String packagePath : PACKAGES) {
|
||||
if (entry.startsWith(packagePath)) {
|
||||
names.add(entry.substring(0, entry.length() - ".class".length()).replace('/', '.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -165,11 +165,19 @@ public class DarkModeFontTransform {
|
||||
RecolorTarget.includeMethodCall(
|
||||
new MethodInfo("micdoodle8.mods.galacticraft.core.client.gui.element.GuiElementCheckbox func_146112_a(Lnet/minecraft/client/Minecraft;II)V # drawButton"),
|
||||
new MethodInfo("net.minecraft.client.gui.FontRenderer func_85187_a(Ljava/lang/String;IIIZ)I # drawString")
|
||||
),
|
||||
RecolorTarget.includeMethodCall(
|
||||
new MethodInfo("micdoodle8.mods.galacticraft.core.client.gui.container.GuiAirLockController func_73863_a(IIF)V # drawScreen"),
|
||||
new MethodInfo("net.minecraft.client.gui.FontRenderer func_78276_b(Ljava/lang/String;III)I # drawString")
|
||||
),
|
||||
// Steve's Factory Manager
|
||||
RecolorTarget.includeMethodCall(
|
||||
new MethodInfo("vswe.stevesfactory.interfaces.GuiBase drawString(Ljava/lang/String;IIFI)V"),
|
||||
new MethodInfo("net.minecraft.client.gui.FontRenderer func_78276_b(Ljava/lang/String;III)I # drawString")
|
||||
),
|
||||
RecolorTarget.includeMethodCall(
|
||||
new MethodInfo("vswe.stevesfactory.interfaces.GuiBase drawSplitString(Ljava/lang/String;IIIFI)V"),
|
||||
new MethodInfo("net.minecraft.client.gui.FontRenderer func_78279_b(Ljava/lang/String;IIII)V # drawSplitString")
|
||||
),
|
||||
// Steve's Carts 2
|
||||
RecolorTarget.includeMethodCall(
|
||||
@@ -252,6 +260,11 @@ public class DarkModeFontTransform {
|
||||
new MethodInfo("com.cleanroommc.modularui.widgets.RichTextWidget draw(Lcom/cleanroommc/modularui/screen/viewport/ModularGuiContext;Lcom/cleanroommc/modularui/theme/WidgetThemeEntry;)V"),
|
||||
new MethodInfo("com.cleanroommc.modularui.drawable.text.RichText drawAtZeroPadded(Lcom/cleanroommc/modularui/screen/viewport/GuiContext;Lcom/cleanroommc/modularui/widget/sizer/Area;Lcom/cleanroommc/modularui/theme/WidgetTheme;)V")
|
||||
),
|
||||
// Hardcore Ender Expansion
|
||||
RecolorTarget.includeMethodCall(
|
||||
new MethodInfo("chylex.hee.gui.GuiTransportBeacon func_73863_a(IIF)V # drawScreen"),
|
||||
new MethodInfo("net.minecraft.client.gui.FontRenderer func_78276_b(Ljava/lang/String;III)I # drawString")
|
||||
),
|
||||
//endregion
|
||||
//region Exclude Methods
|
||||
// Applied Energistics 2
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.gtnewhorizons.angelica.debug.DebugKeyHandler;
|
||||
import com.gtnewhorizons.angelica.rendering.culling.GpuCulling;
|
||||
import static com.gtnewhorizons.angelica.AngelicaMod.MOD_ID;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.Locale;
|
||||
@@ -39,9 +38,9 @@ import com.google.common.base.Objects;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.loading.ModelRegistry;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.vao.VAOManager;
|
||||
import com.gtnewhorizons.angelica.client.font.DarkModeUtils;
|
||||
import com.gtnewhorizons.angelica.client.font.FontProviderBitmap;
|
||||
import com.gtnewhorizons.angelica.commands.AngelicaCommand;
|
||||
import com.gtnewhorizons.angelica.AngelicaMod;
|
||||
import com.gtnewhorizons.angelica.Tags;
|
||||
import com.gtnewhorizons.angelica.common.BlockError;
|
||||
import com.gtnewhorizons.angelica.common.BlockIsbrhTest;
|
||||
import com.gtnewhorizons.angelica.compat.ModStatus;
|
||||
@@ -65,9 +64,9 @@ import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.BackendStartGuard;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VSyncMode;
|
||||
import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
|
||||
import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache;
|
||||
import com.gtnewhorizons.angelica.hudcaching.HUDCaching;
|
||||
import com.gtnewhorizons.angelica.iris.IrisGLSMBridge;
|
||||
import com.gtnewhorizons.angelica.iris.ShaderWarmup;
|
||||
import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt;
|
||||
import com.gtnewhorizons.angelica.render.CloudRenderer;
|
||||
import com.gtnewhorizons.angelica.render.EmissiveTextureAutoloader;
|
||||
@@ -84,6 +83,7 @@ import com.gtnewhorizons.angelica.rendering.celeritas.threading.ThreadedChunkTas
|
||||
import com.gtnewhorizons.angelica.rendering.items.BlockRenderListManager;
|
||||
import com.gtnewhorizons.angelica.rendering.items.ItemRenderListManager;
|
||||
import com.gtnewhorizons.angelica.rendering.tesr.TesrLifecycle;
|
||||
import com.gtnewhorizons.angelica.utils.AngelicaJar;
|
||||
import com.gtnewhorizons.angelica.utils.AnimationMode;
|
||||
import com.gtnewhorizons.angelica.utils.ManagedEnum;
|
||||
import com.gtnewhorizons.angelica.zoom.Zoom;
|
||||
@@ -149,11 +149,7 @@ public final class ClientProxy extends CommonProxy {
|
||||
BlockIsbrhTest.renderId = RenderingRegistry.getNextAvailableRenderId();
|
||||
RenderingRegistry.registerBlockHandler(BlockIsbrhTest.renderId, new IsbrhTestRenderer());
|
||||
}
|
||||
final File source = event.getSourceFile();
|
||||
if (source != null && source.isFile()) {
|
||||
ShaderDiskCache.configure(Minecraft.getMinecraft().mcDataDir.toPath().resolve("angelica").resolve("shadercache"),
|
||||
Tags.VERSION + "|" + source.length() + "|" + source.lastModified());
|
||||
}
|
||||
AngelicaJar.configureShaderDiskCache(event.getSourceFile());
|
||||
if (AngelicaConfig.enableIris) {
|
||||
IrisGLSMBridge.installImmediateExtendedHandler();
|
||||
Iris.warmupShaderTransforms();
|
||||
@@ -211,6 +207,7 @@ public final class ClientProxy extends CommonProxy {
|
||||
ItemRenderListManager.registerReloadListener();
|
||||
BlockRenderListManager.registerReloadListener();
|
||||
}
|
||||
FontProviderBitmap.registerReloadListener();
|
||||
|
||||
// Debug tooling
|
||||
if (SystemProperties.debugTooling() || Tracy.ENABLED) {
|
||||
@@ -391,6 +388,7 @@ public final class ClientProxy extends CommonProxy {
|
||||
if (event.gui instanceof GuiMainMenu && gameStartTime == -1) {
|
||||
gameStartTime = ManagementFactory.getRuntimeMXBean().getUptime() / 1000f;
|
||||
LOGGER.info("The game loaded in {} seconds.", gameStartTime);
|
||||
if (AngelicaConfig.enableIris) ShaderWarmup.start();
|
||||
}
|
||||
|
||||
// force reset zoom when a GUI is opened
|
||||
|
||||
@@ -98,6 +98,10 @@ public class CloudRenderer implements IResourceManagerReloadListener {
|
||||
return GalaxySpaceClouds.supports(world, handler);
|
||||
}
|
||||
|
||||
public void invalidateWorld() {
|
||||
this.world = null;
|
||||
}
|
||||
|
||||
public void checkSettings() {
|
||||
if (world != mc.theWorld) {
|
||||
clearLayers();
|
||||
|
||||
+2
@@ -6,6 +6,7 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import com.gtnewhorizons.angelica.compat.bop.FogBiomeCache;
|
||||
import com.gtnewhorizons.angelica.render.CloudRenderer;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.WorldClient;
|
||||
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
|
||||
@@ -175,6 +176,7 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer<WorldClient, Ang
|
||||
GpuCulling.onWorldUnload();
|
||||
FfpExtendedAttribs.reset();
|
||||
FogBiomeCache.invalidate();
|
||||
CloudRenderer.getCloudRenderer().invalidateWorld();
|
||||
super.unloadWorld();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.gtnewhorizons.angelica.utils;
|
||||
|
||||
import com.gtnewhorizons.angelica.Tags;
|
||||
import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public final class AngelicaJar {
|
||||
private AngelicaJar() {}
|
||||
|
||||
/** The jar or classes directory Angelica was loaded from, or null if something really went wrong. */
|
||||
@Nullable
|
||||
public static Path location() {
|
||||
try {
|
||||
String location = AngelicaJar.class.getProtectionDomain().getCodeSource().getLocation().toString();
|
||||
if (location.startsWith("jar:")) {
|
||||
final int separator = location.indexOf("!/");
|
||||
location = location.substring("jar:".length(), separator < 0 ? location.length() : separator);
|
||||
}
|
||||
return Paths.get(new URI(location));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void configureShaderDiskCache(@Nullable File jar) {
|
||||
if (jar == null || !jar.isFile()) return;
|
||||
ShaderDiskCache.configure(Minecraft.getMinecraft().mcDataDir.toPath().resolve("angelica").resolve("shadercache"),
|
||||
Tags.VERSION + "|" + jar.length() + "|" + jar.lastModified());
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import com.gtnewhorizons.angelica.proxy.ClientProxy;
|
||||
import com.gtnewhorizons.angelica.rendering.StateAwareTessellator;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHolder;
|
||||
import com.gtnewhorizons.angelica.sdlgpu.SDLGPUGate;
|
||||
import com.gtnewhorizons.angelica.utils.AngelicaJar;
|
||||
import cpw.mods.fml.client.registry.ClientRegistry;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.InputEvent;
|
||||
@@ -29,6 +30,7 @@ import net.coderbot.iris.celeritas.IrisCeleritasShaderProvider;
|
||||
import net.coderbot.iris.compat.dh.DHCompat;
|
||||
import net.coderbot.iris.config.IrisConfig;
|
||||
import net.coderbot.iris.gbuffer_overrides.matching.InputAvailability;
|
||||
import net.coderbot.iris.gl.program.RetainedPrograms;
|
||||
import net.coderbot.iris.gl.shader.StandardMacros;
|
||||
import net.coderbot.iris.gui.screen.ShaderPackScreen;
|
||||
import net.coderbot.iris.pipeline.DeferredWorldRenderingPipeline;
|
||||
@@ -38,6 +40,7 @@ import net.coderbot.iris.pipeline.WorldRenderingPipeline;
|
||||
import net.coderbot.iris.pipeline.transform.ShaderTransformer;
|
||||
import net.coderbot.iris.pipeline.transform.TransformPatcher;
|
||||
import net.coderbot.iris.shaderpack.OptionalBoolean;
|
||||
import net.coderbot.iris.shaderpack.IdMap;
|
||||
import net.coderbot.iris.shaderpack.ProgramSet;
|
||||
import net.coderbot.iris.shaderpack.ShaderPack;
|
||||
import net.coderbot.iris.shaderpack.StringPair;
|
||||
@@ -67,19 +70,30 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystemLoopException;
|
||||
import java.nio.file.FileSystemNotFoundException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipException;
|
||||
@@ -107,6 +121,7 @@ public class Iris {
|
||||
@Getter
|
||||
private static String currentPackName;
|
||||
private static String currentPackSettings;
|
||||
private static String currentPackState;
|
||||
@Getter
|
||||
private static boolean initialized;
|
||||
@Getter
|
||||
@@ -288,6 +303,10 @@ public class Iris {
|
||||
|
||||
IrisShaderProviderHolder.setProvider(new IrisCeleritasShaderProvider());
|
||||
|
||||
// The pack loads below, before mod preInit would otherwise set up the cache
|
||||
final Path angelicaJar = AngelicaJar.location();
|
||||
AngelicaJar.configureShaderDiskCache(angelicaJar != null ? angelicaJar.toFile() : null);
|
||||
|
||||
// Warm up the threadpool so shader transformations are faster when we need them
|
||||
ShaderTransformExecutor.warmup();
|
||||
|
||||
@@ -485,9 +504,7 @@ public class Iris {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
final Map<String, String> changedConfigs = tryReadConfigProperties(shaderPackConfigTxt).map(properties -> (Map<String, String>) (Map<?, ?>) properties)
|
||||
.orElse(new HashMap<>());
|
||||
final Map<String, String> changedConfigs = readChangedConfigs(shaderPackConfigTxt);
|
||||
|
||||
changedConfigs.putAll(shaderPackOptionQueue);
|
||||
clearShaderPackOptionQueue();
|
||||
@@ -499,7 +516,12 @@ public class Iris {
|
||||
|
||||
try {
|
||||
final Iterable<StringPair> environmentDefines = StandardMacros.createStandardEnvironmentDefines();
|
||||
currentPack = new ShaderPack(shaderPackPath, changedConfigs, environmentDefines);
|
||||
final String packState = describeSettings(name, shaderPackRoot, toProperties(changedConfigs), environmentDefines, false);
|
||||
final ShaderPack prepared = takePreparedPack(name, packState);
|
||||
currentPack = prepared != null ? prepared : new ShaderPack(shaderPackPath, changedConfigs, environmentDefines);
|
||||
currentPackState = packState;
|
||||
RetainedPrograms.useConfiguration(packState);
|
||||
currentPack.activate();
|
||||
|
||||
final MutableOptionValues changedConfigsValues = currentPack.getShaderPackOptions().getOptionValues().mutableCopy();
|
||||
|
||||
@@ -509,7 +531,7 @@ public class Iris {
|
||||
changedConfigsValues.getStringValues().forEach(configsToSave::setProperty);
|
||||
|
||||
tryUpdateConfigPropertiesFile(shaderPackConfigTxt, configsToSave);
|
||||
currentPackSettings = describeSettings(name, shaderPackRoot, configsToSave, environmentDefines);
|
||||
currentPackSettings = irisConfig.shouldSaveCompiledShaders() ? describeSettings(name, shaderPackRoot, configsToSave, environmentDefines, true) : null;
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to load the shaderpack \"{}\"!", name);
|
||||
logger.error("", e);
|
||||
@@ -526,10 +548,83 @@ public class Iris {
|
||||
return true;
|
||||
}
|
||||
|
||||
private record PreparedPack(String settings, ShaderPack pack) {}
|
||||
|
||||
private static volatile SoftReference<PreparedPack> preparedPack;
|
||||
|
||||
@Nullable
|
||||
public static Runnable prepareSelectedShaderPack() {
|
||||
if (irisConfig == null || irisConfig.areShadersEnabled() || currentPack != null) return null;
|
||||
final Optional<String> name = irisConfig.getShaderPackName();
|
||||
if (name.isEmpty()) return null;
|
||||
final List<StringPair> environmentDefines = new ArrayList<>();
|
||||
StandardMacros.createStandardEnvironmentDefines().forEach(environmentDefines::add);
|
||||
return () -> {
|
||||
try {
|
||||
final PreparedPack prepared = readPreparedPack(name.get(), environmentDefines);
|
||||
if (prepared != null) preparedPack = new SoftReference<>(prepared);
|
||||
} catch (Exception e) {
|
||||
logger.debug("Could not read shaderpack \"{}\" ahead of time", name.get(), e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PreparedPack readPreparedPack(String name, List<StringPair> environmentDefines) throws IOException {
|
||||
final Path shaderPackRoot = getShaderpacksDirectory().resolve(name);
|
||||
final Map<String, String> changedConfigs = readChangedConfigs(getShaderpacksDirectory().resolve(name + ".txt"));
|
||||
final String settings = describeSettings(name, shaderPackRoot, toProperties(changedConfigs), environmentDefines, false);
|
||||
if (settings == null) return null;
|
||||
|
||||
if (Files.isDirectory(shaderPackRoot)) {
|
||||
return new PreparedPack(settings, parsePack(shaderPackRoot.resolve("shaders"), changedConfigs, environmentDefines));
|
||||
}
|
||||
try (FileSystem zipSystem = FileSystems.newFileSystem(shaderPackRoot, Iris.class.getClassLoader())) {
|
||||
final Optional<Path> shadersDirectory = findShadersDirectory(zipSystem);
|
||||
if (shadersDirectory.isEmpty()) return null;
|
||||
return new PreparedPack(settings, parsePack(shadersDirectory.get(), changedConfigs, environmentDefines));
|
||||
}
|
||||
}
|
||||
|
||||
private static ShaderPack parsePack(Path shadersDirectory, Map<String, String> changedConfigs, List<StringPair> environmentDefines) throws IOException {
|
||||
final ShaderPack pack = new ShaderPack(shadersDirectory, changedConfigs, environmentDefines);
|
||||
// Most worlds open in the overworld; other dimensions build their program sets on demand as usual.
|
||||
// Considered having tracking of what dim a user is in but with it being so fast now, not sure if
|
||||
// it's worth saving the 50-80ms "time waste" on first load, especially when we're already pre-loading at game init.
|
||||
pack.getProgramSet("Overworld");
|
||||
return pack;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ShaderPack takePreparedPack(String name, @Nullable String packState) {
|
||||
final SoftReference<PreparedPack> reference = preparedPack;
|
||||
preparedPack = null;
|
||||
final PreparedPack prepared = reference != null ? reference.get() : null;
|
||||
if (prepared == null || !prepared.settings().equals(packState)) return null;
|
||||
logger.info("Reusing shaderpack \"{}\" read earlier, nothing it was read from has changed", name);
|
||||
return prepared.pack();
|
||||
}
|
||||
|
||||
private static Map<String, String> readChangedConfigs(Path shaderPackConfigTxt) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Map<String, String> changedConfigs = tryReadConfigProperties(shaderPackConfigTxt).map(properties -> (Map<String, String>) (Map<?, ?>) properties)
|
||||
.orElse(new HashMap<>());
|
||||
return new HashMap<>(changedConfigs);
|
||||
}
|
||||
|
||||
private static Properties toProperties(Map<String, String> values) {
|
||||
final Properties properties = new Properties();
|
||||
properties.putAll(values);
|
||||
return properties;
|
||||
}
|
||||
|
||||
private static Optional<Path> loadExternalZipShaderpack(Path shaderpackPath) throws IOException {
|
||||
final FileSystem zipSystem = FileSystems.newFileSystem(shaderpackPath, Iris.class.getClassLoader());
|
||||
zipFileSystem = zipSystem;
|
||||
return findShadersDirectory(zipSystem);
|
||||
}
|
||||
|
||||
private static Optional<Path> findShadersDirectory(FileSystem zipSystem) throws IOException {
|
||||
// Should only be one root directory for a zip shaderpack
|
||||
final Path root = zipSystem.getRootDirectories().iterator().next();
|
||||
|
||||
@@ -552,6 +647,7 @@ public class Iris {
|
||||
|
||||
private static void setShadersDisabled() {
|
||||
currentPack = null;
|
||||
currentPackState = null;
|
||||
currentPackSettings = null;
|
||||
ProgramBinaryCache.usePack(null);
|
||||
fallback = false;
|
||||
@@ -710,7 +806,11 @@ public class Iris {
|
||||
*/
|
||||
private static void destroyEverything() {
|
||||
awaitProgramSetWarmup();
|
||||
if (currentPack != null && currentPackState != null) {
|
||||
preparedPack = new SoftReference<>(new PreparedPack(currentPackState, currentPack));
|
||||
}
|
||||
currentPack = null;
|
||||
currentPackState = null;
|
||||
currentPackSettings = null;
|
||||
ProgramBinaryCache.usePack(null);
|
||||
|
||||
@@ -809,6 +909,8 @@ public class Iris {
|
||||
} catch (Exception e) {
|
||||
everyFolderCompiled = false;
|
||||
logger.error("Failed to compile shader folder '{}' for saving", folder, e);
|
||||
} finally {
|
||||
RetainedPrograms.afterPipelineBuilt();
|
||||
}
|
||||
}
|
||||
loopFinished = true;
|
||||
@@ -818,8 +920,65 @@ public class Iris {
|
||||
}
|
||||
}
|
||||
|
||||
private record HashedFile(long size, long modified, String hash) {}
|
||||
|
||||
private static final Map<Path, HashedFile> contentHashes = new ConcurrentHashMap<>();
|
||||
|
||||
private static String contentHash(Path file, BasicFileAttributes attributes) throws IOException {
|
||||
final long size = attributes.size();
|
||||
final long modified = attributes.lastModifiedTime().toMillis();
|
||||
final HashedFile known = contentHashes.get(file);
|
||||
if (known != null && known.size() == size && known.modified() == modified) return known.hash();
|
||||
final String hash;
|
||||
try {
|
||||
hash = Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(file)));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
contentHashes.put(file, new HashedFile(size, modified, hash));
|
||||
return hash;
|
||||
}
|
||||
|
||||
private record VisitedFolder(Path path, @Nullable Object key) {}
|
||||
|
||||
private static Map<String, String> describeFolder(Path root, VisitedFolder folder, List<VisitedFolder> ancestors, boolean byContent) {
|
||||
final Map<String, String> files = new HashMap<>();
|
||||
final List<Supplier<Map<String, String>>> subfolders = new ArrayList<>();
|
||||
final List<VisitedFolder> lineage = new ArrayList<>(ancestors);
|
||||
lineage.add(folder);
|
||||
try (DirectoryStream<Path> entries = Files.newDirectoryStream(folder.path())) {
|
||||
for (Path entry : entries) {
|
||||
BasicFileAttributes attributes;
|
||||
try {
|
||||
attributes = Files.readAttributes(entry, BasicFileAttributes.class);
|
||||
} catch (IOException e) {
|
||||
attributes = Files.readAttributes(entry, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||
}
|
||||
if (attributes.isDirectory()) {
|
||||
final VisitedFolder subfolder = new VisitedFolder(entry, attributes.fileKey());
|
||||
for (VisitedFolder ancestor : lineage) {
|
||||
final boolean same = subfolder.key() != null && ancestor.key() != null
|
||||
? subfolder.key().equals(ancestor.key())
|
||||
: Files.isSameFile(ancestor.path(), entry);
|
||||
if (same) throw new FileSystemLoopException(entry.toString());
|
||||
}
|
||||
subfolders.add(() -> describeFolder(root, subfolder, lineage, byContent));
|
||||
} else if (attributes.isRegularFile()) {
|
||||
files.put(root.relativize(entry).toString(),
|
||||
byContent ? contentHash(entry, attributes) : attributes.size() + "|" + attributes.lastModifiedTime().toMillis());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
for (Map<String, String> nested : AngelicaWorkers.invokeAll(subfolders)) {
|
||||
files.putAll(nested);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String describeSettings(String packName, Path packRoot, Properties options, Iterable<StringPair> environmentDefines) {
|
||||
private static String describeSettings(String packName, Path packRoot, Properties options, Iterable<StringPair> environmentDefines, boolean byContent) {
|
||||
final StringBuilder settings = new StringBuilder(packName).append('\n');
|
||||
if (Files.isRegularFile(packRoot)) {
|
||||
try {
|
||||
@@ -828,18 +987,15 @@ public class Iris {
|
||||
return null;
|
||||
}
|
||||
} else if (Files.isDirectory(packRoot)) {
|
||||
try (Stream<Path> files = Files.walk(packRoot)) {
|
||||
files.filter(Files::isRegularFile).sorted().forEach(file -> {
|
||||
try {
|
||||
settings.append(packRoot.relativize(file)).append('|').append(Files.size(file)).append('|')
|
||||
.append(Files.getLastModifiedTime(file).toMillis()).append('\n');
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
});
|
||||
final TreeMap<String, String> files = new TreeMap<>();
|
||||
try {
|
||||
// One stat per file; this runs on the render thread when shaders turn on, so folders are read in parallel
|
||||
final Object rootKey = Files.readAttributes(packRoot, BasicFileAttributes.class).fileKey();
|
||||
files.putAll(describeFolder(packRoot, new VisitedFolder(packRoot, rootKey), List.of(), byContent));
|
||||
} catch (IOException | UncheckedIOException e) {
|
||||
return null;
|
||||
}
|
||||
files.forEach((file, state) -> settings.append(file).append('|').append(state).append('\n'));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -848,6 +1004,7 @@ public class Iris {
|
||||
for (StringPair define : environmentDefines) {
|
||||
settings.append('#').append(define.getKey()).append(' ').append(define.getValue()).append('\n');
|
||||
}
|
||||
settings.append("modernFallbackMcVersion=").append(IdMap.modernFallbackMcVersion()).append('\n');
|
||||
return settings.toString();
|
||||
}
|
||||
|
||||
@@ -870,7 +1027,12 @@ public class Iris {
|
||||
ShaderTransformExecutor.prepare();
|
||||
PerFrameUniformBlockHarvester.harvest(programs);
|
||||
long startTime = System.nanoTime();
|
||||
WorldRenderingPipeline pipeline = new DeferredWorldRenderingPipeline(programs);
|
||||
final WorldRenderingPipeline pipeline;
|
||||
try {
|
||||
pipeline = new DeferredWorldRenderingPipeline(programs);
|
||||
} finally {
|
||||
RetainedPrograms.afterPipelineBuilt();
|
||||
}
|
||||
long endTime = System.nanoTime();
|
||||
logger.info("[Load #{}] Total shaderpack load time for '{}' in dimension '{}': {} ms", shaderPackLoadId, currentPackName, dimensionName, String.format("%.1f", (endTime - startTime) / 1_000_000.0));
|
||||
return pipeline;
|
||||
|
||||
@@ -10,6 +10,7 @@ public abstract class GlResource {
|
||||
}
|
||||
|
||||
public final void destroy() {
|
||||
if (!isValid) return;
|
||||
destroyInternal();
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import net.coderbot.iris.gl.GlResource;
|
||||
import net.coderbot.iris.gl.blending.DepthColorStorage;
|
||||
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
|
||||
import net.coderbot.iris.shaderpack.FilledIndirectPointer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3i;
|
||||
import org.lwjgl.BufferUtils;
|
||||
@@ -27,9 +28,14 @@ public final class ComputeProgram extends GlResource {
|
||||
private float cachedHeight;
|
||||
private Vector3i cachedWorkGroups;
|
||||
|
||||
ComputeProgram(int program, ProgramUniforms uniforms, ProgramSamplers samplers, ProgramImages images) {
|
||||
@Nullable
|
||||
private final RetainedPrograms.Sources sources;
|
||||
|
||||
ComputeProgram(int program, @Nullable RetainedPrograms.Sources sources, ProgramUniforms uniforms, ProgramSamplers samplers, ProgramImages images) {
|
||||
super(program);
|
||||
|
||||
this.sources = sources;
|
||||
|
||||
localSizeBuffer = BufferUtils.createIntBuffer(3);
|
||||
RenderSystem.getProgramiv(program, GL43.GL_COMPUTE_WORK_GROUP_SIZE, localSizeBuffer);
|
||||
this.uniforms = uniforms;
|
||||
@@ -93,7 +99,11 @@ public final class ComputeProgram extends GlResource {
|
||||
@Override
|
||||
public void destroyInternal() {
|
||||
DepthColorStorage.unregisterOwnedProgram(getGlId());
|
||||
GLStateManager.glDeleteProgram(getGlId());
|
||||
if (sources != null) {
|
||||
RetainedPrograms.retain(sources, getGlId());
|
||||
} else {
|
||||
GLStateManager.glDeleteProgram(getGlId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ import net.coderbot.iris.gl.GlResource;
|
||||
import net.coderbot.iris.gl.blending.DepthColorStorage;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.glsm.RenderSystem;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.lwjgl.opengl.GL42;
|
||||
import org.lwjgl.opengl.GL43;
|
||||
|
||||
@@ -25,9 +26,14 @@ public final class Program extends GlResource {
|
||||
@Getter
|
||||
private final ProgramImages images;
|
||||
|
||||
Program(int program, ProgramUniforms uniforms, ProgramSamplers samplers, ProgramImages images) {
|
||||
@Nullable
|
||||
private final RetainedPrograms.Sources sources;
|
||||
|
||||
Program(int program, @Nullable RetainedPrograms.Sources sources, ProgramUniforms uniforms, ProgramSamplers samplers, ProgramImages images) {
|
||||
super(program);
|
||||
|
||||
this.sources = sources;
|
||||
|
||||
this.uniforms = uniforms;
|
||||
this.samplers = samplers;
|
||||
this.images = images;
|
||||
@@ -63,7 +69,11 @@ public final class Program extends GlResource {
|
||||
@Override
|
||||
public void destroyInternal() {
|
||||
DepthColorStorage.unregisterOwnedProgram(getGlId());
|
||||
GLStateManager.glDeleteProgram(getGlId());
|
||||
if (sources != null) {
|
||||
RetainedPrograms.retain(sources, getGlId());
|
||||
} else {
|
||||
GLStateManager.glDeleteProgram(getGlId());
|
||||
}
|
||||
}
|
||||
|
||||
public int getProgramId() {
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.function.IntSupplier;
|
||||
|
||||
public class ProgramBuilder extends ProgramUniforms.Builder implements SamplerHolder, ImageHolder {
|
||||
private final int program;
|
||||
private RetainedPrograms.Sources sources;
|
||||
private final ProgramSamplers.Builder samplers;
|
||||
private final ProgramImages.Builder images;
|
||||
|
||||
@@ -44,17 +45,29 @@ public class ProgramBuilder extends ProgramUniforms.Builder implements SamplerHo
|
||||
public static ProgramBuilder begin(String name, @Nullable String vertexSource, @Nullable String geometrySource,
|
||||
@Nullable String tessControlSource, @Nullable String tessEvalSource,
|
||||
@Nullable String fragmentSource, ImmutableSet<Integer> reservedTextureUnits) {
|
||||
final ProgramBinaryCache.Key cacheKey = ProgramCreator.cacheKey();
|
||||
final RetainedPrograms.Sources sources = new RetainedPrograms.Sources(vertexSource, geometrySource, tessControlSource, tessEvalSource, fragmentSource, null);
|
||||
final int retained = RetainedPrograms.take(sources);
|
||||
final ProgramBinaryCache.Key cacheKey = retained != 0 && !ProgramBinaryCache.isFullCompileRunning() ? null : ProgramCreator.cacheKey();
|
||||
if (cacheKey != null) {
|
||||
cacheKey.stage(ShaderType.VERTEX.id, vertexSource);
|
||||
if (geometrySource != null) cacheKey.stage(ShaderType.GEOMETRY.id, geometrySource);
|
||||
if (tessControlSource != null) cacheKey.stage(ShaderType.TESSELATION_CONTROL.id, tessControlSource);
|
||||
if (tessEvalSource != null) cacheKey.stage(ShaderType.TESSELATION_EVAL.id, tessEvalSource);
|
||||
cacheKey.stage(ShaderType.FRAGMENT.id, fragmentSource);
|
||||
}
|
||||
if (retained != 0) {
|
||||
if (cacheKey == null || ProgramBinaryCache.saveRetained(cacheKey, retained)) {
|
||||
initializeFallbackUniforms(retained);
|
||||
return new ProgramBuilder(name, retained, reservedTextureUnits).retainAs(sources);
|
||||
}
|
||||
GLStateManager.glDeleteProgram(retained);
|
||||
}
|
||||
|
||||
if (cacheKey != null) {
|
||||
final int saved = ProgramCreator.load(name, cacheKey);
|
||||
if (saved != 0) {
|
||||
initializeFallbackUniforms(saved);
|
||||
return new ProgramBuilder(name, saved, reservedTextureUnits);
|
||||
return new ProgramBuilder(name, saved, reservedTextureUnits).retainAs(sources);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +91,7 @@ public class ProgramBuilder extends ProgramUniforms.Builder implements SamplerHo
|
||||
shader.destroy();
|
||||
}
|
||||
|
||||
return new ProgramBuilder(name, programId, reservedTextureUnits);
|
||||
return new ProgramBuilder(name, programId, reservedTextureUnits).retainAs(sources);
|
||||
}
|
||||
|
||||
/** Initialize alpha and color uniforms to neutral values until the first pass update. */
|
||||
@@ -122,12 +135,23 @@ public class ProgramBuilder extends ProgramUniforms.Builder implements SamplerHo
|
||||
throw new IllegalStateException("This PC does not support compute shaders, but it's attempting to be used???");
|
||||
}
|
||||
|
||||
final ProgramBinaryCache.Key cacheKey = ProgramCreator.cacheKey();
|
||||
final RetainedPrograms.Sources sources = new RetainedPrograms.Sources(null, null, null, null, null, source);
|
||||
final int retained = RetainedPrograms.take(sources);
|
||||
final ProgramBinaryCache.Key cacheKey = retained != 0 && !ProgramBinaryCache.isFullCompileRunning() ? null : ProgramCreator.cacheKey();
|
||||
if (cacheKey != null) {
|
||||
cacheKey.stage(ShaderType.COMPUTE.id, source);
|
||||
}
|
||||
if (retained != 0) {
|
||||
if (cacheKey == null || ProgramBinaryCache.saveRetained(cacheKey, retained)) {
|
||||
return new ProgramBuilder(name, retained, reservedTextureUnits).retainAs(sources);
|
||||
}
|
||||
GLStateManager.glDeleteProgram(retained);
|
||||
}
|
||||
|
||||
if (cacheKey != null) {
|
||||
final int saved = ProgramCreator.load(name, cacheKey);
|
||||
if (saved != 0) {
|
||||
return new ProgramBuilder(name, saved, reservedTextureUnits);
|
||||
return new ProgramBuilder(name, saved, reservedTextureUnits).retainAs(sources);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,15 +161,20 @@ public class ProgramBuilder extends ProgramUniforms.Builder implements SamplerHo
|
||||
|
||||
compute.destroy();
|
||||
|
||||
return new ProgramBuilder(name, programId, reservedTextureUnits);
|
||||
return new ProgramBuilder(name, programId, reservedTextureUnits).retainAs(sources);
|
||||
}
|
||||
|
||||
private ProgramBuilder retainAs(RetainedPrograms.Sources sources) {
|
||||
this.sources = sources;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Program build() {
|
||||
return new Program(program, super.buildUniforms(), this.samplers.build(), this.images.build());
|
||||
return new Program(program, sources, super.buildUniforms(), this.samplers.build(), this.images.build());
|
||||
}
|
||||
|
||||
public ComputeProgram buildCompute() {
|
||||
return new ComputeProgram(program, super.buildUniforms(), this.samplers.build(), this.images.build());
|
||||
return new ComputeProgram(program, sources, super.buildUniforms(), this.samplers.build(), this.images.build());
|
||||
}
|
||||
|
||||
private static GlShader buildShader(ShaderType shaderType, String name, @Nullable String source) {
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package net.coderbot.iris.gl.program;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Linked programs kept from destroyed pipelines. Turning shaders off and on, reloading an unchanged pack, or
|
||||
* travelling between two dimensions rebuilds programs from identical sources; taking them from here skips compile and
|
||||
* link.
|
||||
*/
|
||||
public final class RetainedPrograms {
|
||||
record Sources(@Nullable String vertex, @Nullable String geometry, @Nullable String tessControl,
|
||||
@Nullable String tessEval, @Nullable String fragment, @Nullable String compute) {}
|
||||
|
||||
private static Map<Sources, ArrayDeque<Integer>> recent = new HashMap<>();
|
||||
private static Map<Sources, ArrayDeque<Integer>> older = new HashMap<>();
|
||||
private static String configuration;
|
||||
|
||||
private RetainedPrograms() {}
|
||||
|
||||
public static void useConfiguration(@Nullable String settings) {
|
||||
if (settings == null || !settings.equals(configuration)) {
|
||||
afterPipelineBuilt();
|
||||
afterPipelineBuilt();
|
||||
}
|
||||
configuration = settings;
|
||||
}
|
||||
|
||||
static void retain(Sources sources, int program) {
|
||||
recent.computeIfAbsent(sources, key -> new ArrayDeque<>()).add(program);
|
||||
}
|
||||
|
||||
static int take(Sources sources) {
|
||||
final int program = take(recent, sources);
|
||||
return program != 0 ? program : take(older, sources);
|
||||
}
|
||||
|
||||
private static int take(Map<Sources, ArrayDeque<Integer>> pool, Sources sources) {
|
||||
if (pool.isEmpty()) return 0;
|
||||
final ArrayDeque<Integer> programs = pool.get(sources);
|
||||
if (programs == null) return 0;
|
||||
final int program = programs.poll();
|
||||
if (programs.isEmpty()) pool.remove(sources);
|
||||
return program;
|
||||
}
|
||||
|
||||
public static void afterPipelineBuilt() {
|
||||
for (ArrayDeque<Integer> programs : older.values()) {
|
||||
for (int program : programs) {
|
||||
GLStateManager.glDeleteProgram(program);
|
||||
}
|
||||
}
|
||||
older = recent;
|
||||
recent = new HashMap<>();
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StandardMacros {
|
||||
@@ -42,21 +44,24 @@ public class StandardMacros {
|
||||
defines.add(new StringPair(key, value));
|
||||
}
|
||||
|
||||
private static final Pattern ANGELICA_VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(?:-(?:alpha|beta|pre|rc)(\\d{1,3}))?");
|
||||
|
||||
private static String makeAngelicaVersion()
|
||||
{
|
||||
String[] parts = Tags.VERSION.split("[.-]");
|
||||
int major = Integer.parseInt(parts[0]);
|
||||
int minor = Integer.parseInt(parts[1]);
|
||||
int patch = Integer.parseInt(parts[2]);
|
||||
int sub = 0;
|
||||
return formatAngelicaVersion(Tags.VERSION);
|
||||
}
|
||||
|
||||
// Handle optional prerelease (like beta62)
|
||||
if (parts.length > 3) {
|
||||
String pre = parts[3];
|
||||
String num = pre.replaceAll("\\D+", ""); // remove all non-digits
|
||||
if (!num.isEmpty())
|
||||
sub = Integer.parseInt(num);
|
||||
static String formatAngelicaVersion(String raw)
|
||||
{
|
||||
final Matcher matcher = ANGELICA_VERSION_PATTERN.matcher(raw);
|
||||
if (!matcher.find()) {
|
||||
Iris.logger.warn("Could not parse Angelica version \"{}\"; defining ANGELICA_VERSION as 99999999", raw);
|
||||
return "99999999";
|
||||
}
|
||||
final int major = Integer.parseInt(matcher.group(1));
|
||||
final int minor = Integer.parseInt(matcher.group(2));
|
||||
final int patch = Integer.parseInt(matcher.group(3));
|
||||
final int sub = matcher.group(4) == null ? 0 : Integer.parseInt(matcher.group(4));
|
||||
return String.format("%d%02d%02d%03d", major, minor, patch, sub);
|
||||
}
|
||||
|
||||
|
||||
@@ -181,6 +181,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
|
||||
// Custom images and SSBOs
|
||||
private final Set<GlImage> customImages;
|
||||
private final VertexWriteReplays vertexWriteReplays = new VertexWriteReplays();
|
||||
private final GlImage[] imagesToClear;
|
||||
@Nullable
|
||||
private final ShaderStorageBufferHolder ssboHolder;
|
||||
@@ -543,26 +544,12 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
|
||||
this.shadowComputes = createShadowComputes(programs.getShadowCompute());
|
||||
|
||||
final Map<SharedProgramKey, BuiltProgram> builtPrograms = new HashMap<>();
|
||||
|
||||
this.table = new ProgramTable<>((condition, availability) -> {
|
||||
final int idx;
|
||||
final ProgramId finalId = gbufferProgramId(ids, condition, availability);
|
||||
|
||||
if (availability.texture && availability.lightmap) {
|
||||
idx = 2;
|
||||
} else if (availability.texture) {
|
||||
idx = 1;
|
||||
} else {
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
ProgramId id = ids[condition.ordinal() * 3 + idx];
|
||||
|
||||
if (id == null) {
|
||||
id = ids[idx];
|
||||
}
|
||||
|
||||
final ProgramId finalId = id;
|
||||
|
||||
return cachedPasses.computeIfAbsent(Pair.of(id, availability), p -> {
|
||||
return cachedPasses.computeIfAbsent(Pair.of(finalId, availability), p -> {
|
||||
final ProgramSource source = resolver.resolveNullable(p.getLeft());
|
||||
|
||||
if (condition == RenderCondition.SHADOW || condition == RenderCondition.SHADOW_TRANSLUCENT) {
|
||||
@@ -583,7 +570,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
|
||||
try {
|
||||
return createPass(source, availability,
|
||||
condition == RenderCondition.SHADOW || condition == RenderCondition.SHADOW_TRANSLUCENT, finalId);
|
||||
condition == RenderCondition.SHADOW || condition == RenderCondition.SHADOW_TRANSLUCENT, finalId, builtPrograms);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to create pass for " + source.getName() + " for rendering condition "
|
||||
+ condition + " specialized to input availability " + availability, e);
|
||||
@@ -1131,6 +1118,25 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
null, Collections.emptyList(), false);
|
||||
}
|
||||
|
||||
private static ProgramId gbufferProgramId(ProgramId[] ids, RenderCondition condition, InputAvailability availability) {
|
||||
final int idx;
|
||||
|
||||
if (availability.texture && availability.lightmap) {
|
||||
idx = 2;
|
||||
} else if (availability.texture) {
|
||||
idx = 1;
|
||||
} else {
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
final ProgramId id = ids[condition.ordinal() * 3 + idx];
|
||||
return id != null ? id : ids[idx];
|
||||
}
|
||||
|
||||
private record SharedProgramKey(String sourceName, InputAvailability availability, boolean shadow) {}
|
||||
|
||||
private record BuiltProgram(Program program, Pass firstPass) {}
|
||||
|
||||
private static ProgramBuilder beginBuilder(String name, Map<PatchShaderType, String> transformed) {
|
||||
final String vertex = transformed.get(PatchShaderType.VERTEX);
|
||||
final String geometry = transformed.get(PatchShaderType.GEOMETRY);
|
||||
@@ -1143,11 +1149,22 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
return ProgramBuilder.begin(name, vertex, geometry, tessControl, tessEval, fragment, IrisSamplers.WORLD_RESERVED_TEXTURE_UNITS);
|
||||
}
|
||||
|
||||
private Pass createPass(ProgramSource source, InputAvailability availability, boolean shadow, ProgramId id) {
|
||||
private Pass createPass(ProgramSource source, InputAvailability availability, boolean shadow, ProgramId id,
|
||||
Map<SharedProgramKey, BuiltProgram> builtPrograms) {
|
||||
// Use pre-computed transform if available, otherwise transform synchronously
|
||||
Pair<String, InputAvailability> key = Pair.of(source.getName(), availability);
|
||||
Map<PatchShaderType, String> transformed = attributeTransforms.get(key);
|
||||
|
||||
// The synchronous fallback below depends on the program id, so only precomputed programs are shared
|
||||
final SharedProgramKey shareKey = transformed != null ? new SharedProgramKey(source.getName(), availability, shadow) : null;
|
||||
final BuiltProgram shared = shareKey != null ? builtPrograms.get(shareKey) : null;
|
||||
if (shared != null) {
|
||||
final Pass pass = createPassInner(shared.program(), source.getDirectives(), shadow, id);
|
||||
this.customUniforms.mapPassLike(shared.firstPass(), pass);
|
||||
pass.setInstancingKey(source.getName(), availability, shadow);
|
||||
return pass;
|
||||
}
|
||||
|
||||
if (transformed == null) {
|
||||
// Fallback to synchronous transform if not pre-computed
|
||||
String vertex = source.getVertexSource().orElseThrow(NullPointerException::new);
|
||||
@@ -1163,26 +1180,36 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
}
|
||||
|
||||
ProgramBuilder builder = beginBuilder(source.getName(), transformed);
|
||||
wireGbufferProgram(builder, availability, shadow);
|
||||
final Program program = builder.build();
|
||||
vertexWriteReplays.attach(source.getName(), program, transformed, (samplers, images) -> wireGbufferResources(samplers, images, availability, shadow));
|
||||
|
||||
final Pass pass = createPassInner(builder, source.getDirectives(), availability, shadow, id);
|
||||
final Pass pass = createPassInner(program, source.getDirectives(), shadow, id);
|
||||
this.customUniforms.mapholderToPass(builder, pass);
|
||||
pass.setInstancingKey(source.getName(), availability, shadow);
|
||||
if (shareKey != null) {
|
||||
builtPrograms.put(shareKey, new BuiltProgram(program, pass));
|
||||
}
|
||||
return pass;
|
||||
}
|
||||
|
||||
private void wireGbufferProgram(ProgramBuilder builder, InputAvailability availability, boolean shadow) {
|
||||
CommonUniforms.addDynamicUniforms(builder, FogMode.PER_VERTEX);
|
||||
this.customUniforms.assignTo(builder);
|
||||
wireGbufferResources(builder, builder, availability, shadow);
|
||||
}
|
||||
|
||||
private void wireGbufferResources(SamplerHolder samplers, ImageHolder images, InputAvailability availability, boolean shadow) {
|
||||
final Supplier<ImmutableSet<Integer>> flipped = shadow ? flippedShadowGbuffers : flippedGbuffers;
|
||||
|
||||
TextureStage textureStage = TextureStage.GBUFFERS_AND_SHADOW;
|
||||
|
||||
ProgramSamplers.CustomTextureSamplerInterceptor customTextureSamplerInterceptor =
|
||||
ProgramSamplers.customTextureSamplerInterceptor(builder,
|
||||
ProgramSamplers.customTextureSamplerInterceptor(samplers,
|
||||
customTextureManager.getCustomTextureIdMap(textureStage));
|
||||
|
||||
IrisSamplers.addRenderTargetSamplers(customTextureSamplerInterceptor, flipped, renderTargets, false, this);
|
||||
IrisImages.addRenderTargetImages(builder, flipped, renderTargets);
|
||||
IrisImages.addRenderTargetImages(images, flipped, renderTargets);
|
||||
|
||||
if (!shouldBindPBR) {
|
||||
shouldBindPBR = IrisSamplers.hasPBRSamplers(customTextureSamplerInterceptor);
|
||||
@@ -1198,7 +1225,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
|
||||
IrisSamplers.addCustomImages(customTextureSamplerInterceptor, customImages);
|
||||
IrisSamplers.addCustomTextures(customTextureSamplerInterceptor, customTextureManager.getIrisCustomTextures());
|
||||
IrisImages.addCustomImages(builder, customImages);
|
||||
IrisImages.addCustomImages(images, customImages);
|
||||
|
||||
if (IrisSamplers.hasShadowSamplers(customTextureSamplerInterceptor)) {
|
||||
if (!shadow) {
|
||||
@@ -1207,15 +1234,12 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
|
||||
if (shadowRenderTargets != null) {
|
||||
recordSamplerUsage(IrisSamplers.addShadowSamplers(customTextureSamplerInterceptor, shadowRenderTargets, null, true));
|
||||
IrisImages.addShadowColorImages(builder, shadowRenderTargets, null);
|
||||
IrisImages.addShadowColorImages(images, shadowRenderTargets, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Pass createPassInner(ProgramBuilder builder, ProgramDirectives programDirectives, InputAvailability availability, boolean shadow, ProgramId id) {
|
||||
|
||||
wireGbufferProgram(builder, availability, shadow);
|
||||
|
||||
private Pass createPassInner(Program program, ProgramDirectives programDirectives, boolean shadow, ProgramId id) {
|
||||
GlFramebuffer framebufferBeforeTranslucents;
|
||||
GlFramebuffer framebufferAfterTranslucents;
|
||||
|
||||
@@ -1240,12 +1264,8 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
}
|
||||
});
|
||||
|
||||
Pass pass = new Pass(builder.build(), framebufferBeforeTranslucents, framebufferAfterTranslucents, alphaTestOverride,
|
||||
return new Pass(program, framebufferBeforeTranslucents, framebufferAfterTranslucents, alphaTestOverride,
|
||||
programDirectives.getBlendModeOverride().orElse(id.getBlendModeOverride()), bufferOverrides, shadow);
|
||||
|
||||
this.customUniforms.mapholderToPass(builder, pass);
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
public void addGbufferOrShadowSamplers(SamplerHolder samplers, ImageHolder images, Supplier<ImmutableSet<Integer>> flipped,
|
||||
@@ -1468,7 +1488,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
if (!instancedVariantAttempted[i]) {
|
||||
instancedVariantAttempted[i] = true;
|
||||
if (instancingSourceName != null && supportsInstancing[i]) {
|
||||
instancedVariants[i] = buildInstancedVariant(instancingSourceName, instancingAvailability, instancingShadow, kind);
|
||||
instancedVariants[i] = sharedInstancedVariant(instancingSourceName, instancingAvailability, instancingShadow, kind);
|
||||
}
|
||||
}
|
||||
return instancedVariants[i];
|
||||
@@ -1487,6 +1507,20 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
}
|
||||
}
|
||||
|
||||
private record InstancedVariantKey(String sourceName, InputAvailability availability, boolean shadow, Instancing kind) {}
|
||||
|
||||
// Passes sharing a program also share its instanced variants
|
||||
private final Map<InstancedVariantKey, Program> instancedVariantPrograms = new HashMap<>();
|
||||
|
||||
@Nullable
|
||||
private Program sharedInstancedVariant(String sourceName, InputAvailability availability, boolean shadow, Instancing kind) {
|
||||
final InstancedVariantKey key = new InstancedVariantKey(sourceName, availability, shadow, kind);
|
||||
if (instancedVariantPrograms.containsKey(key)) return instancedVariantPrograms.get(key);
|
||||
final Program variant = buildInstancedVariant(sourceName, availability, shadow, kind);
|
||||
instancedVariantPrograms.put(key, variant);
|
||||
return variant;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Program buildInstancedVariant(String sourceName, InputAvailability availability, boolean shadow, Instancing kind) {
|
||||
final Map<Pair<String, InputAvailability>, Map<PatchShaderType, String>> transforms = instancedAttributeTransforms.get(kind);
|
||||
@@ -1498,6 +1532,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
final ProgramBuilder builder = beginBuilder(sourceName + kind.variantSuffix, transformed);
|
||||
wireGbufferProgram(builder, availability, shadow);
|
||||
final Program variant = builder.build();
|
||||
vertexWriteReplays.attach(sourceName + kind.variantSuffix, variant, transformed, (samplers, images) -> wireGbufferResources(samplers, images, availability, shadow));
|
||||
this.customUniforms.mapholderToPass(builder, variant);
|
||||
return variant;
|
||||
} catch (Exception e) {
|
||||
@@ -1577,6 +1612,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
}
|
||||
}
|
||||
}
|
||||
vertexWriteReplays.destroy();
|
||||
if (shadowVoxelizationCompute != null) {
|
||||
shadowVoxelizationCompute.destroy();
|
||||
shadowVoxelizationCompute = null;
|
||||
@@ -2440,6 +2476,16 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
|
||||
if (source == null) {
|
||||
return false;
|
||||
}
|
||||
boolean mentioned = false;
|
||||
for (String builtin : MV_BUILTINS) {
|
||||
if (source.contains(builtin)) {
|
||||
mentioned = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!mentioned) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
final Transformer transformer = new Transformer(ShaderParser.parseShader(source).full());
|
||||
for (String builtin : MV_BUILTINS) {
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
package net.coderbot.iris.pipeline;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.RenderSystem;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.BackendManager;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VertexWriteReplaySetup;
|
||||
import com.gtnewhorizons.angelica.glsm.texture.InternalTextureFormat;
|
||||
import com.gtnewhorizons.angelica.glsm.texture.TextureType;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.gl.image.ImageHolder;
|
||||
import net.coderbot.iris.gl.program.ComputeProgram;
|
||||
import net.coderbot.iris.gl.program.Program;
|
||||
import net.coderbot.iris.gl.program.ProgramBuilder;
|
||||
import net.coderbot.iris.gl.sampler.GlSampler;
|
||||
import net.coderbot.iris.gl.sampler.SamplerHolder;
|
||||
import net.coderbot.iris.gl.state.ValueUpdateNotifier;
|
||||
import net.coderbot.iris.pipeline.transform.PatchShaderType;
|
||||
import net.coderbot.iris.pipeline.transform.RwImageStoreExtractor;
|
||||
import net.coderbot.iris.pipeline.transform.RwImageStoreExtractor.VertexInput;
|
||||
import net.coderbot.iris.samplers.IrisSamplers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
/**
|
||||
* SDL-GPU only allows image writes from compute, so a gbuffers/shadow vertex shader that writes images gets a compute
|
||||
* twin that the backend runs over every draw made with the program.
|
||||
*/
|
||||
final class VertexWriteReplays {
|
||||
|
||||
private final List<ComputeProgram> twins = new ArrayList<>();
|
||||
private final IntArrayList attachedPrograms = new IntArrayList();
|
||||
|
||||
void attach(String name, Program program, Map<PatchShaderType, String> transformed, BiConsumer<SamplerHolder, ImageHolder> wireResources) {
|
||||
BackendManager.RENDER_BACKEND.setVertexWriteReplay(program.getProgramId(), null);
|
||||
final String source = transformed.get(PatchShaderType.COMPUTE);
|
||||
if (RwImageStoreExtractor.parseSentinel(source) != RwImageStoreExtractor.RwExtractMode.VERTEX_REPLAY) return;
|
||||
|
||||
final ComputeProgram twin;
|
||||
try {
|
||||
twin = ProgramBuilder.beginCompute(name + "_vertex_replay", source, IrisSamplers.WORLD_RESERVED_TEXTURE_UNITS).buildCompute();
|
||||
} catch (RuntimeException e) {
|
||||
Iris.logger.error("Vertex image write replay for {} failed to build; its image writes are dropped", name, e);
|
||||
return;
|
||||
}
|
||||
|
||||
final List<String> writtenImages = RwImageStoreExtractor.parseVertexReplayImages(source);
|
||||
final List<String> images = new ArrayList<>(writtenImages);
|
||||
images.addAll(RwImageStoreExtractor.parseVertexReplayReadImages(source));
|
||||
final List<String> samplers = new ArrayList<>();
|
||||
for (String sampler : RwImageStoreExtractor.parseVertexReplaySamplers(source)) {
|
||||
if (RenderSystem.getUniformLocation(twin.getProgramId(), sampler) != -1) samplers.add(sampler);
|
||||
}
|
||||
final ResourceCollector resources = new ResourceCollector(images, samplers);
|
||||
wireResources.accept(resources, resources);
|
||||
for (int i = 0; i < resources.images.length; i++) {
|
||||
if (resources.images[i] == null) {
|
||||
Iris.logger.warn("Vertex image writes of {} use image {}, which the pipeline does not provide; they are dropped", name, resources.imageNames.get(i));
|
||||
twin.destroy();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final List<VertexInput> inputs = RwImageStoreExtractor.parseVertexReplayInputs(source);
|
||||
final String[] inputNames = new String[inputs.size()];
|
||||
final int[] inputLocations = new int[inputs.size()];
|
||||
for (int i = 0; i < inputs.size(); i++) {
|
||||
inputNames[i] = inputs.get(i).name();
|
||||
inputLocations[i] = inputs.get(i).location();
|
||||
}
|
||||
BackendManager.RENDER_BACKEND.setVertexWriteReplay(program.getProgramId(), new VertexWriteReplaySetup(twin.getProgramId(),
|
||||
inputNames, inputLocations, resources.images, writtenImages.size(),
|
||||
resources.samplerNames.toArray(new String[0]), resources.samplerUnits, resources.samplerTextures, resources.samplerObjects,
|
||||
RwImageStoreExtractor.VG_REPLAY_VERTEX_BUFFER_BINDING, RwImageStoreExtractor.VG_REPLAY_VERTEX_BUFFER_COUNT, RwImageStoreExtractor.VG_REPLAY_INDEX_BUFFER_BINDING));
|
||||
attachedPrograms.add(program.getProgramId());
|
||||
twins.add(twin);
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
for (int i = 0; i < attachedPrograms.size(); i++) BackendManager.RENDER_BACKEND.setVertexWriteReplay(attachedPrograms.getInt(i), null);
|
||||
attachedPrograms.clear();
|
||||
for (ComputeProgram twin : twins) twin.destroy();
|
||||
twins.clear();
|
||||
}
|
||||
|
||||
private static final class ResourceCollector implements SamplerHolder, ImageHolder {
|
||||
final List<String> imageNames;
|
||||
final IntSupplier[] images;
|
||||
final List<String> samplerNames;
|
||||
final int[] samplerUnits;
|
||||
final IntSupplier[] samplerTextures;
|
||||
final int[] samplerObjects;
|
||||
|
||||
ResourceCollector(List<String> imageNames, List<String> samplerNames) {
|
||||
this.imageNames = imageNames;
|
||||
this.images = new IntSupplier[imageNames.size()];
|
||||
this.samplerNames = samplerNames;
|
||||
this.samplerUnits = new int[samplerNames.size()];
|
||||
Arrays.fill(samplerUnits, -1);
|
||||
this.samplerTextures = new IntSupplier[samplerNames.size()];
|
||||
this.samplerObjects = new int[samplerNames.size()];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasImage(String name) {
|
||||
return imageNames.contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTextureImage(IntSupplier textureID, InternalTextureFormat internalFormat, String name) {
|
||||
final int index = imageNames.indexOf(name);
|
||||
if (index >= 0 && images[index] == null) images[index] = textureID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSampler(String name) {
|
||||
return samplerNames.contains(name) || imageNames.contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addExternalSampler(int textureUnit, String... names) {
|
||||
for (String name : names) {
|
||||
final int index = samplerNames.indexOf(name);
|
||||
if (index >= 0 && unassigned(index)) samplerUnits[index] = textureUnit;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addDefaultSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, GlSampler sampler, String... names) {
|
||||
return addDynamicSampler(type, texture, notifier, sampler, names);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addDynamicSampler(TextureType type, IntSupplier texture, GlSampler sampler, String... names) {
|
||||
return addDynamicSampler(type, texture, null, sampler, names);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addDynamicSampler(TextureType type, IntSupplier texture, ValueUpdateNotifier notifier, GlSampler sampler, String... names) {
|
||||
boolean used = false;
|
||||
for (String name : names) {
|
||||
final int index = samplerNames.indexOf(name);
|
||||
if (index < 0) continue;
|
||||
used = true;
|
||||
if (unassigned(index)) {
|
||||
samplerTextures[index] = texture;
|
||||
samplerObjects[index] = sampler != null ? sampler.getId() : 0;
|
||||
}
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
private boolean unassigned(int index) {
|
||||
return samplerUnits[index] < 0 && samplerTextures[index] == null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package net.coderbot.iris.pipeline.transform;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.glsm.RenderSystem;
|
||||
import net.coderbot.iris.gl.program.ComputeProgram;
|
||||
import net.coderbot.iris.postprocess.FullScreenQuadRenderer;
|
||||
import net.coderbot.iris.uniforms.custom.CustomUniforms;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL42;
|
||||
@@ -16,6 +17,7 @@ public final class PreRasterComputeDispatcher {
|
||||
final int prevProgram = GLStateManager.glGetInteger(GL20.GL_CURRENT_PROGRAM);
|
||||
compute.use();
|
||||
uniforms.push(compute);
|
||||
if (mode == RwImageStoreExtractor.RwExtractMode.COMPOSITE_VSH) FullScreenQuadRenderer.uploadCompositeMatrices();
|
||||
int targetSizeLoc = cachedTargetSizeLoc;
|
||||
if (mode == RwImageStoreExtractor.RwExtractMode.COMPOSITE_FSH) {
|
||||
if (targetSizeLoc == LOC_UNRESOLVED) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.coderbot.iris.pipeline.transform;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.backend.BackendManager;
|
||||
import com.gtnewhorizons.angelica.glsm.GlslTransformUtils;
|
||||
import net.coderbot.iris.gl.image.ImageInformation;
|
||||
import com.gtnewhorizons.angelica.glsm.texture.InternalTextureFormat;
|
||||
@@ -7,6 +8,7 @@ import org.antlr.v4.runtime.CommonToken;
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
import org.embeddedt.embeddium.impl.render.shader.ShaderLoader;
|
||||
import org.taumc.glsl.ShaderParser;
|
||||
import org.taumc.glsl.Transformer;
|
||||
import org.taumc.glsl.grammar.GLSLLexer;
|
||||
@@ -22,26 +24,30 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public final class RwImageStoreExtractor {
|
||||
|
||||
public enum RwExtractMode { CHUNK, COMPOSITE_VSH, COMPOSITE_FSH }
|
||||
public enum RwExtractMode { CHUNK, COMPOSITE_VSH, COMPOSITE_FSH, VERTEX_REPLAY }
|
||||
|
||||
public static RwExtractMode parseSentinel(String computeSource) {
|
||||
if (computeSource == null) return null;
|
||||
if (computeSource.startsWith("// _vg_mode: chunk")) return RwExtractMode.CHUNK;
|
||||
if (computeSource.startsWith("// _vg_mode: composite-vsh")) return RwExtractMode.COMPOSITE_VSH;
|
||||
if (computeSource.startsWith("// _vg_mode: composite-fsh")) return RwExtractMode.COMPOSITE_FSH;
|
||||
if (computeSource.startsWith("// _vg_mode: vertex-replay")) return RwExtractMode.VERTEX_REPLAY;
|
||||
return null;
|
||||
}
|
||||
|
||||
public record VertexInput(String name, String glslType, int location) {}
|
||||
|
||||
public record AttributeSlot(String name, String glslType, int byteOffset, String unpackExpr, boolean liveBranch) {
|
||||
public AttributeSlot(String name, String glslType, int byteOffset, String unpackExpr) {
|
||||
this(name, glslType, byteOffset, unpackExpr, true);
|
||||
}
|
||||
}
|
||||
|
||||
public record Result(String strippedSource, String computeSource, Set<String> writtenImages, RwExtractMode mode) {}
|
||||
public record Result(String strippedSource, String computeSource, Set<String> writtenImages, RwExtractMode mode, Set<String> atomicImages) {}
|
||||
|
||||
public record ImageDecl(String name, String glslType, String layoutFormat) {}
|
||||
|
||||
@@ -53,6 +59,9 @@ public final class RwImageStoreExtractor {
|
||||
|
||||
public static final int VG_VBUF_SSBO_BINDING = 9;
|
||||
public static final int VG_RANGES_SSBO_BINDING = 10;
|
||||
public static final int VG_REPLAY_VERTEX_BUFFER_BINDING = 9;
|
||||
public static final int VG_REPLAY_VERTEX_BUFFER_COUNT = 4;
|
||||
public static final int VG_REPLAY_INDEX_BUFFER_BINDING = VG_REPLAY_VERTEX_BUFFER_BINDING + VG_REPLAY_VERTEX_BUFFER_COUNT;
|
||||
private static final int STRIDE_BYTES = 48;
|
||||
private static final int STRIDE_UINTS = STRIDE_BYTES / 4;
|
||||
|
||||
@@ -62,8 +71,30 @@ public final class RwImageStoreExtractor {
|
||||
return activeCustomImages;
|
||||
}
|
||||
|
||||
private static volatile Set<String> atomicCustomImages = Set.of();
|
||||
|
||||
public static void setActiveCustomImages(Map<String, ImageInformation> images) {
|
||||
activeCustomImages = (images == null) ? Map.of() : Map.copyOf(images);
|
||||
final TreeSet<String> atomic = new TreeSet<>();
|
||||
if (!activeCustomImages.isEmpty() && !BackendManager.RENDER_BACKEND.supportsComputeImageAtomics()) {
|
||||
for (ImageInformation image : activeCustomImages.values()) {
|
||||
if (image.isRelative()) continue;
|
||||
if (BackendManager.RENDER_BACKEND.supportsComputeImageAtomics(image.width(), image.height(), image.depth())) atomic.add(image.name());
|
||||
}
|
||||
}
|
||||
if (!atomic.equals(atomicCustomImages)) {
|
||||
atomicCustomImages = atomic;
|
||||
TransformPatcher.clearCache();
|
||||
ShaderTransformer.clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean supportsAtomics(String image) {
|
||||
return atomicCustomImages.contains(image) || BackendManager.RENDER_BACKEND.supportsComputeImageAtomics();
|
||||
}
|
||||
|
||||
static String atomicCustomImagesKey() {
|
||||
return String.join(",", atomicCustomImages);
|
||||
}
|
||||
|
||||
private static final Set<String> RW_CALLS = Set.of(
|
||||
@@ -73,7 +104,11 @@ public final class RwImageStoreExtractor {
|
||||
);
|
||||
|
||||
public static Result tryExtract(String source, PatchShaderType stage, String programName) {
|
||||
if (source == null) return null;
|
||||
return tryExtract(source, stage, programName, null);
|
||||
}
|
||||
|
||||
public static Result tryExtract(String source, PatchShaderType stage, String programName, RwExtractMode vertexMode) {
|
||||
if (source == null || !mayWriteImages(source)) return null;
|
||||
if (stage != PatchShaderType.VERTEX && stage != PatchShaderType.FRAGMENT) return null;
|
||||
|
||||
final ShaderParser.ParsedShader parsed = ShaderParser.parseShader(source);
|
||||
@@ -94,17 +129,188 @@ public final class RwImageStoreExtractor {
|
||||
final Map<String, String> chunkAttrAstTypes = (stage == PatchShaderType.VERTEX) ? collectChunkAttrAstTypes(decls) : Map.of();
|
||||
final Set<String> chunkAttrsUsed = (stage == PatchShaderType.VERTEX) ? collectChunkAttrsUsed(decls, root, source) : Set.of();
|
||||
|
||||
final RwExtractMode mode = (stage == PatchShaderType.VERTEX) ? (chunkAttrsUsed.isEmpty() ? RwExtractMode.COMPOSITE_VSH : RwExtractMode.CHUNK) : RwExtractMode.COMPOSITE_FSH;
|
||||
final RwExtractMode mode = (stage == PatchShaderType.VERTEX)
|
||||
? (vertexMode != null ? vertexMode : chunkAttrsUsed.isEmpty() ? RwExtractMode.COMPOSITE_VSH : RwExtractMode.CHUNK)
|
||||
: RwExtractMode.COMPOSITE_FSH;
|
||||
|
||||
final List<AttributeSlot> attrs = (mode == RwExtractMode.CHUNK) ? collectVertexAttributes(chunkAttrsUsed, chunkAttrAstTypes) : List.of();
|
||||
final Set<String> quadInputs = (mode == RwExtractMode.COMPOSITE_VSH) ? declaredQuadInputs(decls) : Set.of();
|
||||
final boolean definesFtransform = definesFunction(root, "iris_ftransform");
|
||||
|
||||
final String stripped = buildRasterOutputViaSourceSlicing(source, decls, exprs, writtenImages, declared, mode);
|
||||
|
||||
final Transformer tc = new Transformer(root);
|
||||
applyComputeMutations(tc, writtenImages, declared, chunkAttrsUsed, mode, nonWriteonlyImages);
|
||||
final String compute = buildComputeOutput(root, mode, writtenImages, nonWriteonlyImages, declared, attrs);
|
||||
applyComputeMutations(tc, writtenImages, declared, mode == RwExtractMode.COMPOSITE_VSH ? quadInputs : chunkAttrsUsed, mode, nonWriteonlyImages);
|
||||
final String compute = buildComputeOutput(root, mode, writtenImages, nonWriteonlyImages, declared, attrs, quadInputs, definesFtransform, null);
|
||||
|
||||
return new Result(stripped, compute, writtenImages, mode);
|
||||
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 || !mayWriteImages(source)) return null;
|
||||
|
||||
final ShaderParser.ParsedShader parsed = ShaderParser.parseShader(source);
|
||||
final GLSLParser.Translation_unitContext root = parsed.full();
|
||||
|
||||
final List<GLSLParser.Single_declarationContext> decls = new ArrayList<>();
|
||||
final List<GLSLParser.Postfix_expressionContext> exprs = new ArrayList<>();
|
||||
GlslAstHelpers.collectInto(root,
|
||||
new List[] { decls, exprs },
|
||||
new Class<?>[] { GLSLParser.Single_declarationContext.class, GLSLParser.Postfix_expressionContext.class });
|
||||
|
||||
final Map<String, ImageDecl> declared = collectImageDecls(decls);
|
||||
final Set<String> writtenImages = new LinkedHashSet<>();
|
||||
final Set<String> nonWriteonlyImages = new LinkedHashSet<>();
|
||||
classifyImageCalls(exprs, declared, writtenImages, nonWriteonlyImages);
|
||||
if (writtenImages.isEmpty()) return null;
|
||||
|
||||
final String stripped = buildRasterOutputViaSourceSlicing(source, decls, exprs, writtenImages, declared, RwExtractMode.VERTEX_REPLAY);
|
||||
final List<VertexInput> inputs = collectVertexInputs(decls);
|
||||
if (inputs == null) return new Result(stripped, null, writtenImages, RwExtractMode.VERTEX_REPLAY, Set.of());
|
||||
|
||||
final List<String> readImages = readOnlyImages(exprs, declared, writtenImages);
|
||||
final Set<String> inputNames = new LinkedHashSet<>();
|
||||
for (VertexInput input : inputs) inputNames.add(input.name());
|
||||
final Transformer tc = new Transformer(root);
|
||||
applyComputeMutations(tc, writtenImages, declared, inputNames, RwExtractMode.VERTEX_REPLAY, nonWriteonlyImages);
|
||||
for (String name : readImages) tc.removeVariable(name);
|
||||
final ReplayLayout layout = new ReplayLayout(inputs, readImages, replayHeader(inputs, writtenImages, readImages, collectSamplerNames(decls)));
|
||||
final String compute = buildComputeOutput(root, RwExtractMode.VERTEX_REPLAY, writtenImages, nonWriteonlyImages, declared, List.of(), Set.of(), false, layout);
|
||||
|
||||
return new Result(stripped, compute, writtenImages, RwExtractMode.VERTEX_REPLAY, atomicImages(root, writtenImages));
|
||||
}
|
||||
|
||||
private record ReplayLayout(List<VertexInput> inputs, List<String> readImages, String header) {}
|
||||
|
||||
private static final String REPLAY_INPUTS_HEADER = "// _vg_inputs:";
|
||||
private static final String REPLAY_IMAGES_HEADER = "// _vg_images:";
|
||||
private static final String REPLAY_READ_IMAGES_HEADER = "// _vg_read_images:";
|
||||
private static final String REPLAY_SAMPLERS_HEADER = "// _vg_samplers:";
|
||||
|
||||
private static String replayHeader(List<VertexInput> inputs, Set<String> writtenImages, List<String> readImages, List<String> samplers) {
|
||||
final StringBuilder out = new StringBuilder(REPLAY_INPUTS_HEADER);
|
||||
for (VertexInput input : inputs) out.append(' ').append(input.name()).append(':').append(input.glslType()).append(':').append(input.location());
|
||||
out.append('\n').append(REPLAY_IMAGES_HEADER);
|
||||
for (String image : sortedImages(writtenImages)) out.append(' ').append(image);
|
||||
out.append('\n').append(REPLAY_READ_IMAGES_HEADER);
|
||||
for (String image : readImages) out.append(' ').append(image);
|
||||
out.append('\n').append(REPLAY_SAMPLERS_HEADER);
|
||||
for (String sampler : samplers) out.append(' ').append(sampler);
|
||||
return out.append('\n').toString();
|
||||
}
|
||||
|
||||
/** Images the shader reads but never writes, sorted; the replay binds them after the written ones. */
|
||||
private static List<String> readOnlyImages(List<GLSLParser.Postfix_expressionContext> exprs, Map<String, ImageDecl> declared, Set<String> writtenImages) {
|
||||
final Set<String> read = new LinkedHashSet<>();
|
||||
for (var expr : exprs) {
|
||||
if (GlslAstHelpers.extractCallName(expr) == null) continue;
|
||||
final String firstArg = GlslAstHelpers.firstArgIdentifier(expr);
|
||||
if (declared.containsKey(firstArg) && !writtenImages.contains(firstArg)) read.add(firstArg);
|
||||
}
|
||||
final List<String> sorted = new ArrayList<>(read);
|
||||
Collections.sort(sorted);
|
||||
return sorted;
|
||||
}
|
||||
|
||||
/** The images a vertex-replay compute source only reads, in the order of their bindings after the written ones. */
|
||||
public static List<String> parseVertexReplayReadImages(String computeSource) {
|
||||
return headerTokens(computeSource, REPLAY_READ_IMAGES_HEADER);
|
||||
}
|
||||
|
||||
/** The samplers a vertex-replay compute source declares. */
|
||||
public static List<String> parseVertexReplaySamplers(String computeSource) {
|
||||
return headerTokens(computeSource, REPLAY_SAMPLERS_HEADER);
|
||||
}
|
||||
|
||||
/** The inputs of a vertex-replay compute source, in the order of its {@code _vg_attr} slots. */
|
||||
public static List<VertexInput> parseVertexReplayInputs(String computeSource) {
|
||||
final List<VertexInput> out = new ArrayList<>();
|
||||
for (String token : headerTokens(computeSource, REPLAY_INPUTS_HEADER)) {
|
||||
final String[] parts = token.split(":");
|
||||
if (parts.length != 3) continue;
|
||||
out.add(new VertexInput(parts[0], parts[1], Integer.parseInt(parts[2])));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The images a vertex-replay compute source writes, in the order of their bindings. */
|
||||
public static List<String> parseVertexReplayImages(String computeSource) {
|
||||
return headerTokens(computeSource, REPLAY_IMAGES_HEADER);
|
||||
}
|
||||
|
||||
private static List<String> headerTokens(String computeSource, String header) {
|
||||
if (computeSource == null) return List.of();
|
||||
final int start = computeSource.indexOf(header);
|
||||
if (start < 0) return List.of();
|
||||
final int end = computeSource.indexOf('\n', start);
|
||||
final String line = computeSource.substring(start + header.length(), end < 0 ? computeSource.length() : end).trim();
|
||||
if (line.isEmpty()) return List.of();
|
||||
return List.of(line.split(" "));
|
||||
}
|
||||
|
||||
private static final Set<String> REPLAYABLE_INPUT_TYPES = Set.of(
|
||||
"float", "vec2", "vec3", "vec4", "int", "ivec2", "ivec3", "ivec4", "uint", "uvec2", "uvec3", "uvec4");
|
||||
|
||||
/** Global {@code in}/{@code attribute} declarations, or null if one of them cannot be fetched per component. */
|
||||
private static List<VertexInput> collectVertexInputs(List<GLSLParser.Single_declarationContext> decls) {
|
||||
final List<VertexInput> out = new ArrayList<>();
|
||||
for (var decl : decls) {
|
||||
final var fst = decl.fully_specified_type();
|
||||
if (fst == null || fst.type_qualifier() == null) continue;
|
||||
if (GlslAstHelpers.enclosingOfType(decl, GLSLParser.Function_definitionContext.class) != null) continue;
|
||||
boolean isInput = false;
|
||||
int location = -1;
|
||||
for (var sq : fst.type_qualifier().single_type_qualifier()) {
|
||||
if (sq.storage_qualifier() != null) {
|
||||
final String storage = sq.storage_qualifier().getText();
|
||||
if (storage.equals("in") || storage.equals("attribute")) isInput = true;
|
||||
}
|
||||
final var lq = sq.layout_qualifier();
|
||||
if (lq == null || lq.layout_qualifier_id_list() == null) continue;
|
||||
for (var id : lq.layout_qualifier_id_list().layout_qualifier_id()) {
|
||||
if (id.IDENTIFIER() == null || !"location".equals(id.IDENTIFIER().getText())) continue;
|
||||
try {
|
||||
location = Integer.parseInt(id.getText().substring(id.getText().indexOf('=') + 1).trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isInput) continue;
|
||||
final String type = fst.type_specifier().getText();
|
||||
if (!REPLAYABLE_INPUT_TYPES.contains(type)) return null;
|
||||
final List<GLSLParser.Typeless_declarationContext> declarators = new ArrayList<>();
|
||||
declarators.add(decl.typeless_declaration());
|
||||
if (decl.getParent() instanceof GLSLParser.Init_declarator_listContext list) declarators.addAll(list.typeless_declaration());
|
||||
for (var typeless : declarators) {
|
||||
if (typeless == null || typeless.IDENTIFIER() == null || typeless.array_specifier() != null) return null;
|
||||
out.add(new VertexInput(typeless.IDENTIFIER().getText(), type, location));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Global sampler uniforms, which the replay may read even after the graphics program stops reading them. */
|
||||
private static List<String> collectSamplerNames(List<GLSLParser.Single_declarationContext> decls) {
|
||||
final List<String> out = new ArrayList<>();
|
||||
for (var decl : decls) {
|
||||
final var fst = decl.fully_specified_type();
|
||||
if (fst == null || fst.type_specifier() == null) continue;
|
||||
if (GlslAstHelpers.enclosingOfType(decl, GLSLParser.Function_definitionContext.class) != null) continue;
|
||||
final String type = fst.type_specifier().getText();
|
||||
if (!type.startsWith("sampler") && !type.startsWith("isampler") && !type.startsWith("usampler")) continue;
|
||||
final List<GLSLParser.Typeless_declarationContext> declarators = new ArrayList<>();
|
||||
declarators.add(decl.typeless_declaration());
|
||||
if (decl.getParent() instanceof GLSLParser.Init_declarator_listContext list) declarators.addAll(list.typeless_declaration());
|
||||
for (var typeless : declarators) {
|
||||
if (typeless != null && typeless.IDENTIFIER() != null) out.add(typeless.IDENTIFIER().getText());
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static Map<String, ImageDecl> collectImageDecls(List<GLSLParser.Single_declarationContext> decls) {
|
||||
@@ -227,6 +433,11 @@ public final class RwImageStoreExtractor {
|
||||
final String firstArg = GlslAstHelpers.firstArgIdentifier(expr);
|
||||
if (!writtenImages.contains(firstArg)) continue;
|
||||
if (RW_CALLS.contains(fname)) {
|
||||
final ImageDecl d = declared.get(firstArg);
|
||||
if (d != null && !isCallTopLevelStatement(expr)) {
|
||||
edits.add(replaceOf(expr, scalarZeroFor(d.glslType())));
|
||||
continue;
|
||||
}
|
||||
final var stmt = GlslAstHelpers.enclosingOfType(expr, GLSLParser.StatementContext.class);
|
||||
if (stmt != null) edits.add(replaceOf(stmt, ";"));
|
||||
} else if ("imageLoad".equals(fname)) {
|
||||
@@ -265,13 +476,22 @@ public final class RwImageStoreExtractor {
|
||||
private static void applyComputeMutations(Transformer tc, Set<String> writtenImages, Map<String, ImageDecl> declared, Set<String> chunkAttrsUsed, RwExtractMode mode, Set<String> nonWriteonlyImages) {
|
||||
hoistNonConstGlobalInitializers(tc);
|
||||
renameMainToVgBody(tc);
|
||||
rewriteImageAtomicsToNonAtomic(tc, declared, writtenImages, nonWriteonlyImages);
|
||||
if (mode != RwExtractMode.VERTEX_REPLAY) rewriteImageAtomicsToNonAtomic(tc, declared, writtenImages, nonWriteonlyImages);
|
||||
for (String name : writtenImages) tc.removeVariable(name);
|
||||
if (mode == RwExtractMode.CHUNK) {
|
||||
if (mode == RwExtractMode.CHUNK || mode == RwExtractMode.VERTEX_REPLAY) {
|
||||
for (String name : chunkAttrsUsed) tc.removeVariable(name);
|
||||
convertInterfaceBlocks(tc);
|
||||
if (mode == RwExtractMode.VERTEX_REPLAY) {
|
||||
stripInterfaceLayoutQualifiers(tc);
|
||||
removeQualifierOnlyDeclarations(tc);
|
||||
}
|
||||
stripStorageQualifiers(tc, Set.of("out", "varying"));
|
||||
} else if (mode == RwExtractMode.COMPOSITE_VSH) {
|
||||
for (String name : chunkAttrsUsed) tc.removeVariable(name);
|
||||
tc.renameFunctionCall("ftransform", "iris_ftransform");
|
||||
convertInterfaceBlocks(tc);
|
||||
stripInterfaceLayoutQualifiers(tc);
|
||||
removeQualifierOnlyDeclarations(tc);
|
||||
stripStorageQualifiers(tc, Set.of("in", "attribute", "out", "varying"));
|
||||
} else if (mode == RwExtractMode.COMPOSITE_FSH) {
|
||||
stripFragOutputs(tc);
|
||||
@@ -306,6 +526,17 @@ public final class RwImageStoreExtractor {
|
||||
});
|
||||
}
|
||||
|
||||
private static Set<String> atomicImages(GLSLParser.Translation_unitContext computeRoot, Set<String> writtenImages) {
|
||||
final Set<String> out = new LinkedHashSet<>();
|
||||
for (var expr : GlslAstHelpers.collectAll(computeRoot, GLSLParser.Postfix_expressionContext.class)) {
|
||||
final String fname = GlslAstHelpers.extractCallName(expr);
|
||||
if (fname == null || !ATOMIC_CALLS.contains(fname)) continue;
|
||||
final String image = GlslAstHelpers.firstArgIdentifier(expr);
|
||||
if (writtenImages.contains(image)) out.add(image);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static boolean isCallTopLevelStatement(GLSLParser.Postfix_expressionContext expr) {
|
||||
final GLSLParser.StatementContext stmt = GlslAstHelpers.enclosingOfType(expr, GLSLParser.StatementContext.class);
|
||||
if (stmt == null) return false;
|
||||
@@ -378,6 +609,14 @@ public final class RwImageStoreExtractor {
|
||||
return "float";
|
||||
}
|
||||
|
||||
private static String scalarZeroFor(String glslType) {
|
||||
return switch (scalarFor(glslType)) {
|
||||
case "int" -> "0";
|
||||
case "uint" -> "0u";
|
||||
default -> "0.0";
|
||||
};
|
||||
}
|
||||
|
||||
private static String componentVecFor(String glslType) {
|
||||
if (glslType.startsWith("iimage")) return "ivec4";
|
||||
if (glslType.startsWith("uimage")) return "uvec4";
|
||||
@@ -396,6 +635,7 @@ public final class RwImageStoreExtractor {
|
||||
t.mutateTree(tree -> {
|
||||
for (var sq : GlslAstHelpers.collectAll(tree, GLSLParser.Storage_qualifierContext.class)) {
|
||||
if (!kill.contains(sq.getText())) continue;
|
||||
if (GlslAstHelpers.enclosingOfType(sq, GLSLParser.Parameter_declarationContext.class) != null) continue;
|
||||
blankLeadingToken(sq);
|
||||
}
|
||||
});
|
||||
@@ -415,6 +655,91 @@ public final class RwImageStoreExtractor {
|
||||
|
||||
private static final Set<String> AUXILIARY_STORAGE_QUALIFIERS = Set.of("centroid", "sample", "patch");
|
||||
|
||||
private static final Set<String> INTERFACE_STORAGE_QUALIFIERS = Set.of("in", "out", "attribute", "varying");
|
||||
|
||||
private static void removeQualifierOnlyDeclarations(Transformer t) {
|
||||
t.mutateTree(tree -> {
|
||||
if (tree.children == null) return;
|
||||
tree.children.removeIf(child -> child instanceof GLSLParser.External_declarationContext ed
|
||||
&& ed.declaration() != null && isQualifierOnly(ed.declaration()));
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean isQualifierOnly(GLSLParser.DeclarationContext decl) {
|
||||
if (decl.LEFT_BRACE() != null) return false;
|
||||
if (decl.init_declarator_list() == null) return decl.type_qualifier() != null && qualifiesStageInterface(decl.type_qualifier());
|
||||
final var single = decl.init_declarator_list().single_declaration();
|
||||
return single != null && single.typeless_declaration() == null && decl.init_declarator_list().COMMA().isEmpty()
|
||||
&& single.fully_specified_type() != null && single.fully_specified_type().type_qualifier() != null
|
||||
&& qualifiesStageInterface(single.fully_specified_type().type_qualifier());
|
||||
}
|
||||
|
||||
private static boolean qualifiesStageInterface(GLSLParser.Type_qualifierContext tq) {
|
||||
for (var sq : tq.single_type_qualifier()) {
|
||||
if (sq.invariant_qualifier() != null || sq.precise_qualifier() != null) return true;
|
||||
if (sq.storage_qualifier() != null && INTERFACE_STORAGE_QUALIFIERS.contains(sq.storage_qualifier().getText())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void convertInterfaceBlocks(Transformer t) {
|
||||
t.mutateTree(tree -> {
|
||||
if (tree.children == null) return;
|
||||
tree.children.removeIf(child -> child instanceof GLSLParser.External_declarationContext ed
|
||||
&& isInterfaceBlock(ed.declaration()) && "gl_PerVertex".equals(ed.declaration().IDENTIFIER(0).getText()));
|
||||
for (var child : tree.children) {
|
||||
if (!(child instanceof GLSLParser.External_declarationContext ed) || !isInterfaceBlock(ed.declaration())) continue;
|
||||
final GLSLParser.DeclarationContext decl = ed.declaration();
|
||||
final String blockName = decl.IDENTIFIER(0).getText();
|
||||
blankAllTerminals(decl.type_qualifier());
|
||||
if (decl.IDENTIFIER().size() > 1) {
|
||||
setFirstTerminal(decl.type_qualifier(), "struct");
|
||||
setText(decl.RIGHT_BRACE(), "}; " + blockName);
|
||||
} else {
|
||||
setText(decl.IDENTIFIER(0), "");
|
||||
setText(decl.LEFT_BRACE(), "");
|
||||
setText(decl.RIGHT_BRACE(), "");
|
||||
setText(decl.SEMICOLON(), "");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean isInterfaceBlock(GLSLParser.DeclarationContext decl) {
|
||||
return decl != null && decl.LEFT_BRACE() != null && decl.type_qualifier() != null && !decl.IDENTIFIER().isEmpty()
|
||||
&& qualifiesStageInterface(decl.type_qualifier());
|
||||
}
|
||||
|
||||
private static void setText(TerminalNode node, String text) {
|
||||
if (node != null && node.getSymbol() instanceof CommonToken ct) ct.setText(text);
|
||||
}
|
||||
|
||||
private static void setFirstTerminal(ParseTree node, String text) {
|
||||
if (node instanceof TerminalNode tn) {
|
||||
setText(tn, text);
|
||||
return;
|
||||
}
|
||||
if (node.getChildCount() > 0) setFirstTerminal(node.getChild(0), text);
|
||||
}
|
||||
|
||||
private static void stripInterfaceLayoutQualifiers(Transformer t) {
|
||||
t.mutateTree(tree -> {
|
||||
for (var tq : GlslAstHelpers.collectAll(tree, GLSLParser.Type_qualifierContext.class)) {
|
||||
boolean isInterface = false;
|
||||
for (var sq : tq.single_type_qualifier()) {
|
||||
if (sq.storage_qualifier() != null && INTERFACE_STORAGE_QUALIFIERS.contains(sq.storage_qualifier().getText())) {
|
||||
isInterface = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isInterface) continue;
|
||||
for (var sq : tq.single_type_qualifier()) {
|
||||
if (sq.layout_qualifier() != null) blankAllTerminals(sq.layout_qualifier());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void blankLeadingToken(ParserRuleContext ctx) {
|
||||
if (ctx.getChildCount() == 0) return;
|
||||
if (!(ctx.getChild(0) instanceof TerminalNode tn)) return;
|
||||
@@ -524,19 +849,42 @@ public final class RwImageStoreExtractor {
|
||||
});
|
||||
}
|
||||
|
||||
private static String buildComputeOutput(GLSLParser.Translation_unitContext computeRoot, RwExtractMode mode, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared, List<AttributeSlot> attrs) {
|
||||
private static final Set<String> QUAD_INPUTS = Set.of("iris_Vertex", "iris_MultiTexCoord0");
|
||||
|
||||
private static Set<String> declaredQuadInputs(List<GLSLParser.Single_declarationContext> decls) {
|
||||
final Set<String> out = new LinkedHashSet<>();
|
||||
for (var decl : decls) {
|
||||
if (decl.typeless_declaration() == null || decl.typeless_declaration().IDENTIFIER() == null) continue;
|
||||
if (GlslAstHelpers.enclosingOfType(decl, GLSLParser.Function_definitionContext.class) != null) continue;
|
||||
final String name = decl.typeless_declaration().IDENTIFIER().getText();
|
||||
if (QUAD_INPUTS.contains(name)) out.add(name);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static boolean definesFunction(GLSLParser.Translation_unitContext root, String name) {
|
||||
for (var fdef : GlslAstHelpers.collectAll(root, GLSLParser.Function_definitionContext.class)) {
|
||||
final var id = fdef.function_prototype().IDENTIFIER();
|
||||
if (id != null && name.equals(id.getText())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String buildComputeOutput(GLSLParser.Translation_unitContext computeRoot, RwExtractMode mode, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared, List<AttributeSlot> attrs, Set<String> quadInputs, boolean definesFtransform, ReplayLayout replay) {
|
||||
final StringBuilder out = new StringBuilder(8192);
|
||||
out.append("// _vg_mode: ").append(modeTag(mode)).append('\n');
|
||||
if (replay != null) out.append(replay.header());
|
||||
out.append("#version 460 core\n");
|
||||
switch (mode) {
|
||||
case CHUNK -> emitChunkPrelude(out, writtenImages, nonWriteonlyImages, declared, attrs);
|
||||
case COMPOSITE_VSH -> emitCompositeVshPrelude(out, writtenImages, nonWriteonlyImages, declared);
|
||||
case COMPOSITE_VSH -> emitCompositeVshPrelude(out, writtenImages, nonWriteonlyImages, declared, quadInputs, definesFtransform);
|
||||
case COMPOSITE_FSH -> emitCompositeFshPrelude(out, writtenImages, nonWriteonlyImages, declared);
|
||||
case VERTEX_REPLAY -> emitVertexReplayPrelude(out, writtenImages, nonWriteonlyImages, declared, replay);
|
||||
}
|
||||
out.append('\n');
|
||||
out.append(GlslTransformUtils.getFormattedShader(computeRoot, ""));
|
||||
out.append('\n');
|
||||
emitDispatchMain(out, mode, attrs);
|
||||
emitDispatchMain(out, mode, attrs, replay != null ? replay.inputs() : List.of(), quadInputs);
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
@@ -545,19 +893,26 @@ public final class RwImageStoreExtractor {
|
||||
case CHUNK -> "chunk";
|
||||
case COMPOSITE_VSH -> "composite-vsh";
|
||||
case COMPOSITE_FSH -> "composite-fsh";
|
||||
case VERTEX_REPLAY -> "vertex-replay";
|
||||
};
|
||||
}
|
||||
|
||||
private static void emitImageDecls(StringBuilder out, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared) {
|
||||
private static List<String> sortedImages(Set<String> writtenImages) {
|
||||
final List<String> sorted = new ArrayList<>(writtenImages);
|
||||
Collections.sort(sorted);
|
||||
return sorted;
|
||||
}
|
||||
|
||||
private static void emitImageDecls(StringBuilder out, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared) {
|
||||
final List<String> sorted = sortedImages(writtenImages);
|
||||
final Map<String, ImageInformation> infos = activeCustomImages;
|
||||
for (int i = 0; i < sorted.size(); i++) {
|
||||
final String name = sorted.get(i);
|
||||
final ImageDecl d = declared.get(name);
|
||||
if (d == null) continue;
|
||||
final InternalTextureFormat fmt = (infos != null && infos.get(name) != null) ? infos.get(name).internalTextureFormat() : null;
|
||||
final String layoutInner = (fmt != null) ? "binding = " + i + ", " + fmt.name().toLowerCase(Locale.ROOT) : "binding = " + i;
|
||||
final String format = fmt != null ? fmt.name().toLowerCase(Locale.ROOT) : d.layoutFormat();
|
||||
final String layoutInner = (format != null) ? "binding = " + i + ", " + format : "binding = " + i;
|
||||
final String qualifier = nonWriteonlyImages.contains(name) ? "" : "writeonly ";
|
||||
out.append("layout(").append(layoutInner).append(") ").append(qualifier).append("uniform ").append(d.glslType()).append(' ').append(name).append(";\n");
|
||||
}
|
||||
@@ -591,7 +946,7 @@ public final class RwImageStoreExtractor {
|
||||
out.append("void _vert_init() {}\n");
|
||||
}
|
||||
|
||||
private static void emitCompositeVshPrelude(StringBuilder out, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared) {
|
||||
private static void emitCompositeVshPrelude(StringBuilder out, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared, Set<String> quadInputs, boolean definesFtransform) {
|
||||
out.append("layout(local_size_x = 64) in;\n\n");
|
||||
emitImageDecls(out, writtenImages, nonWriteonlyImages, declared);
|
||||
out.append("\nconst vec4 _vg_quad[4] = vec4[4](\n");
|
||||
@@ -601,10 +956,67 @@ public final class RwImageStoreExtractor {
|
||||
out.append(" vec4(0.0, 0.0, 0.0, 1.0));\n");
|
||||
out.append("vec4 _vg_gl_vertex;\n");
|
||||
out.append("int _vg_gl_vertex_id;\n");
|
||||
out.append("vec4 _vg_sink_pos;\n");
|
||||
out.append("float _vg_sink_psize;\n");
|
||||
out.append("float _vg_sink_clip[8];\n");
|
||||
out.append("#define gl_Vertex _vg_gl_vertex\n");
|
||||
out.append("#define gl_VertexID _vg_gl_vertex_id\n");
|
||||
out.append("uniform mat4 gl_ModelViewProjectionMatrix;\n");
|
||||
out.append("vec4 iris_ftransform() { return gl_ModelViewProjectionMatrix * gl_Vertex; }\n");
|
||||
out.append("#define gl_Position _vg_sink_pos\n");
|
||||
out.append("#define gl_PointSize _vg_sink_psize\n");
|
||||
out.append("#define gl_ClipDistance _vg_sink_clip\n");
|
||||
for (String input : quadInputs) out.append("vec4 ").append(input).append(";\n");
|
||||
if (!definesFtransform) {
|
||||
out.append("uniform mat4 gl_ModelViewProjectionMatrix;\n");
|
||||
out.append("vec4 iris_ftransform() { return gl_ModelViewProjectionMatrix * gl_Vertex; }\n");
|
||||
}
|
||||
}
|
||||
|
||||
private static void emitVertexReplayPrelude(StringBuilder out, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared, ReplayLayout replay) {
|
||||
final List<VertexInput> inputs = replay.inputs();
|
||||
out.append("layout(local_size_x = 64) in;\n\n");
|
||||
emitImageDecls(out, writtenImages, nonWriteonlyImages, declared);
|
||||
final Map<String, ImageInformation> infos = activeCustomImages;
|
||||
for (int i = 0; i < replay.readImages().size(); i++) {
|
||||
final ImageDecl d = declared.get(replay.readImages().get(i));
|
||||
final InternalTextureFormat fmt = infos.get(d.name()) != null ? infos.get(d.name()).internalTextureFormat() : null;
|
||||
final String format = fmt != null ? fmt.name().toLowerCase(Locale.ROOT) : d.layoutFormat();
|
||||
out.append("layout(binding = ").append(writtenImages.size() + i).append(format != null ? ", " + format : "").append(") readonly uniform ")
|
||||
.append(d.glslType()).append(' ').append(d.name()).append(";\n");
|
||||
}
|
||||
out.append('\n');
|
||||
for (int i = 0; i < VG_REPLAY_VERTEX_BUFFER_COUNT; i++) {
|
||||
out.append("layout(std430, binding = ").append(VG_REPLAY_VERTEX_BUFFER_BINDING + i)
|
||||
.append(") readonly buffer _VgVertices").append(i).append(" { uint d[]; } _vg_vertices").append(i).append(";\n");
|
||||
}
|
||||
out.append("layout(std430, binding = ").append(VG_REPLAY_INDEX_BUFFER_BINDING).append(") readonly buffer _VgIndices { uint d[]; } _vg_indices;\n\n");
|
||||
final int slots = Math.max(1, inputs.size());
|
||||
out.append("uniform uvec4 _vg_attr[").append(slots).append("];\n");
|
||||
out.append("uniform vec4 _vg_attrDefault[").append(slots).append("];\n");
|
||||
out.append("uniform ivec4 _vg_draw;\n");
|
||||
out.append("uniform ivec3 _vg_index;\n");
|
||||
out.append("uniform uvec2 _vg_restart;\n");
|
||||
out.append("uniform int _vg_invocationBase;\n\n");
|
||||
out.append("vec4 _vg_sink_pos;\n");
|
||||
out.append("float _vg_sink_psize;\n");
|
||||
out.append("float _vg_sink_clip[8];\n");
|
||||
out.append("int _vg_vertex_id;\n");
|
||||
out.append("int _vg_instance_id;\n");
|
||||
out.append("#define gl_Position _vg_sink_pos\n");
|
||||
out.append("#define gl_PointSize _vg_sink_psize\n");
|
||||
out.append("#define gl_ClipDistance _vg_sink_clip\n");
|
||||
out.append("#define gl_VertexID _vg_vertex_id\n");
|
||||
out.append("#define gl_VertexIndex _vg_vertex_id\n");
|
||||
out.append("#define gl_InstanceID _vg_instance_id\n");
|
||||
out.append("#define gl_InstanceIndex _vg_instance_id\n");
|
||||
for (String suffix : new String[] { "", "ARB" }) {
|
||||
out.append("#define gl_BaseVertex").append(suffix).append(" _vg_draw.w\n");
|
||||
out.append("#define gl_BaseInstance").append(suffix).append(" 0\n");
|
||||
out.append("#define gl_DrawID").append(suffix).append(" 0\n");
|
||||
}
|
||||
out.append('\n');
|
||||
for (VertexInput input : inputs) out.append(input.glslType()).append(' ').append(input.name()).append(";\n");
|
||||
out.append('\n');
|
||||
out.append(ShaderLoader.getShaderSource("angelica:include/vertex_replay_fetch.glsl"));
|
||||
}
|
||||
|
||||
private static void emitCompositeFshPrelude(StringBuilder out, Set<String> writtenImages, Set<String> nonWriteonlyImages, Map<String, ImageDecl> declared) {
|
||||
@@ -630,7 +1042,7 @@ public final class RwImageStoreExtractor {
|
||||
}
|
||||
}
|
||||
|
||||
private static void emitDispatchMain(StringBuilder out, RwExtractMode mode, List<AttributeSlot> attrs) {
|
||||
private static void emitDispatchMain(StringBuilder out, RwExtractMode mode, List<AttributeSlot> attrs, List<VertexInput> inputs, Set<String> quadInputs) {
|
||||
out.append("void main() {\n");
|
||||
switch (mode) {
|
||||
case CHUNK -> {
|
||||
@@ -652,6 +1064,7 @@ public final class RwImageStoreExtractor {
|
||||
out.append(" uint vid = gl_GlobalInvocationID.x;\n");
|
||||
out.append(" if (vid >= 4u) return;\n");
|
||||
out.append(" _vg_gl_vertex = _vg_quad[vid];\n");
|
||||
for (String input : quadInputs) out.append(" ").append(input).append(" = _vg_quad[vid];\n");
|
||||
out.append(" _vg_gl_vertex_id = int(vid);\n");
|
||||
out.append(" _vg_body();\n");
|
||||
}
|
||||
@@ -661,10 +1074,37 @@ public final class RwImageStoreExtractor {
|
||||
out.append(" _vg_gl_fragcoord_raw = vec4(vec2(px) + 0.5, 0.0, 1.0);\n");
|
||||
out.append(" _vg_body();\n");
|
||||
}
|
||||
case VERTEX_REPLAY -> {
|
||||
out.append(" uint _vg_g = uint(_vg_invocationBase) + gl_GlobalInvocationID.x;\n");
|
||||
out.append(" uint _vg_count = uint(_vg_draw.y);\n");
|
||||
out.append(" if (_vg_count == 0u || _vg_g >= _vg_count * uint(_vg_draw.z)) return;\n");
|
||||
out.append(" uint _vg_v = _vg_g % _vg_count;\n");
|
||||
out.append(" uint _vg_i = _vg_g / _vg_count;\n");
|
||||
out.append(" uint _vg_vertex = uint(_vg_draw.x) + _vg_v;\n");
|
||||
out.append(" if (_vg_index.x != 0) {\n");
|
||||
out.append(" uint _vg_index_value = _vg_v < uint(_vg_index.z) ? _vg_read(4u, uint(_vg_index.y) + _vg_v * uint(_vg_index.x), uint(_vg_index.x)) : 0u;\n");
|
||||
out.append(" if (_vg_restart.x != 0u && _vg_index_value == _vg_restart.y) return;\n");
|
||||
out.append(" _vg_vertex = _vg_index_value + uint(_vg_draw.w);\n");
|
||||
out.append(" }\n");
|
||||
out.append(" _vg_vertex_id = int(_vg_vertex);\n");
|
||||
out.append(" _vg_instance_id = int(_vg_i);\n");
|
||||
for (int k = 0; k < inputs.size(); k++) {
|
||||
final VertexInput input = inputs.get(k);
|
||||
out.append(" ").append(input.name()).append(" = ").append(input.glslType()).append('(')
|
||||
.append(fetchFunctionFor(input.glslType())).append('(').append(k).append("u, _vg_vertex, _vg_i));\n");
|
||||
}
|
||||
out.append(" _vg_body();\n");
|
||||
}
|
||||
}
|
||||
out.append("}\n");
|
||||
}
|
||||
|
||||
private static String fetchFunctionFor(String glslType) {
|
||||
if (glslType.startsWith("u")) return "_vg_uint";
|
||||
if (glslType.startsWith("i")) return "_vg_int";
|
||||
return "_vg_float";
|
||||
}
|
||||
|
||||
private static List<AttributeSlot> collectVertexAttributes(Set<String> chunkAttrsUsed, Map<String, String> chunkAttrAstTypes) {
|
||||
final List<AttributeSlot> out = new ArrayList<>();
|
||||
for (String name : chunkAttrsUsed) {
|
||||
|
||||
@@ -35,6 +35,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -237,7 +238,8 @@ public class ShaderTransformer {
|
||||
final ShaderDiskCache.Key k = ShaderDiskCache.key("iris-transform").str(patch.name());
|
||||
parameters.appendDiskKey(k);
|
||||
k.i(RenderSystem.getMaxGlslVersion()).b(RenderSystem.supportsSSBO()).b(RenderSystem.supportsImageLoadStore())
|
||||
.b(RenderSystem.isGLES()).b(BackendManager.RENDER_BACKEND.isSDLGPU()).i(maxSupportedHoistVersion);
|
||||
.b(RenderSystem.isGLES()).b(BackendManager.RENDER_BACKEND.isSDLGPU()).b(BackendManager.RENDER_BACKEND.supportsComputeImageAtomics()).str(RwImageStoreExtractor.atomicCustomImagesKey())
|
||||
.i(maxSupportedHoistVersion);
|
||||
if (enabledRequirements == null) {
|
||||
k.i(-1);
|
||||
} else {
|
||||
@@ -559,36 +561,73 @@ public class ShaderTransformer {
|
||||
artifactsOut.put(shaderType, new StageArtifact(treeHolder[0], formattedShader.length() - printedBody.length()));
|
||||
}
|
||||
}
|
||||
if (BackendManager.RENDER_BACKEND.isSDLGPU()) extractRwImageStores(result, patchType, artifactsOut);
|
||||
if (BackendManager.RENDER_BACKEND.isSDLGPU()) extractRwImageStores(result, patchType, artifactsOut, RwImageStoreExtractor::supportsAtomics);
|
||||
watch.stop();
|
||||
Iris.logger.info("[Load #{}] Transformed shader for {} in {}", Iris.getShaderPackLoadId(), patchType.name(), watch);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void extractRwImageStores(EnumMap<PatchShaderType, String> result, Patch patchType, EnumMap<PatchShaderType, StageArtifact> artifactsOut) {
|
||||
static void extractRwImageStores(EnumMap<PatchShaderType, String> result, Patch patchType, EnumMap<PatchShaderType, StageArtifact> artifactsOut, Predicate<String> imageAtomics) {
|
||||
if (result.containsKey(PatchShaderType.COMPUTE)) return;
|
||||
if (patchType == Patch.ATTRIBUTES) {
|
||||
extractVertexReplay(result, artifactsOut, imageAtomics);
|
||||
return;
|
||||
}
|
||||
|
||||
final String vsh = result.get(PatchShaderType.VERTEX);
|
||||
final String fsh = result.get(PatchShaderType.FRAGMENT);
|
||||
final RwImageStoreExtractor.Result vshResult = (vsh != null) ? RwImageStoreExtractor.tryExtract(vsh, PatchShaderType.VERTEX, patchType.name()) : null;
|
||||
final RwImageStoreExtractor.RwExtractMode vertexMode = patchType == Patch.COMPOSITE ? RwImageStoreExtractor.RwExtractMode.COMPOSITE_VSH : null;
|
||||
final RwImageStoreExtractor.Result vshResult = (vsh != null) ? RwImageStoreExtractor.tryExtract(vsh, PatchShaderType.VERTEX, patchType.name(), vertexMode) : null;
|
||||
final RwImageStoreExtractor.Result fshResult = (fsh != null) ? RwImageStoreExtractor.tryExtract(fsh, PatchShaderType.FRAGMENT, patchType.name()) : null;
|
||||
|
||||
if (vshResult != null && fshResult != null) {
|
||||
throw new RuntimeException("Program " + patchType.name() + " writes images from both VSH and FSH; only one stage may write per program under SDL_GPU");
|
||||
}
|
||||
if (vshResult != null) {
|
||||
result.put(PatchShaderType.VERTEX, vshResult.strippedSource());
|
||||
result.put(PatchShaderType.COMPUTE, vshResult.computeSource());
|
||||
if (artifactsOut != null) artifactsOut.remove(PatchShaderType.VERTEX);
|
||||
Iris.logger.info("[RwImageStoreExtractor] Extracted compute pre-pass for {} (mode={}, written={})", patchType.name(), vshResult.mode(), vshResult.writtenImages());
|
||||
} else if (fshResult != null) {
|
||||
result.put(PatchShaderType.FRAGMENT, fshResult.strippedSource());
|
||||
result.put(PatchShaderType.COMPUTE, fshResult.computeSource());
|
||||
if (artifactsOut != null) artifactsOut.remove(PatchShaderType.FRAGMENT);
|
||||
Iris.logger.info("[RwImageStoreExtractor] Extracted compute pre-pass for {} (mode={}, written={})", patchType.name(), fshResult.mode(), fshResult.writtenImages());
|
||||
final RwImageStoreExtractor.Result extracted = vshResult != null ? vshResult : fshResult;
|
||||
if (extracted == null) return;
|
||||
final PatchShaderType stage = vshResult != null ? PatchShaderType.VERTEX : PatchShaderType.FRAGMENT;
|
||||
result.put(stage, extracted.strippedSource());
|
||||
if (artifactsOut != null) artifactsOut.remove(stage);
|
||||
if (patchType != Patch.COMPOSITE && patchType != Patch.CELERITAS_TERRAIN) {
|
||||
Iris.logger.info("[RwImageStoreExtractor] Dropped image writes from {} (written={})", patchType.name(), extracted.writtenImages());
|
||||
} else if (needsUnsupportedAtomics(extracted, imageAtomics)) {
|
||||
Iris.logger.info("[RwImageStoreExtractor] Dropped image writes from {}, they need image atomics this backend cannot run (written={})", patchType.name(), extracted.writtenImages());
|
||||
} else {
|
||||
result.put(PatchShaderType.COMPUTE, extracted.computeSource());
|
||||
Iris.logger.info("[RwImageStoreExtractor] Extracted compute pre-pass for {} (mode={}, written={})", patchType.name(), extracted.mode(), extracted.writtenImages());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean needsUnsupportedAtomics(RwImageStoreExtractor.Result extracted, Predicate<String> imageAtomics) {
|
||||
return !extracted.atomicImages().stream().allMatch(imageAtomics);
|
||||
}
|
||||
|
||||
private static void extractVertexReplay(EnumMap<PatchShaderType, String> result, EnumMap<PatchShaderType, StageArtifact> artifactsOut, Predicate<String> imageAtomics) {
|
||||
final String fsh = result.get(PatchShaderType.FRAGMENT);
|
||||
final RwImageStoreExtractor.Result fshResult = (fsh != null) ? RwImageStoreExtractor.tryExtract(fsh, PatchShaderType.FRAGMENT, Patch.ATTRIBUTES.name()) : null;
|
||||
if (fshResult != null) {
|
||||
result.put(PatchShaderType.FRAGMENT, fshResult.strippedSource());
|
||||
if (artifactsOut != null) artifactsOut.remove(PatchShaderType.FRAGMENT);
|
||||
Iris.logger.info("[RwImageStoreExtractor] Dropped fragment image writes from ATTRIBUTES (written={})", fshResult.writtenImages());
|
||||
}
|
||||
|
||||
final String vsh = result.get(PatchShaderType.VERTEX);
|
||||
final RwImageStoreExtractor.Result vshResult = (vsh != null) ? RwImageStoreExtractor.tryExtractVertexReplay(vsh) : null;
|
||||
if (vshResult == null) return;
|
||||
result.put(PatchShaderType.VERTEX, vshResult.strippedSource());
|
||||
if (artifactsOut != null) artifactsOut.remove(PatchShaderType.VERTEX);
|
||||
if (vshResult.computeSource() == null) {
|
||||
Iris.logger.info("[RwImageStoreExtractor] Dropped vertex image writes from ATTRIBUTES, an input cannot be replayed (written={})", vshResult.writtenImages());
|
||||
return;
|
||||
}
|
||||
if (needsUnsupportedAtomics(vshResult, imageAtomics)) {
|
||||
Iris.logger.info("[RwImageStoreExtractor] Dropped vertex image writes from ATTRIBUTES, they need image atomics this backend cannot run (written={})", vshResult.writtenImages());
|
||||
return;
|
||||
}
|
||||
result.put(PatchShaderType.COMPUTE, vshResult.computeSource());
|
||||
Iris.logger.info("[RwImageStoreExtractor] Extracted vertex replay for ATTRIBUTES (written={})", vshResult.writtenImages());
|
||||
}
|
||||
|
||||
private static void doTransform(Transformer transformer, Patch patchType, Parameters parameters, int versionInt) {
|
||||
switch (patchType) {
|
||||
case CELERITAS_TERRAIN:
|
||||
@@ -717,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();
|
||||
}
|
||||
|
||||
@@ -8,15 +8,25 @@ public class ConstDirectiveParser {
|
||||
public static List<ConstDirective> findDirectives(String source) {
|
||||
List<ConstDirective> directives = new ArrayList<>();
|
||||
|
||||
// Match any valid newline sequence
|
||||
// https://stackoverflow.com/a/31060125
|
||||
for (String line : source.split("\\R")) {
|
||||
findDirectiveInLine(line).ifPresent(directives::add);
|
||||
int from = 0;
|
||||
while (true) {
|
||||
final int match = source.indexOf("const", from);
|
||||
if (match < 0) break;
|
||||
int start = match;
|
||||
while (start > 0 && !isLineBreak(source.charAt(start - 1))) start--;
|
||||
int end = match + "const".length();
|
||||
while (end < source.length() && !isLineBreak(source.charAt(end))) end++;
|
||||
findDirectiveInLine(source.substring(start, end)).ifPresent(directives::add);
|
||||
from = end;
|
||||
}
|
||||
|
||||
return directives;
|
||||
}
|
||||
|
||||
private static boolean isLineBreak(char c) {
|
||||
return c == '\n' || c == '\r' || c == '\u000B' || c == '\f' || c == '\u0085' || c == '\u2028' || c == '\u2029';
|
||||
}
|
||||
|
||||
public static Optional<ConstDirective> findDirectiveInLine(String line) {
|
||||
// Valid const directives contain the following elements:
|
||||
// * Zero or more whitespace characters
|
||||
|
||||
@@ -81,8 +81,18 @@ public class IdMap {
|
||||
* Detects whether a pack's block.properties has a dedicated 1.7.10 section.
|
||||
*/
|
||||
public static boolean detectLegacySection(Path shaderPath) {
|
||||
String rawBlockProperties = readProperties(shaderPath, "block.properties");
|
||||
return rawBlockProperties != null && LEGACY_DIRECTIVE_PATTERN.matcher(rawBlockProperties).find();
|
||||
final String rawBlockProperties = readProperties(shaderPath, "block.properties");
|
||||
if (rawBlockProperties == null) return false;
|
||||
int from = 0;
|
||||
while (true) {
|
||||
final int match = rawBlockProperties.indexOf("10710", from);
|
||||
if (match < 0) return false;
|
||||
final int start = rawBlockProperties.lastIndexOf('\n', match) + 1;
|
||||
int end = rawBlockProperties.indexOf('\n', match);
|
||||
if (end < 0) end = rawBlockProperties.length();
|
||||
if (LEGACY_DIRECTIVE_PATTERN.matcher(rawBlockProperties.substring(start, end)).find()) return true;
|
||||
from = end;
|
||||
}
|
||||
}
|
||||
|
||||
public static String modernFallbackMcVersion() {
|
||||
@@ -90,9 +100,8 @@ public class IdMap {
|
||||
? String.valueOf(AngelicaConfig.modernFallbackMcVersion) : "260101";
|
||||
}
|
||||
|
||||
IdMap(Path shaderPath, ShaderPackOptions shaderPackOptions, Iterable<StringPair> environmentDefines) {
|
||||
// Check if block.properties has a dedicated 1.7.10 section
|
||||
this.hasLegacySection = detectLegacySection(shaderPath);
|
||||
IdMap(Path shaderPath, boolean hasLegacySection, ShaderPackOptions shaderPackOptions, Iterable<StringPair> environmentDefines) {
|
||||
this.hasLegacySection = hasLegacySection;
|
||||
|
||||
Iterable<StringPair> resolvedDefines;
|
||||
if (this.hasLegacySection) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.gtnewhorizons.angelica.glsm.threading.AngelicaWorkers;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||
@@ -36,6 +37,7 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
@@ -53,7 +55,10 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ShaderPack {
|
||||
@@ -79,6 +84,7 @@ public class ShaderPack {
|
||||
private final boolean mappedByDimensionProperties;
|
||||
private final String legacyDefaultFolder;
|
||||
private final Function<AbsolutePackPath, String> sourceProvider;
|
||||
private final ImmutableList<AbsolutePackPath> sourceFiles;
|
||||
private final ShaderProperties shaderProperties;
|
||||
private boolean hasLoggedCacheLimitReached = false;
|
||||
|
||||
@@ -90,7 +96,8 @@ public class ShaderPack {
|
||||
@Getter private final Object2ObjectMap<String, ImageInformation> customImages;
|
||||
@Getter private final Int2ObjectMap<ShaderStorageInfo> bufferObjects;
|
||||
@Getter private final ShaderPackOptions shaderPackOptions;
|
||||
@Getter private final OptionMenuContainer menuContainer;
|
||||
private final ProfileSet profiles;
|
||||
private OptionMenuContainer menuContainer;
|
||||
|
||||
private final ProfileSet.ProfileResult profile;
|
||||
private final String profileInfo;
|
||||
@@ -200,8 +207,18 @@ public class ShaderPack {
|
||||
}
|
||||
this.legacyDefaultFolder = legacyDefault;
|
||||
|
||||
// Only the options screen reads translations, so they load alongside the rest of the pack
|
||||
final CompletableFuture<LanguageMap> languageMapFuture = AngelicaWorkers.submit(() -> {
|
||||
try {
|
||||
return new LanguageMap(root.resolve("lang"));
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
});
|
||||
|
||||
// Read all files and included files recursively
|
||||
IncludeGraph graph = new IncludeGraph(root, starts.build());
|
||||
this.sourceFiles = starts.build();
|
||||
IncludeGraph graph = new IncludeGraph(root, sourceFiles);
|
||||
|
||||
if (!graph.getFailures().isEmpty()) {
|
||||
graph.getFailures().forEach((path, error) -> {
|
||||
@@ -211,13 +228,14 @@ public class ShaderPack {
|
||||
throw new IOException("Failed to resolve some #include directives, see previous messages for details");
|
||||
}
|
||||
|
||||
this.languageMap = new LanguageMap(root.resolve("lang"));
|
||||
|
||||
// Discover, merge, and apply shader pack options
|
||||
this.shaderPackOptions = new ShaderPackOptions(graph, changedConfigs);
|
||||
graph = this.shaderPackOptions.getIncludes();
|
||||
|
||||
final String mcVersionOverride = IdMap.detectLegacySection(root) ? null : IdMap.modernFallbackMcVersion();
|
||||
final boolean hasLegacyIdSection = IdMap.detectLegacySection(root);
|
||||
final String mcVersionOverride = hasLegacyIdSection ? null : IdMap.modernFallbackMcVersion();
|
||||
final ShaderPackOptions options = this.shaderPackOptions;
|
||||
final CompletableFuture<IdMap> idMapFuture = AngelicaWorkers.submit(() -> new IdMap(root, hasLegacyIdSection, options, environmentDefines));
|
||||
|
||||
List<StringPair> finalEnvironmentDefines = new ArrayList<>();
|
||||
for (StringPair define : environmentDefines) {
|
||||
@@ -275,7 +293,7 @@ public class ShaderPack {
|
||||
}
|
||||
});
|
||||
|
||||
this.menuContainer = new OptionMenuContainer(shaderProperties, this.shaderPackOptions, profiles);
|
||||
this.profiles = profiles;
|
||||
|
||||
{
|
||||
String profileName = getCurrentProfileName();
|
||||
@@ -305,38 +323,39 @@ public class ShaderPack {
|
||||
return null;
|
||||
}
|
||||
|
||||
ImmutableList<String> lines = includeProcessor.getIncludedFile(path);
|
||||
final byte[] contentHash = includeProcessor.getIncludedFileHash(path);
|
||||
|
||||
if (lines == null) {
|
||||
if (contentHash == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
for (String line : lines) {
|
||||
builder.append(line);
|
||||
builder.append('\n');
|
||||
}
|
||||
|
||||
// Apply GLSL preprocessor to source, while making environment defines available.
|
||||
//
|
||||
// This uses similar techniques to the *.properties preprocessor to avoid actually putting
|
||||
// #define statements in the actual source - instead, we tell the preprocessor about them
|
||||
// directly. This removes one obstacle to accurate reporting of line numbers for errors,
|
||||
// though there exist many more (such as relocating all #extension directives and similar things)
|
||||
String source = builder.toString();
|
||||
return JcppProcessor.glslPreprocessSource(contentHash, () -> {
|
||||
final ImmutableList<String> lines = includeProcessor.getIncludedFile(path);
|
||||
int length = 0;
|
||||
for (String line : lines) {
|
||||
length += line.length() + 1;
|
||||
}
|
||||
|
||||
// Apply shader pack workarounds for version compatibility (before preprocessing)
|
||||
source = ShaderPackWorkarounds.apply(source);
|
||||
final StringBuilder builder = new StringBuilder(length);
|
||||
|
||||
source = JcppProcessor.glslPreprocessSource(source, finalEnvironmentDefines1);
|
||||
for (String line : lines) {
|
||||
builder.append(line);
|
||||
builder.append('\n');
|
||||
}
|
||||
|
||||
return source;
|
||||
// Apply shader pack workarounds for version compatibility (before preprocessing)
|
||||
return ShaderPackWorkarounds.apply(builder.toString());
|
||||
}, finalEnvironmentDefines1);
|
||||
};
|
||||
|
||||
this.base = new ProgramSet(AbsolutePackPath.fromAbsolutePath("/"), sourceProvider, shaderProperties, this);
|
||||
|
||||
this.idMap = new IdMap(root, shaderPackOptions, environmentDefines);
|
||||
final AbsolutePackPath rootDirectory = AbsolutePackPath.fromAbsolutePath("/");
|
||||
this.base = new ProgramSet(rootDirectory, preprocessInParallel(rootDirectory), shaderProperties, this);
|
||||
|
||||
customNoiseTexture = shaderProperties.getNoiseTexturePath().map(path -> {
|
||||
try {
|
||||
@@ -375,10 +394,35 @@ public class ShaderPack {
|
||||
this.customImages = shaderProperties.getCustomImages();
|
||||
this.bufferObjects = shaderProperties.getBufferObjects();
|
||||
|
||||
this.idMap = await(idMapFuture);
|
||||
this.languageMap = await(languageMapFuture);
|
||||
}
|
||||
|
||||
// Only the options screen reads the menu, don't build unless it's needed
|
||||
public OptionMenuContainer getMenuContainer() {
|
||||
if (menuContainer == null) {
|
||||
menuContainer = new OptionMenuContainer(shaderProperties, shaderPackOptions, profiles);
|
||||
}
|
||||
return menuContainer;
|
||||
}
|
||||
|
||||
/** Publishes this pack's custom images to the shader transformers; call when the pack becomes the current one. */
|
||||
public void activate() {
|
||||
SamplerToStorageImageRewriter.setActiveCandidates(SamplerToStorageImageRewriter.buildCandidates(this.customImages.values()));
|
||||
RwImageStoreExtractor.setActiveCustomImages(this.customImages);
|
||||
}
|
||||
|
||||
private static <T> T await(CompletableFuture<T> future) throws IOException {
|
||||
try {
|
||||
return future.join();
|
||||
} catch (CompletionException e) {
|
||||
if (e.getCause() instanceof UncheckedIOException io) throw io.getCause();
|
||||
if (e.getCause() instanceof RuntimeException runtime) throw runtime;
|
||||
if (e.getCause() instanceof Error error) throw error;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private String getCurrentProfileName() {
|
||||
return profile.current.map(p -> p.name).orElse("Custom");
|
||||
}
|
||||
@@ -667,6 +711,26 @@ public class ShaderPack {
|
||||
}
|
||||
}
|
||||
|
||||
private Function<AbsolutePackPath, String> preprocessInParallel(AbsolutePackPath directory) {
|
||||
final String directoryPath = directory.getPathString();
|
||||
final String prefix = directoryPath.endsWith("/") ? directoryPath : directoryPath + "/";
|
||||
final List<AbsolutePackPath> files = new ArrayList<>();
|
||||
final List<Supplier<String>> tasks = new ArrayList<>();
|
||||
for (AbsolutePackPath file : sourceFiles) {
|
||||
final String path = file.getPathString();
|
||||
if (path.startsWith(prefix) && path.indexOf('/', prefix.length()) < 0) {
|
||||
files.add(file);
|
||||
tasks.add(() -> sourceProvider.apply(file));
|
||||
}
|
||||
}
|
||||
final List<String> sources = AngelicaWorkers.invokeAll(tasks);
|
||||
final Map<AbsolutePackPath, String> preprocessed = new HashMap<>();
|
||||
for (int i = 0; i < files.size(); i++) {
|
||||
preprocessed.put(files.get(i), sources.get(i));
|
||||
}
|
||||
return path -> preprocessed.containsKey(path) ? preprocessed.get(path) : sourceProvider.apply(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or creates the appropriate ProgramSet for the given dimension.
|
||||
*
|
||||
@@ -721,12 +785,8 @@ public class ShaderPack {
|
||||
if (programSet == null) {
|
||||
// Create ProgramSet on-demand for dimension folder
|
||||
try {
|
||||
programSet = new ProgramSet(
|
||||
AbsolutePackPath.fromAbsolutePath("/" + folderName),
|
||||
sourceProvider,
|
||||
shaderProperties,
|
||||
this
|
||||
);
|
||||
final AbsolutePackPath directory = AbsolutePackPath.fromAbsolutePath("/" + folderName);
|
||||
programSet = new ProgramSet(directory, preprocessInParallel(directory), shaderProperties, this);
|
||||
dimensionProgramSets.put(folderName, programSet);
|
||||
|
||||
// Check cache size limit and evict LRU dimensions if needed
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.coderbot.iris.shaderpack.include;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.gtnewhorizons.angelica.glsm.threading.AngelicaWorkers;
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.shaderpack.error.RusticError;
|
||||
import net.coderbot.iris.shaderpack.transform.line.LineTransform;
|
||||
@@ -18,6 +19,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -75,14 +77,18 @@ public class IncludeGraph {
|
||||
List<AbsolutePackPath> queue = new ArrayList<>(startingPaths);
|
||||
Set<AbsolutePackPath> seen = new HashSet<>(startingPaths);
|
||||
|
||||
Map<AbsolutePackPath, CompletableFuture<ReadResult>> reads = new HashMap<>();
|
||||
for (AbsolutePackPath start : startingPaths) {
|
||||
reads.put(start, readLater(root, start));
|
||||
}
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
AbsolutePackPath next = queue.remove(queue.size() - 1);
|
||||
|
||||
String source;
|
||||
final ReadResult read = reads.remove(next).join();
|
||||
final IOException e = read.failure();
|
||||
|
||||
try {
|
||||
source = readFile(next.resolved(root));
|
||||
} catch (IOException e) {
|
||||
if (e != null) {
|
||||
AbsolutePackPath src = cameFrom.get(next);
|
||||
|
||||
if (src == null) {
|
||||
@@ -110,7 +116,7 @@ public class IncludeGraph {
|
||||
continue;
|
||||
}
|
||||
|
||||
ImmutableList<String> lines = ImmutableList.copyOf(source.split("\\R"));
|
||||
ImmutableList<String> lines = read.lines();
|
||||
|
||||
FileNode node = new FileNode(next, lines);
|
||||
boolean selfInclude = false;
|
||||
@@ -128,6 +134,7 @@ public class IncludeGraph {
|
||||
} else if (!seen.contains(included)) {
|
||||
queue.add(included);
|
||||
seen.add(included);
|
||||
reads.put(included, readLater(root, included));
|
||||
cameFrom.put(included, next);
|
||||
lineNumberInclude.put(included, line);
|
||||
}
|
||||
@@ -254,6 +261,18 @@ public class IncludeGraph {
|
||||
return failures;
|
||||
}
|
||||
|
||||
private record ReadResult(ImmutableList<String> lines, IOException failure) {}
|
||||
|
||||
private static CompletableFuture<ReadResult> readLater(Path root, AbsolutePackPath path) {
|
||||
return AngelicaWorkers.submit(() -> {
|
||||
try {
|
||||
return new ReadResult(ImmutableList.copyOf(readFile(path.resolved(root)).split("\\R")), null);
|
||||
} catch (IOException e) {
|
||||
return new ReadResult(null, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static String readFile(Path path) throws IOException {
|
||||
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@@ -3,18 +3,69 @@ package net.coderbot.iris.shaderpack.include;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
// TODO: Write tests for this code
|
||||
public class IncludeProcessor {
|
||||
private final IncludeGraph graph;
|
||||
private final Map<AbsolutePackPath, ImmutableList<String>> cache;
|
||||
|
||||
private final Map<AbsolutePackPath, byte[]> hashes;
|
||||
|
||||
public IncludeProcessor(IncludeGraph graph) {
|
||||
this.graph = graph;
|
||||
this.cache = new HashMap<>();
|
||||
this.cache = new ConcurrentHashMap<>();
|
||||
this.hashes = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public byte[] getIncludedFileHash(AbsolutePackPath path) {
|
||||
byte[] hash = hashes.get(path);
|
||||
|
||||
if (hash == null) {
|
||||
hash = hash(path);
|
||||
if (hash != null) hashes.put(path, hash);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
private byte[] hash(AbsolutePackPath path) {
|
||||
final FileNode fileNode = graph.getNodes().get(path);
|
||||
|
||||
if (fileNode == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
final ImmutableList<String> lines = fileNode.getLines();
|
||||
final ImmutableMap<Integer, AbsolutePackPath> includes = fileNode.getIncludes();
|
||||
|
||||
// A line can't contain '\n' and an include is a fixed 32 bytes, so the encoding is unambiguous
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
final AbsolutePackPath include = includes.get(i);
|
||||
|
||||
if (include != null) {
|
||||
digest.update((byte) 1);
|
||||
digest.update(Objects.requireNonNull(getIncludedFileHash(include)));
|
||||
} else {
|
||||
digest.update((byte) 0);
|
||||
digest.update(lines.get(i).getBytes(StandardCharsets.UTF_8));
|
||||
digest.update((byte) '\n');
|
||||
}
|
||||
}
|
||||
|
||||
return digest.digest();
|
||||
}
|
||||
|
||||
// TODO: Actual error handling
|
||||
@@ -24,7 +75,7 @@ public class IncludeProcessor {
|
||||
|
||||
if (lines == null) {
|
||||
lines = process(path);
|
||||
cache.put(path, lines);
|
||||
if (lines != null) cache.put(path, lines);
|
||||
}
|
||||
|
||||
return lines;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.coderbot.iris.shaderpack.preprocessor;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache;
|
||||
import net.coderbot.iris.shaderpack.StringPair;
|
||||
import org.anarres.cpp.Feature;
|
||||
import org.anarres.cpp.LexerException;
|
||||
@@ -7,7 +8,18 @@ import org.anarres.cpp.Preprocessor;
|
||||
import org.anarres.cpp.StringLexerSource;
|
||||
import org.anarres.cpp.Token;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class JcppProcessor {
|
||||
public static String glslPreprocessSource(byte[] contentHash, Supplier<String> source, Iterable<StringPair> environmentDefines) {
|
||||
final ShaderDiskCache.Key cacheKey = PreprocessedSourceCache.key("glsl", contentHash, environmentDefines);
|
||||
final String cached = PreprocessedSourceCache.get(cacheKey);
|
||||
if (cached != null) return cached;
|
||||
final String preprocessed = glslPreprocessSource(source.get(), environmentDefines);
|
||||
PreprocessedSourceCache.put(cacheKey, preprocessed);
|
||||
return preprocessed;
|
||||
}
|
||||
|
||||
// Derived from GlShader from Canvas, licenced under LGPL
|
||||
public static String glslPreprocessSource(String source, Iterable<StringPair> environmentDefines) {
|
||||
if (source.contains(GlslCollectingListener.VERSION_MARKER)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package net.coderbot.iris.shaderpack.preprocessor;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache;
|
||||
import net.coderbot.iris.shaderpack.StringPair;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/** Disk cache of preprocessor output, keyed on the input and every macro handed to the preprocessor. */
|
||||
final class PreprocessedSourceCache {
|
||||
private PreprocessedSourceCache() {}
|
||||
|
||||
@Nullable
|
||||
static ShaderDiskCache.Key key(String kind, String source, Iterable<StringPair> defines) {
|
||||
return ShaderDiskCache.isEnabled() ? withDefines(ShaderDiskCache.key("iris-preprocess").str(kind).str(source), defines) : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static ShaderDiskCache.Key key(String kind, byte[] contentHash, Iterable<StringPair> defines) {
|
||||
return ShaderDiskCache.isEnabled() ? withDefines(ShaderDiskCache.key("iris-preprocess").str(kind).bytes(contentHash), defines) : null;
|
||||
}
|
||||
|
||||
private static ShaderDiskCache.Key withDefines(ShaderDiskCache.Key key, Iterable<StringPair> defines) {
|
||||
for (StringPair define : defines) {
|
||||
key.str(define.getKey()).str(define.getValue());
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static String get(@Nullable ShaderDiskCache.Key key) {
|
||||
return key == null ? null : ShaderDiskCache.getString(key);
|
||||
}
|
||||
|
||||
static void put(@Nullable ShaderDiskCache.Key key, String preprocessed) {
|
||||
if (key != null) ShaderDiskCache.putString(key, preprocessed);
|
||||
}
|
||||
}
|
||||
+54
-18
@@ -1,5 +1,6 @@
|
||||
package net.coderbot.iris.shaderpack.preprocessor;
|
||||
|
||||
import com.gtnewhorizons.angelica.glsm.shader.ShaderDiskCache;
|
||||
import net.coderbot.iris.Iris;
|
||||
import net.coderbot.iris.shaderpack.StringPair;
|
||||
import net.coderbot.iris.shaderpack.option.ShaderPackOptions;
|
||||
@@ -17,7 +18,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PropertiesPreprocessor {
|
||||
// Derived from ShaderProcessor.glslPreprocessSource, which is derived from GlShader from Canvas, licenced under LGPL
|
||||
@@ -29,6 +29,14 @@ public class PropertiesPreprocessor {
|
||||
final List<String> booleanValues = getBooleanValues(shaderPackOptions);
|
||||
final Map<String, String> stringValues = getStringValues(shaderPackOptions);
|
||||
|
||||
final List<StringPair> macros = new ArrayList<>();
|
||||
for (String value : booleanValues) macros.add(new StringPair(value, ""));
|
||||
for (StringPair envDefine : environmentDefines) macros.add(envDefine);
|
||||
stringValues.forEach((name, value) -> macros.add(new StringPair(name, value)));
|
||||
final ShaderDiskCache.Key cacheKey = PreprocessedSourceCache.key("properties", source, macros);
|
||||
final String cached = PreprocessedSourceCache.get(cacheKey);
|
||||
if (cached != null) return cached;
|
||||
|
||||
try (Preprocessor pp = new Preprocessor()) {
|
||||
for (String value : booleanValues) {
|
||||
pp.addMacro(value);
|
||||
@@ -46,7 +54,9 @@ public class PropertiesPreprocessor {
|
||||
}
|
||||
});
|
||||
|
||||
return process(pp, source);
|
||||
final String preprocessed = process(pp, source);
|
||||
PreprocessedSourceCache.put(cacheKey, preprocessed);
|
||||
return preprocessed;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Unexpected IOException while processing macros", e);
|
||||
} catch (LexerException e) {
|
||||
@@ -77,24 +87,10 @@ public class PropertiesPreprocessor {
|
||||
PropertyCollectingListener listener = new PropertyCollectingListener();
|
||||
preprocessor.setListener(listener);
|
||||
|
||||
// Not super efficient, but this removes trailing whitespace on lines, fixing an issue with whitespace after
|
||||
// This removes trailing whitespace on lines, fixing an issue with whitespace after
|
||||
// line continuations (see PreprocessorTest#testWeirdPropertiesLineContinuation)
|
||||
// Required for Voyager Shader
|
||||
source = Arrays.stream(source.split("\\r\\n|\\n|\\r")).map(String::trim).filter(s -> !s.isEmpty())
|
||||
.map(line -> {
|
||||
if (line.startsWith("#")) {
|
||||
for (PreprocessorCommand command : PreprocessorCommand.values()) {
|
||||
if (line.startsWith("#" + (command.name().replace("PP_", "").toLowerCase(Locale.ROOT)))) {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
// In PropertyCollectingListener we suppress "unknown preprocessor directive errors" and
|
||||
// assume the line to be a comment, since in .properties files `#` also functions as a comment
|
||||
// marker.
|
||||
return line.replace("#", "");
|
||||
}).collect(Collectors.joining("\n")) + "\n";
|
||||
source = cleanLines(source);
|
||||
// TODO: This is a horrible fix to trick the preprocessor into not seeing the backslashes during processing. We need a better way to do this.
|
||||
source = source.replace("\\", "IRIS_PASSTHROUGHBACKSLASH");
|
||||
|
||||
@@ -119,6 +115,46 @@ public class PropertiesPreprocessor {
|
||||
return (listener.collectLines() + source).replace("IRIS_PASSTHROUGHBACKSLASH", "\\");
|
||||
}
|
||||
|
||||
private static final String[] DIRECTIVE_PREFIXES = Arrays.stream(PreprocessorCommand.values())
|
||||
.map(command -> "#" + command.name().replace("PP_", "").toLowerCase(Locale.ROOT))
|
||||
.toArray(String[]::new);
|
||||
|
||||
// Same result as splitting on \r\n|\n|\r, trimming, dropping empty lines and joining with \n
|
||||
static String cleanLines(String source) {
|
||||
final StringBuilder out = new StringBuilder(source.length());
|
||||
boolean first = true;
|
||||
int start = 0;
|
||||
final int length = source.length();
|
||||
while (start <= length) {
|
||||
int end = start;
|
||||
while (end < length && source.charAt(end) != '\n' && source.charAt(end) != '\r') end++;
|
||||
final String line = source.substring(start, end).trim();
|
||||
if (!line.isEmpty()) {
|
||||
if (!first) out.append('\n');
|
||||
first = false;
|
||||
out.append(cleanLine(line));
|
||||
}
|
||||
if (end < length && source.charAt(end) == '\r' && end + 1 < length && source.charAt(end + 1) == '\n') end++;
|
||||
start = end + 1;
|
||||
}
|
||||
return out.append('\n').toString();
|
||||
}
|
||||
|
||||
private static String cleanLine(String line) {
|
||||
if (line.startsWith("#")) {
|
||||
for (String prefix : DIRECTIVE_PREFIXES) {
|
||||
if (line.startsWith(prefix)) {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
// In PropertyCollectingListener we suppress "unknown preprocessor directive errors" and
|
||||
// assume the line to be a comment, since in .properties files `#` also functions as a comment
|
||||
// marker.
|
||||
return line.indexOf('#') < 0 ? line : line.replace("#", "");
|
||||
}
|
||||
|
||||
private static List<String> getBooleanValues(ShaderPackOptions shaderPackOptions) {
|
||||
List<String> booleanValues = new ArrayList<>();
|
||||
|
||||
|
||||
Loaded 100 of 120 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user