Skip to content

Commit

Permalink
Updated to iris 1.6.9 and added basic ssbo support
Browse files Browse the repository at this point in the history
  • Loading branch information
MCRcortex committed Sep 19, 2023
1 parent 65effe4 commit 08737e2
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 27 deletions.
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation(fabricApi.module("fabric-rendering-fluids-v1", project.fabric_version))
modImplementation(fabricApi.module("fabric-rendering-data-attachment-v1", project.fabric_version))
modImplementation(fabricApi.module("fabric-key-binding-api-v1", project.fabric_version))
modImplementation(fabricApi.module("fabric-key-binding-api-v1", project.fabric_version))
modImplementation(fabricApi.module("fabric-resource-loader-v0", project.fabric_version))
modImplementation(fabricApi.module("fabric-api-base", project.fabric_version))
modImplementation(fabricApi.module("fabric-block-view-api-v2", project.fabric_version))
modImplementation(fabricApi.module("fabric-rendering-fluids-v1", project.fabric_version))
modImplementation("net.fabricmc.fabric-api:fabric-rendering-data-attachment-v1:0.3.36+92a0d36777")
modImplementation(fabricApi.module("fabric-resource-loader-v0", project.fabric_version))

modImplementation "maven.modrinth:sodium:mc1.20.1-0.5.2"
modImplementation "maven.modrinth:iris:1.6.8+1.20.1"
modImplementation "maven.modrinth:sodium:mc1.20.1-0.5.3"
modImplementation "maven.modrinth:iris:1.6.9+1.20.1"

modRuntimeOnly 'org.anarres:jcpp:1.4.14'
modRuntimeOnly 'io.github.douira:glsl-transformer:2.0.0-pre13'
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ yarn_mappings=1.20.1+build.5
loader_version=0.14.21

#Fabric api
fabric_version=0.84.0+1.20.1
fabric_version=0.88.1+1.20.1
# Mod Properties
mod_version=0.0.3-pre-alpha
mod_version=0.0.4-pre-alpha
maven_group=me.cortex
archives_base_name=vulkanite

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.cortex.vulkanite.acceleration.AccelerationManager;
import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.compat.IVGBuffer;
import me.cortex.vulkanite.compat.IVGImage;
import me.cortex.vulkanite.compat.RaytracingShaderSet;
import me.cortex.vulkanite.lib.base.VContext;
Expand All @@ -19,6 +20,9 @@
import me.cortex.vulkanite.lib.other.sync.VSemaphore;
import me.cortex.vulkanite.lib.pipeline.RaytracePipelineBuilder;
import me.cortex.vulkanite.lib.pipeline.VRaytracePipeline;
import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
import net.coderbot.iris.gl.buffer.ShaderStorageBufferHolder;
import net.coderbot.iris.gl.buffer.ShaderStorageInfo;
import net.coderbot.iris.texture.pbr.PBRTextureHolder;
import net.coderbot.iris.texture.pbr.PBRTextureManager;
import net.coderbot.iris.uniforms.CapturedRenderingState;
Expand Down Expand Up @@ -69,7 +73,7 @@ public class VulkanPipeline {

private int fidx;

public VulkanPipeline(VContext ctx, AccelerationManager accelerationManager, RaytracingShaderSet[] passes) {
public VulkanPipeline(VContext ctx, AccelerationManager accelerationManager, RaytracingShaderSet[] passes, int[] ssboIds) {
this.ctx = ctx;
this.accelerationManager = accelerationManager;
this.singleUsePool = ctx.cmd.createSingleUsePool();
Expand Down Expand Up @@ -120,15 +124,22 @@ public VulkanPipeline(VContext ctx, AccelerationManager accelerationManager, Ray
.maxAnisotropy(1.0f));

try {
layout = new DescriptorSetLayoutBuilder()
var layoutBuilder = new DescriptorSetLayoutBuilder()
.binding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_ALL)//camera data
.binding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, VK_SHADER_STAGE_ALL)//funni acceleration buffer
.binding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_ALL)//funni buffer buffer
.binding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_ALL)//output texture
.binding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_ALL)//block texture
.binding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_ALL)//block texture normal
.binding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_ALL)//block texture specular
.build(ctx);
.binding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_ALL);//block texture specular

