Skip to content

Commit

Permalink
Improve SSBO handling
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Feb 6, 2024
1 parent 01d8803 commit 4de5246
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/coderbot/iris/Iris.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.coderbot.iris.compat.dh.DHCompat;
import net.coderbot.iris.config.IrisConfig;
import net.coderbot.iris.gl.GLDebug;
import net.coderbot.iris.gl.buffer.ShaderStorageBufferHolder;
import net.coderbot.iris.gl.shader.ShaderCompileException;
import net.coderbot.iris.gl.shader.StandardMacros;
import net.coderbot.iris.gui.debug.DebugLoadFailedGridScreen;
Expand Down Expand Up @@ -635,6 +636,8 @@ private static void destroyEverything() {

getPipelineManager().destroyPipeline();

ShaderStorageBufferHolder.deleteAllBuffers();

// Close the zip filesystem that the shaderpack was loaded from
//
// This prevents a FileSystemAlreadyExistsException when reloading shaderpacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import net.coderbot.iris.gl.sampler.SamplerLimits;
import org.lwjgl.opengl.GL43C;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ShaderStorageBufferHolder {
private int cachedWidth;
private int cachedHeight;
private ShaderStorageBuffer[] buffers;
private boolean destroyed;

private static List<ShaderStorageBuffer> activeBuffers = new ArrayList<>();

public ShaderStorageBufferHolder(Int2ObjectArrayMap<ShaderStorageInfo> overrides, int width, int height) {
destroyed = false;
cachedWidth = width;
Expand All @@ -29,6 +33,7 @@ public ShaderStorageBufferHolder(Int2ObjectArrayMap<ShaderStorageInfo> overrides
}

buffers[index] = new ShaderStorageBuffer(index, bufferInfo);
activeBuffers.add(buffers[index]);
int buffer = buffers[index].getId();

if (bufferInfo.relative()) {
Expand All @@ -48,7 +53,9 @@ public void hasResizedScreen(int width, int height) {
cachedWidth = width;
cachedHeight = height;
for (ShaderStorageBuffer buffer : buffers) {
buffer.resizeIfRelative(width, height);
if (buffer != null) {
buffer.resizeIfRelative(width, height);
}
}
}
}
Expand All @@ -63,18 +70,28 @@ public void setupBuffers() {
}

for (ShaderStorageBuffer buffer : buffers) {
buffer.bind();
if (buffer != null) {
buffer.bind();
}
}
}

public void destroyBuffers() {
for (ShaderStorageBuffer buffer : buffers) {
buffer.destroy();
if (buffer != null) {
buffer.destroy();
activeBuffers.remove(buffer);
}
}
buffers = null;
destroyed = true;
}

public static void deleteAllBuffers() {
activeBuffers.forEach(ShaderStorageBuffer::destroy);
activeBuffers.clear();
}

private static class OutOfVideoMemoryError extends RuntimeException {
public OutOfVideoMemoryError(String s) {
super(s);
Expand Down

0 comments on commit 4de5246

Please sign in to comment.