Skip to content

Commit

Permalink
Add the ability to load file into SSBO
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 15, 2024
1 parent c5ec134 commit 422fad9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@
import net.irisshaders.iris.gl.GLDebug;
import net.irisshaders.iris.gl.IrisRenderSystem;
import org.lwjgl.opengl.GL43C;
import org.lwjgl.opengl.GL46C;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;

// Do not extend GlResource, this is immutable.
public class ShaderStorageBuffer {
protected final int index;
protected final ShaderStorageInfo info;
protected final BuiltShaderStorageInfo info;
protected int id;
protected final ByteBuffer content;

public ShaderStorageBuffer(int index, ShaderStorageInfo info) {
public ShaderStorageBuffer(int index, BuiltShaderStorageInfo info) {
this.id = IrisRenderSystem.createBuffers();
if (info.content() != null) {
content = MemoryUtil.memAlloc(info.content().length);
content.put(info.content());
content.flip();
} else {
content = null;
}
GLDebug.nameObject(GL43C.GL_BUFFER, id, "SSBO " + index);
this.index = index;
this.info = info;
Expand All @@ -30,6 +42,7 @@ protected void destroy() {
IrisRenderSystem.bindBufferBase(GL43C.GL_SHADER_STORAGE_BUFFER, index, 0);
// DO NOT use the GlStateManager version here! On Linux, it will attempt to clear the data using BufferData and cause GL errors.
IrisRenderSystem.deleteBuffers(id);
MemoryUtil.memFree(content);
}

public void bind() {
Expand All @@ -56,4 +69,15 @@ public void resizeIfRelative(int width, int height) {
public int getId() {
return id;
}

public void createStatic() {
GlStateManager._glBindBuffer(GL43C.GL_SHADER_STORAGE_BUFFER, getId());
IrisRenderSystem.bufferStorage(GL43C.GL_SHADER_STORAGE_BUFFER, info.size(), content == null ? 0 : GL46C.GL_DYNAMIC_STORAGE_BIT);
if (content != null) {
GL46C.glBufferSubData(GL43C.GL_SHADER_STORAGE_BUFFER, 0, content);
} else {
IrisRenderSystem.clearBufferSubData(GL43C.GL_SHADER_STORAGE_BUFFER, GL43C.GL_R8, 0, info.size(), GL43C.GL_RED, GL43C.GL_BYTE, new int[]{0});
}
bind();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ShaderStorageBufferHolder {
private boolean destroyed;


public ShaderStorageBufferHolder(Int2ObjectArrayMap<ShaderStorageInfo> overrides, int width, int height) {
public ShaderStorageBufferHolder(Int2ObjectArrayMap<BuiltShaderStorageInfo> overrides, int width, int height) {
destroyed = false;
cachedWidth = width;
cachedHeight = height;
Expand All @@ -40,10 +40,7 @@ public ShaderStorageBufferHolder(Int2ObjectArrayMap<ShaderStorageInfo> overrides
if (bufferInfo.relative()) {
buffers[index].resizeIfRelative(width, height);
} else {
GlStateManager._glBindBuffer(GL43C.GL_SHADER_STORAGE_BUFFER, buffer);
IrisRenderSystem.bufferStorage(GL43C.GL_SHADER_STORAGE_BUFFER, bufferInfo.size(), 0);
IrisRenderSystem.clearBufferSubData(GL43C.GL_SHADER_STORAGE_BUFFER, GL43C.GL_R8, 0, bufferInfo.size(), GL43C.GL_RED, GL43C.GL_BYTE, new int[]{0});
IrisRenderSystem.bindBufferBase(GL43C.GL_SHADER_STORAGE_BUFFER, index, buffer);
buffers[index].createStatic();
}
});
GlStateManager._glBindBuffer(GL43C.GL_SHADER_STORAGE_BUFFER, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.irisshaders.iris.gl.buffer;

public record ShaderStorageInfo(long size, boolean relative, float scaleX, float scaleY) {
public record ShaderStorageInfo(long size, boolean relative, float scaleX, float scaleY, String name) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ public IrisRenderingPipeline(ProgramSet programSet) {
int internalFormat = TextureInfoCache.INSTANCE.getInfo(depthTextureId).getInternalFormat();
DepthBufferFormat depthBufferFormat = DepthBufferFormat.fromGlEnumOrDefault(internalFormat);

if (!programSet.getPackDirectives().getBufferObjects().isEmpty()) {
if (!pack.getBufferObjects().isEmpty()) {
if (IrisRenderSystem.supportsSSBO()) {
this.shaderStorageBufferHolder = new ShaderStorageBufferHolder(programSet.getPackDirectives().getBufferObjects(), main.width, main.height);
this.shaderStorageBufferHolder = new ShaderStorageBufferHolder(pack.getBufferObjects(), main.width, main.height);

this.shaderStorageBufferHolder.setupBuffers();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.api.v0.IrisApi;
import net.irisshaders.iris.features.FeatureFlags;
import net.irisshaders.iris.gl.buffer.BuiltShaderStorageInfo;
import net.irisshaders.iris.gl.buffer.ShaderStorageInfo;
import net.irisshaders.iris.gl.texture.TextureDefinition;
import net.irisshaders.iris.gui.FeatureMissingErrorScreen;
import net.irisshaders.iris.gui.screen.ShaderPackScreen;
Expand Down Expand Up @@ -86,6 +90,7 @@ public class ShaderPack {
private final ShaderProperties shaderProperties;
private final List<String> dimensionIds;
private Map<NamespacedId, String> dimensionMap;
private Int2ObjectArrayMap<BuiltShaderStorageInfo> bufferObjects;

public ShaderPack(Path root, ImmutableList<StringPair> environmentDefines, boolean isZip) throws IOException, IllegalStateException {
this(root, Collections.emptyMap(), environmentDefines, isZip);
Expand Down Expand Up @@ -113,6 +118,7 @@ public ShaderPack(Path root, Map<String, String> changedConfigs, ImmutableList<S
potentialFileNames);

dimensionIds = new ArrayList<>();
bufferObjects = new Int2ObjectArrayMap<>();

final boolean[] hasDimensionIds = {false}; // Thanks Java

Expand Down Expand Up @@ -171,6 +177,35 @@ public ShaderPack(Path root, Map<String, String> changedConfigs, ImmutableList<S
.map(source -> new ShaderProperties(source, shaderPackOptions, finalEnvironmentDefines))
.orElseGet(ShaderProperties::empty);

for (Int2ObjectMap.Entry<ShaderStorageInfo> shaderStorageInfoEntry : shaderProperties.getBufferObjects().int2ObjectEntrySet()) {
ShaderStorageInfo info = shaderStorageInfoEntry.getValue();

if (info.name() == null) {
bufferObjects.put(shaderStorageInfoEntry.getIntKey(), new BuiltShaderStorageInfo(info.size(), info.relative(), info.scaleX(), info.scaleY(), null));
continue;
} else {
String path = info.name();

try {
if (path.startsWith("/")) {
// NB: This does not guarantee the resulting path is in the shaderpack as a double slash could be used,
// this just fixes shaderpacks like Continuum 2.0.4 that use a leading slash in texture paths
path = path.substring(1);
}

byte[] data = Files.readAllBytes(root.resolve(path));

if (data.length > info.size()) {
throw new IllegalStateException("Tried to load a shader storage file with no space in the buffer! Increase the buffer size.");
}

bufferObjects.put(shaderStorageInfoEntry.getIntKey(), new BuiltShaderStorageInfo(info.size(), info.relative(), info.scaleX(), info.scaleY(), data));
} catch (IOException e) {
Iris.logger.error("Shader storage buffer with index " + shaderStorageInfoEntry.getIntKey() + " and path " + path + " could not be read.", e);
}
}
}

activeFeatures = new HashSet<>();
for (int i = 0; i < shaderProperties.getRequiredFeatureFlags().size(); i++) {
activeFeatures.add(FeatureFlags.getValue(shaderProperties.getRequiredFeatureFlags().get(i)));
Expand Down Expand Up @@ -587,4 +622,8 @@ public OptionMenuContainer getMenuContainer() {
public boolean hasFeature(FeatureFlags feature) {
return activeFeatures.contains(feature);
}

public Int2ObjectArrayMap<BuiltShaderStorageInfo> getBufferObjects() {
return bufferObjects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class PackDirectives {
private Object2ObjectMap<String, Object2BooleanMap<String>> explicitFlips = new Object2ObjectOpenHashMap<>();
private Object2ObjectMap<String, TextureScaleOverride> scaleOverrides = new Object2ObjectOpenHashMap<>();
private Object2ObjectMap<Tri<String, TextureType, TextureStage>, String> textureMap;
private Int2ObjectArrayMap<ShaderStorageInfo> bufferObjects;
private Optional<ParticleRenderingSettings> particleRenderingSettings;

private PackDirectives(Set<Integer> supportedRenderTargets, PackShadowDirectives packShadowDirectives) {
Expand All @@ -63,7 +62,6 @@ private PackDirectives(Set<Integer> supportedRenderTargets, PackShadowDirectives
drynessHalfLife = 200.0f;
eyeBrightnessHalfLife = 10.0f;
centerDepthHalfLife = 1.0F;
bufferObjects = new Int2ObjectArrayMap<>();
renderTargetDirectives = new PackRenderTargetDirectives(supportedRenderTargets);
shadowDirectives = packShadowDirectives;
}
Expand Down Expand Up @@ -94,7 +92,6 @@ public PackDirectives(Set<Integer> supportedRenderTargets, ShaderProperties prop
prepareBeforeShadow = properties.getPrepareBeforeShadow().orElse(false);
particleRenderingSettings = properties.getParticleRenderingSettings();
textureMap = properties.getCustomTexturePatching();
bufferObjects = properties.getBufferObjects();
}

PackDirectives(Set<Integer> supportedRenderTargets, PackDirectives directives) {
Expand All @@ -112,7 +109,6 @@ public PackDirectives(Set<Integer> supportedRenderTargets, ShaderProperties prop
prepareBeforeShadow = directives.prepareBeforeShadow;
particleRenderingSettings = directives.particleRenderingSettings;
textureMap = directives.textureMap;
bufferObjects = directives.bufferObjects;
}

private static float clamp(float val, float lo, float hi) {
Expand Down Expand Up @@ -239,10 +235,6 @@ public PackShadowDirectives getShadowDirectives() {
return shadowDirectives;
}

public Int2ObjectArrayMap<ShaderStorageInfo> getBufferObjects() {
return bufferObjects;
}

public boolean supportsColorCorrection() {
return supportsColorCorrection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,21 @@ public ShaderProperties(String contents, ShaderPackOptions shaderPackOptions, It
boolean isRelative;
float scaleX, scaleY;
String[] parts = value.split(" ");
if (parts.length == 1) {
if (parts.length <= 2) {
try {
trueIndex = Integer.parseInt(index);
trueSize = Long.parseLong(value);
trueSize = Long.parseLong(parts[0]);
} catch (NumberFormatException e) {
Iris.logger.error("Number format exception parsing SSBO index/size!", e);
return;
}

String name = null;

if (parts.length > 1) {
name = parts[1];
}

if (trueIndex > 8) {
Iris.logger.fatal("SSBO's cannot use buffer numbers higher than 8, they're reserved!");
return;
Expand All @@ -392,7 +398,7 @@ public ShaderProperties(String contents, ShaderPackOptions shaderPackOptions, It
return;
}

bufferObjects.put(trueIndex, new ShaderStorageInfo(trueSize, false, 0, 0));
bufferObjects.put(trueIndex, new ShaderStorageInfo(trueSize, false, 0, 0, name));
} else {
// Assume it's a long one
try {
Expand All @@ -416,7 +422,7 @@ public ShaderProperties(String contents, ShaderPackOptions shaderPackOptions, It
return;
}

bufferObjects.put(trueIndex, new ShaderStorageInfo(trueSize, isRelative, scaleX, scaleY));
bufferObjects.put(trueIndex, new ShaderStorageInfo(trueSize, isRelative, scaleX, scaleY, null));
}
});

Expand Down

0 comments on commit 422fad9

Please sign in to comment.