for (int id : ssboIds) {
//NOTE:FIXME: the + 7 is cause of all the other bindings
layoutBuilder.binding(id + 7, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_ALL);
}


layout = layoutBuilder.build(ctx);

//TODO: use frameahead count instead of just... 10
descriptors = new VDescriptorPool(ctx, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 10, layout.types);
Expand Down Expand Up @@ -162,7 +173,7 @@ private static void applyImageBarrier(VkImageMemoryBarrier barrier, VImage image
private VSemaphore previousSemaphore;

private int frameId;
public void renderPostShadows(VGImage outImg, Camera camera) {
public void renderPostShadows(VGImage outImg, Camera camera, ShaderStorageBuffer[] ssbos) {
this.singleUsePool.doReleases();
PBRTextureManager.notifyPBRTexturesChanged();

Expand Down Expand Up @@ -218,16 +229,19 @@ public void renderPostShadows(VGImage outImg, Camera camera) {

long desc = descriptors.get(fidx);

new DescriptorUpdateBuilder(ctx, 7, placeholderImageView)
var updater = new DescriptorUpdateBuilder(ctx, 7 + ssbos.length, placeholderImageView)
.set(desc)
.uniform(0, uboBuffer)
.acceleration(1, tlas)
.buffer(2, accelerationManager.getReferenceBuffer())
.imageStore(3, composite0mainView.getView(()->outImg))
.imageSampler(4, blockAtlasView.getView(), sampler)
.imageSampler(5, blockAtlasNormalView.getView(), sampler)
.imageSampler(6, blockAtlasSpecularView.getView(), sampler)
.apply();
.imageSampler(6, blockAtlasSpecularView.getView(), sampler);
for (ShaderStorageBuffer ssbo : ssbos) {
updater.buffer(ssbo.getIndex() + 7, ((IVGBuffer) ssbo).getBuffer());
}
updater.apply();


//TODO: dont use a single use pool for commands like this...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import me.cortex.vulkanite.lib.memory.VGBuffer;

public interface IVkBuffer {
public interface IVGBuffer {
VGBuffer getBuffer();
void setBuffer(VGBuffer buffer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import me.cortex.vulkanite.compat.IRenderTargetVkGetter;
import me.cortex.vulkanite.compat.RaytracingShaderSet;
import me.cortex.vulkanite.lib.base.VContext;
import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
import net.coderbot.iris.gl.buffer.ShaderStorageBufferHolder;
import net.coderbot.iris.gl.buffer.ShaderStorageInfo;
import net.coderbot.iris.mixin.LevelRendererAccessor;
import net.coderbot.iris.pipeline.newshader.NewWorldRenderingPipeline;
import net.coderbot.iris.rendertarget.RenderTargets;
Expand All @@ -22,6 +25,7 @@
@Mixin(value = NewWorldRenderingPipeline.class, remap = false)
public class MixinNewWorldRenderingPipeline {
@Shadow @Final private RenderTargets renderTargets;
@Shadow private ShaderStorageBufferHolder shaderStorageBufferHolder;
@Unique private RaytracingShaderSet[] rtShaderPasses = null;
@Unique private VContext ctx;
@Unique private VulkanPipeline pipeline;
Expand All @@ -36,13 +40,14 @@ private void injectRTShader(ProgramSet set, CallbackInfo ci) {
for (int i = 0; i < passes.length; i++) {
rtShaderPasses[i] = new RaytracingShaderSet(ctx, passes[i]);
}
pipeline = new VulkanPipeline(ctx, Vulkanite.INSTANCE.getAccelerationManager(), rtShaderPasses);

pipeline = new VulkanPipeline(ctx, Vulkanite.INSTANCE.getAccelerationManager(), rtShaderPasses, set.getPackDirectives().getBufferObjects().keySet().toArray(new int[0]));
}
}

@Inject(method = "renderShadows", at = @At("TAIL"))
private void renderShadows(LevelRendererAccessor par1, Camera par2, CallbackInfo ci) {
pipeline.renderPostShadows(((IRenderTargetVkGetter)renderTargets.get(0)).getMain(), par2);
pipeline.renderPostShadows(((IRenderTargetVkGetter)renderTargets.getOrCreate(0)).getMain(), par2, ((ShaderStorageBufferHolderAccessor)shaderStorageBufferHolder).getBuffers());
}

@Inject(method = "destroyShaders", at = @At("TAIL"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.cortex.vulkanite.mixin.iris;

import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.compat.IVGBuffer;
import me.cortex.vulkanite.lib.memory.VGBuffer;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;

import static org.lwjgl.opengl.GL15C.glDeleteBuffers;

@Mixin(value = ShaderStorageBuffer.class, remap = false)
public class MixinShaderStorageBuffer implements IVGBuffer {
@Shadow protected int id;
@Unique
private VGBuffer vkBuffer;

public VGBuffer getBuffer() {
return vkBuffer;
}

public void setBuffer(VGBuffer buffer) {
if (vkBuffer != null && buffer != null) {
throw new IllegalStateException("Override buffer not null");
}
this.vkBuffer = buffer;
if (buffer != null) {
glDeleteBuffers(id);
id = vkBuffer.glId;
}
}

@Redirect(method = "destroy", at = @At(value = "INVOKE", target = "Lnet/coderbot/iris/gl/IrisRenderSystem;deleteBuffers(I)V"))
private void redirectDelete(int id) {
if (vkBuffer != null) {
Vulkanite.INSTANCE.addSyncedCallback(vkBuffer::free);
} else {
IrisRenderSystem.deleteBuffers(id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.cortex.vulkanite.mixin.iris;

import com.mojang.blaze3d.platform.GlStateManager;
import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.compat.IVGBuffer;
import me.cortex.vulkanite.lib.memory.VGBuffer;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
import net.coderbot.iris.gl.buffer.ShaderStorageBufferHolder;
import net.coderbot.iris.gl.buffer.ShaderStorageInfo;
import org.lwjgl.opengl.GL43C;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import static org.lwjgl.vulkan.VK10.VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;

@Mixin(value = ShaderStorageBufferHolder.class, remap = false)
public class MixinShaderStorageBufferHolder {
@Unique private ShaderStorageInfo storageInfo;

@Redirect(method = "lambda$new$0", at = @At(value = "INVOKE", target = "Lnet/coderbot/iris/gl/buffer/ShaderStorageInfo;relative()Z"))
private boolean alwaysReturnTrue(ShaderStorageInfo instance) {
this.storageInfo = instance;
return true;
}

private static VGBuffer alloc(int size) {
return Vulkanite.INSTANCE.getCtx().memory.createSharedBuffer(size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
}

@Redirect(method = "lambda$new$0", at = @At(value = "INVOKE", target = "Lnet/coderbot/iris/gl/buffer/ShaderStorageBuffer;resizeIfRelative(II)V"))
private void redirectSizeAllocation(ShaderStorageBuffer instance, int width, int height) {
if (storageInfo.relative()) {
instance.resizeIfRelative(width, height);
} else {
((IVGBuffer)instance).setBuffer(alloc(storageInfo.size()));
GlStateManager._glBindBuffer(GL43C.GL_SHADER_STORAGE_BUFFER, instance.getId());
IrisRenderSystem.clearBufferSubData(GL43C.GL_SHADER_STORAGE_BUFFER, GL43C.GL_R8, 0, storageInfo.size(), GL43C.GL_RED, GL43C.GL_BYTE, new int[] {0});
IrisRenderSystem.bindBufferBase(GL43C.GL_SHADER_STORAGE_BUFFER, instance.getIndex(), instance.getId());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.cortex.vulkanite.mixin.iris;

import net.coderbot.iris.gl.buffer.ShaderStorageBuffer;
import net.coderbot.iris.gl.buffer.ShaderStorageBufferHolder;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(value = ShaderStorageBufferHolder.class, remap = false)
public interface ShaderStorageBufferHolderAccessor {
@Accessor
ShaderStorageBuffer[] getBuffers();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.cortex.vulkanite.mixin.sodium.gl;

import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.compat.IVkBuffer;
import me.cortex.vulkanite.compat.IVGBuffer;
import me.jellysquid.mods.sodium.client.gl.buffer.GlBuffer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -12,7 +12,7 @@
public class MixinGLRenderDevice {
@Inject(method = "deleteBuffer", at = @At("HEAD"), cancellable = true)
private void redirectDelete(GlBuffer buffer, CallbackInfo ci) {
if (buffer instanceof IVkBuffer vkBuffer && vkBuffer.getBuffer() != null) {
if (buffer instanceof IVGBuffer vkBuffer && vkBuffer.getBuffer() != null) {
Vulkanite.INSTANCE.addSyncedCallback(vkBuffer.getBuffer()::free);
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.cortex.vulkanite.mixin.sodium.gl;

import me.cortex.vulkanite.compat.IVGBuffer;
import me.cortex.vulkanite.compat.IVulkanContextGetter;
import me.cortex.vulkanite.compat.IVkBuffer;
import me.cortex.vulkanite.lib.base.VContext;
import me.cortex.vulkanite.lib.memory.VGBuffer;
import me.jellysquid.mods.sodium.client.gl.arena.GlBufferArena;
Expand All @@ -26,14 +26,14 @@ private VGBuffer createBuffer(VContext ctx, long size) {

@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lme/jellysquid/mods/sodium/client/gl/device/CommandList;allocateStorage(Lme/jellysquid/mods/sodium/client/gl/buffer/GlMutableBuffer;JLme/jellysquid/mods/sodium/client/gl/buffer/GlBufferUsage;)V"))
private void redirectBufferInit(CommandList instance, GlMutableBuffer buffer, long size, GlBufferUsage usage) {
var vkglbuff = (IVkBuffer)buffer;
var vkglbuff = (IVGBuffer)buffer;
var ctx = ((IVulkanContextGetter)instance).getCtx();
vkglbuff.setBuffer(createBuffer(ctx, size));
}

@Redirect(method = "transferSegments", at = @At(value = "INVOKE", target = "Lme/jellysquid/mods/sodium/client/gl/device/CommandList;allocateStorage(Lme/jellysquid/mods/sodium/client/gl/buffer/GlMutableBuffer;JLme/jellysquid/mods/sodium/client/gl/buffer/GlBufferUsage;)V"))
private void redirectBufferTransfer(CommandList instance, GlMutableBuffer buffer, long size, GlBufferUsage usage) {
var vkglbuff = (IVkBuffer)buffer;
var vkglbuff = (IVGBuffer)buffer;
var ctx = ((IVulkanContextGetter)instance).getCtx();
vkglbuff.setBuffer(createBuffer(ctx, size));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.cortex.vulkanite.mixin.sodium.gl;

import me.cortex.vulkanite.compat.IVkBuffer;
import me.cortex.vulkanite.compat.IVGBuffer;
import me.cortex.vulkanite.lib.memory.VGBuffer;
import me.jellysquid.mods.sodium.client.gl.buffer.GlBuffer;
import me.jellysquid.mods.sodium.client.gl.buffer.GlMutableBuffer;
Expand All @@ -10,7 +10,7 @@
import static org.lwjgl.opengl.GL15C.glDeleteBuffers;

@Mixin(value = GlMutableBuffer.class, remap = false)
public class MixinMutableBuffer extends GlBuffer implements IVkBuffer {
public class MixinMutableBuffer extends GlBuffer implements IVGBuffer {
@Unique private VGBuffer vkBuffer;

public VGBuffer getBuffer() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"depends": {
"fabricloader": ">=0.14.21",
"iris": "=1.6.8",
"sodium": "=0.5.2"
"iris": "=1.6.9",
"sodium": "=0.5.3"
}
}
7 changes: 6 additions & 1 deletion src/main/resources/vulkanite.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
],
"injectors": {
"defaultRequire": 1
}
},
"mixins": [
"iris.MixinShaderStorageBuffer",
"iris.MixinShaderStorageBufferHolder",
"iris.ShaderStorageBufferHolderAccessor"
]
}

0 comments on commit 08737e2

Please sign in to comment.