Compare commits

...
61 changed files with 2292 additions and 439 deletions

No files matched your search

+2
View File
@@ -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
@@ -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) {
@@ -72,6 +72,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 +87,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");
@@ -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; }
@@ -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) {
@@ -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;
+3
View File
@@ -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
+37
View File
@@ -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()
@@ -41,6 +41,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 +65,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;
@@ -168,6 +170,7 @@ public class SDLGPURenderBackend extends RenderBackend {
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 +251,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) {
@@ -515,14 +520,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();
@@ -1333,6 +1337,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 +1357,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;
@@ -2761,8 +2771,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 +2821,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 +2920,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 +2944,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 +3072,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 +3149,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 +3362,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 +3402,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 +3444,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 +3459,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 +3899,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 +4159,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 +4230,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) {
@@ -4469,8 +4539,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);
}
}
}
@@ -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);
@@ -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=");
@@ -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) {
@@ -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;
}
@@ -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) {
@@ -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); }
@@ -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();
@@ -752,6 +756,7 @@ public final class ResourceManager {
public void deleteBuffer(int glId) {
final ByteBuffer droppedPboStaging;
final ByteBuffer droppedPlainStaging;
final PersistentMapping droppedPm;
wLock.lock();
try {
@@ -763,6 +768,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 +783,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 +962,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 +1045,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 +1065,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 +1078,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 +1265,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 +1279,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 +1299,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 +1563,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 +1661,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 +1676,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 +2647,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) {}
@@ -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);
@@ -53,6 +53,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 +65,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 +236,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) {
@@ -377,6 +407,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 +465,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 +604,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 +648,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 +857,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 +920,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());
}
@@ -1476,6 +1536,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 +1581,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 +1597,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 +1801,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 +1885,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();
@@ -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);
}
@@ -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();
@@ -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());
@@ -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;
@@ -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);
}
@@ -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);
@@ -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);
}
}
}
@@ -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());
@@ -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());
}
@@ -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");
}
@@ -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);
}
}
}
@@ -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;
}
}
}
}
@@ -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;
@@ -41,7 +40,6 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vao.VAOManager;
import com.gtnewhorizons.angelica.client.font.DarkModeUtils;
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 +63,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 +82,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 +148,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();
@@ -391,6 +386,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
@@ -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());
}
}
+178 -16
View File
@@ -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);
}
@@ -543,26 +543,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 +569,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 +1117,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 +1148,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,9 +1179,15 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
}
ProgramBuilder builder = beginBuilder(source.getName(), transformed);
wireGbufferProgram(builder, availability, shadow);
final Program program = builder.build();
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;
}
@@ -1212,10 +1234,7 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R
}
}
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 +1259,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 +1483,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 +1502,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);
@@ -2440,6 +2469,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) {
@@ -227,6 +227,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)) {
@@ -378,6 +383,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";
@@ -576,16 +576,16 @@ public class ShaderTransformer {
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) {
result.put(PatchShaderType.COMPUTE, extracted.computeSource());
Iris.logger.info("[RwImageStoreExtractor] Extracted compute pre-pass for {} (mode={}, written={})", patchType.name(), extracted.mode(), extracted.writtenImages());
} else {
Iris.logger.info("[RwImageStoreExtractor] Dropped image writes from {} (written={})", patchType.name(), extracted.writtenImages());
}
}
@@ -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);
}
}
@@ -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<>();
@@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
@@ -46,6 +47,7 @@ public final class PBRResourceIndex {
private static IResourceManager owner;
private static CompletableFuture<Map<IResourcePack, PackIndex>> pending;
private static Map<IResourcePack, PackIndex> built;
private static final Map<ResourceLocation, Boolean> answers = new HashMap<>();
private static boolean disabled;
private static boolean warned;
@@ -75,6 +77,7 @@ public final class PBRResourceIndex {
owner = null;
pending = null;
built = null;
answers.clear();
}
public static synchronized boolean mayExist(IResourceManager resourceManager, ResourceLocation location) {
@@ -99,6 +102,16 @@ public final class PBRResourceIndex {
return true;
}
}
final Boolean known = answers.get(location);
if (known != null) {
return known;
}
final boolean present = lookUp(manager, location, path);
answers.put(location, present);
return present;
}
private static boolean lookUp(SimpleReloadableResourceManager manager, ResourceLocation location, String path) {
final Object domain = manager.domainResourceManagers.get(location.getResourceDomain());
if (domain == null) {
return false;
@@ -234,6 +234,11 @@ public class CustomUniforms implements FunctionContext {
locationMap.put(pass, locationMap.remove(holder));
}
public void mapPassLike(Object existingPass, Object pass) {
final PassUniforms uniforms = locationMap.get(existingPass);
if (uniforms != null) locationMap.put(pass, uniforms);
}
public void update() {
if (Tracy.ENABLED) {
@@ -0,0 +1,39 @@
package net.coderbot.iris;
import com.gtnewhorizons.angelica.config.AngelicaConfig;
import net.coderbot.iris.shaderpack.StringPair;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.*;
class PackSettingsFingerprintTest {
@TempDir Path dir;
@Test
void effectiveFallbackVersionInvalidatesPreparedAndCompiledPackSettings() throws Exception {
final Path pack = Files.write(dir.resolve("pack.zip"), new byte[] {1});
final Method describe = Iris.class.getDeclaredMethod("describeSettings", String.class, Path.class,
Properties.class, Iterable.class, boolean.class);
describe.setAccessible(true);
final int previous = AngelicaConfig.modernFallbackMcVersion;
try {
for (boolean byContent : new boolean[] {false, true}) {
AngelicaConfig.modernFallbackMcVersion = 0;
final Object defaults = describe.invoke(null, "pack", pack, new Properties(), List.of(new StringPair("MC_VERSION", "10710")), byContent);
AngelicaConfig.modernFallbackMcVersion = 260101;
assertEquals(defaults, describe.invoke(null, "pack", pack, new Properties(), List.of(new StringPair("MC_VERSION", "10710")), byContent));
AngelicaConfig.modernFallbackMcVersion = 12001;
assertNotEquals(defaults, describe.invoke(null, "pack", pack, new Properties(), List.of(new StringPair("MC_VERSION", "10710")), byContent));
}
} finally {
AngelicaConfig.modernFallbackMcVersion = previous;
}
}
}
@@ -109,7 +109,7 @@ class ProgramBindingRegressionTest {
int id = linkedProgram();
int other = linkedProgram();
ProgramUniforms uniforms = mock(ProgramUniforms.class);
Program program = new Program(id, uniforms, mock(ProgramSamplers.class), mock(ProgramImages.class));
Program program = new Program(id, null, uniforms, mock(ProgramSamplers.class), mock(ProgramImages.class));
DeferredWorldRenderingPipeline pipeline = mock(DeferredWorldRenderingPipeline.class);
DeferredWorldRenderingPipeline.Pass pass = Reflect.allocate(DeferredWorldRenderingPipeline.Pass.class);
Reflect.set(pass, "program", program);
@@ -0,0 +1,28 @@
package net.coderbot.iris.gl.shader;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class StandardMacrosVersionTest {
@Test
void releaseAndPrereleaseTags() {
assertEquals("20228000", StandardMacros.formatAngelicaVersion("2.2.28"));
assertEquals("20000008", StandardMacros.formatAngelicaVersion("2.0.0-alpha8"));
assertEquals("10000078", StandardMacros.formatAngelicaVersion("1.0.0-beta78-pre"));
assertEquals("20228000", StandardMacros.formatAngelicaVersion("2.2.28-pre"));
}
@Test
void devBuildsIgnoreBranchAndHashDigits() {
assertEquals("20228000", StandardMacros.formatAngelicaVersion("2.2.28-master.7+12857006ab-dirty"));
assertEquals("20228000", StandardMacros.formatAngelicaVersion("2.2.28-fix2117.3+12857006ab"));
assertEquals("20000012", StandardMacros.formatAngelicaVersion("2.0.0-alpha12-master.3+12857006ab"));
}
@Test
void unparseableVersionsReportNewest() {
assertEquals("99999999", StandardMacros.formatAngelicaVersion("moltenvk-v3.4.2-master.1+38af64f10c-dirty"));
assertEquals("99999999", StandardMacros.formatAngelicaVersion("NO-GIT-TAG-SET"));
}
}
@@ -50,6 +50,27 @@ class RwImageStoreExtractorTest {
"void main() { gl_Position = iris_Vertex; }",
"");
private static final String ATOMIC_RESULT_VSH = String.join("\n",
"#version 460 core",
"layout(r32i) uniform iimage2D counter_img;",
"void main() {",
" int previous = imageAtomicCompSwap(counter_img, ivec2(0), 0, -1);",
" if (previous == 0) {",
" imageAtomicAdd(counter_img, ivec2(1), 1);",
" }",
" gl_Position = vec4(0.0);",
"}",
"");
@Test void raster_keepsVariablesInitializedFromAtomics() {
final var result = RwImageStoreExtractor.tryExtract(ATOMIC_RESULT_VSH, PatchShaderType.VERTEX, "prepare");
assertNotNull(result);
final String stripped = norm(result.strippedSource());
assertTrue(stripped.contains("int previous = 0;"), stripped);
assertTrue(stripped.contains("if (previous == 0)"), stripped);
assertFalse(stripped.contains("imageAtomic"), stripped);
}
@Test void detection_returnsNullWhenNoImageStore() {
assertNull(RwImageStoreExtractor.tryExtract(NO_IMAGESTORE_VSH, PatchShaderType.VERTEX, "shadow"));
}
+2
View File
@@ -2,6 +2,8 @@ plugins {
id("com.gtnewhorizons.gtnhconvention")
}
apply(from = "../gradle/mod-version.gradle.kts")
minecraft {
extraRunJvmArguments.add("-Dumbra.dumpClass=true")
extraRunJvmArguments.add("-Dangelica.sdlgpu.enable=true")
+1
View File
@@ -1,4 +1,5 @@
gtnh.settings.blowdryerTag = 0.2.2
gtnh.modules.gitVersion = false
modName = Umbra
modId = umbra