Skip to content

Commit

Permalink
Almost works
Browse files Browse the repository at this point in the history
  • Loading branch information
MCRcortex committed Oct 15, 2023
1 parent 3c926bc commit 815c635
Show file tree
Hide file tree
Showing 42 changed files with 967 additions and 179 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ dependencies {

modRuntimeOnly 'net.java.dev.jna:jna:5.9.0'

implementation("com.github.FiguraMC.LuaJ:luaj-core:3.0.6-figura")
implementation("com.github.FiguraMC.LuaJ:luaj-jse:3.0.6-figura")
include(implementation("com.github.FiguraMC.LuaJ:luaj-core:3.0.6-figura"))
include(implementation("com.github.FiguraMC.LuaJ:luaj-jse:3.0.6-figura"))
}

processResources {
Expand Down Expand Up @@ -186,4 +186,7 @@ dependencies {
include(implementation("org.lwjgl:lwjgl-shaderc:$lwjglVersion"))
include(runtimeOnly("org.lwjgl:lwjgl-shaderc:$lwjglVersion:$winNatives"))
include(runtimeOnly("org.lwjgl:lwjgl-shaderc:$lwjglVersion:$linuxNatives"))

include(runtimeOnly("org.lwjgl:lwjgl-spvc:$lwjglVersion:$winNatives"))
include(runtimeOnly("org.lwjgl:lwjgl-spvc:$lwjglVersion:$linuxNatives"))
}
148 changes: 145 additions & 3 deletions src/main/java/me/cortex/vulkanite/client/rendering/VkPipeline2.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,167 @@
package me.cortex.vulkanite.client.rendering;

import me.cortex.vulkanite.acceleration.AccelerationManager;
import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.client.rendering.srp.graph.GraphExecutor;
import me.cortex.vulkanite.client.rendering.srp.lua.LuaContextHost;
import me.cortex.vulkanite.client.rendering.srp.lua.LuaExternalObjects;
import me.cortex.vulkanite.compat.RaytracingShaderSet;
import me.cortex.vulkanite.lib.base.VContext;
import me.cortex.vulkanite.lib.cmd.VCmdBuff;
import me.cortex.vulkanite.lib.cmd.VCommandPool;
import me.cortex.vulkanite.lib.memory.VBuffer;
import me.cortex.vulkanite.lib.memory.VGImage;
import me.cortex.vulkanite.lib.other.sync.VGSemaphore;
import me.cortex.vulkanite.lib.other.sync.VSemaphore;
import me.cortex.vulkanite.mixin.iris.MixinCelestialUniforms;
import me.cortex.vulkanite.mixin.iris.MixinCommonUniforms;
import net.coderbot.iris.texture.pbr.PBRTextureManager;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.CelestialUniforms;
import net.coderbot.iris.uniforms.SystemTimeUniforms;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;

import static org.lwjgl.opengl.EXTSemaphore.GL_LAYOUT_GENERAL_EXT;
import static org.lwjgl.opengl.GL11C.glFinish;
import static org.lwjgl.opengl.GL11C.glFlush;
import static org.lwjgl.util.vma.Vma.VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
import static org.lwjgl.vulkan.KHRRayTracingPipeline.VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR;
import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.VK10.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;

public class VkPipeline2 {
private final VContext ctx;
private final AccelerationManager accelerationManager;
private final VCommandPool singleUsePool;
private final LuaContextHost pipeline;
private final EntityCapture capture = new EntityCapture();


public VkPipeline2(VContext ctx, AccelerationManager accelerationManager, RaytracingShaderSet[] passes, int[] ssboIds, VGImage[] customTextures) {
public VkPipeline2(VContext ctx, LuaContextHost pipeline, AccelerationManager accelerationManager) {
this.ctx = ctx;
this.accelerationManager = accelerationManager;
this.singleUsePool = ctx.cmd.createSingleUsePool();

pipeline.loadScript("srp.lua");
pipeline.run();
this.pipeline = pipeline;
}

private void buildEntities() {
accelerationManager.setEntityData(capture.capture(CapturedRenderingState.INSTANCE.getTickDelta(), MinecraftClient.getInstance().world));
}

private Camera camera;
private MixinCelestialUniforms celestialUniforms;

public void setup(Camera camera, MixinCelestialUniforms celestialUniforms) {
this.camera = camera;
this.celestialUniforms = celestialUniforms;
}

private VBuffer createUBO() {
VBuffer uboBuffer = ctx.memory.createBuffer(1024,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
0, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
long ptr = uboBuffer.map();
MemoryUtil.memSet(ptr, 0, 1024);
{
ByteBuffer bb = MemoryUtil.memByteBuffer(ptr, 1024);

Vector3f tmpv3 = new Vector3f();
Matrix4f invProjMatrix = new Matrix4f();
Matrix4f invViewMatrix = new Matrix4f();

CapturedRenderingState.INSTANCE.getGbufferProjection().invert(invProjMatrix);
new Matrix4f(CapturedRenderingState.INSTANCE.getGbufferModelView()).translate(camera.getPos().toVector3f().negate()).invert(invViewMatrix);

invProjMatrix.transformProject(-1, -1, 0, 1, tmpv3).get(bb);
invProjMatrix.transformProject(+1, -1, 0, 1, tmpv3).get(4*Float.BYTES, bb);
invProjMatrix.transformProject(-1, +1, 0, 1, tmpv3).get(8*Float.BYTES, bb);
invProjMatrix.transformProject(+1, +1, 0, 1, tmpv3).get(12*Float.BYTES, bb);
invViewMatrix.get(Float.BYTES * 16, bb);

celestialUniforms.invokeGetSunPosition().get(Float.BYTES * 32, bb);
celestialUniforms.invokeGetMoonPosition().get(Float.BYTES * 36, bb);

bb.putInt(Float.BYTES * 40, SystemTimeUniforms.COUNTER.getAsInt());

int flags = MixinCommonUniforms.invokeIsEyeInWater() & 3;
bb.putInt(Float.BYTES * 41, flags);
bb.rewind();
}
uboBuffer.unmap();
uboBuffer.flush();
return uboBuffer;
}

private void buildGraph() {
public void execute() {
this.buildEntities();
this.singleUsePool.doReleases();
PBRTextureManager.notifyPBRTexturesChanged();

var outImgsGlIds = new int[0];
var outImgsGlLayouts = new int[0];

var in = this.ctx.sync.createSharedBinarySemaphore();
in.glSignal(new int[0], outImgsGlIds, outImgsGlLayouts);
glFlush();

var tlasLink = ctx.sync.createBinarySemaphore();
var tlas = accelerationManager.buildTLAS(in, tlasLink);

if (tlas == null) {
glFinish();
tlasLink.free();
in.free();
return;
}

if (LuaExternalObjects.TERRAIN_GEOMETRY_LAYOUT.getConcrete() != accelerationManager.getGeometrySet()) {
LuaExternalObjects.TERRAIN_GEOMETRY_LAYOUT.setConcrete(accelerationManager.getGeometrySet());
}

var ubo = this.createUBO();
LuaExternalObjects.COMMON_UNIFORM_BUFFER.setConcrete(ubo);
LuaExternalObjects.WORLD_ACCELERATION_STRUCTURE.setConcrete(tlas);

var cmd = this.singleUsePool.createCommandBuffer();
cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
var fence = this.ctx.sync.createFence();


GraphExecutor.execute(this.pipeline.getGraph(), cmd, fence);


cmd.end();

var out = ctx.sync.createSharedBinarySemaphore();
this.ctx.cmd.submit(0, new VCmdBuff[]{cmd}, new VSemaphore[]{tlasLink}, new int[]{VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR}, new VSemaphore[]{out}, fence);

ctx.sync.addCallback(fence, ()->{
in.free();
cmd.enqueueFree();
fence.free();
tlasLink.free();
ubo.free();
Vulkanite.INSTANCE.addSyncedCallback(out::free);
});

out.glWait(new int[0], outImgsGlIds, outImgsGlLayouts);
glFlush();
}

public void destory() {
vkDeviceWaitIdle(this.ctx.device);
this.pipeline.destory();
this.ctx.sync.checkFences();
this.singleUsePool.doReleases();
this.singleUsePool.free();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.coderbot.iris.texture.pbr.PBRTextureHolder;
import net.coderbot.iris.texture.pbr.PBRTextureManager;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.CommonUniforms;
import net.coderbot.iris.uniforms.SystemTimeUniforms;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
Expand Down Expand Up @@ -172,27 +171,27 @@ public VulkanPipeline(VContext ctx, AccelerationManager accelerationManager, Ray
supportsEntities = supportsEntitiesT;
try {
var commonSetExpected = new ShaderReflection.Set(new ShaderReflection.Binding[]{
new ShaderReflection.Binding("", 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, false),
new ShaderReflection.Binding("", 1, VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0, false),
new ShaderReflection.Binding("", 3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false),
new ShaderReflection.Binding("", 4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false),
new ShaderReflection.Binding("", 5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false),
new ShaderReflection.Binding("", 6, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, maxIrisRenderTargets, false),
new ShaderReflection.Binding("", 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, false, 1),
new ShaderReflection.Binding("", 1, VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0, false, 1),
new ShaderReflection.Binding("", 3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false, 1),
new ShaderReflection.Binding("", 4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false, 1),
new ShaderReflection.Binding("", 5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false, 1),
new ShaderReflection.Binding("", 6, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, maxIrisRenderTargets, false, 1),
});

var geomSetExpected = new ShaderReflection.Set(new ShaderReflection.Binding[]{
new ShaderReflection.Binding("", 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, true)
new ShaderReflection.Binding("", 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, true, 1)
});

ArrayList<ShaderReflection.Binding> customTexBindings = new ArrayList<>();
for (int i = 0; i < customTextureViews.length; i++) {
customTexBindings.add(new ShaderReflection.Binding("", i, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false));
customTexBindings.add(new ShaderReflection.Binding("", i, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, false, 2));
}
var customTexSetExpected = new ShaderReflection.Set(customTexBindings);

ArrayList<ShaderReflection.Binding> ssboBindings = new ArrayList<>();
for (int id : ssboIds) {
ssboBindings.add(new ShaderReflection.Binding("", id, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, true));
ssboBindings.add(new ShaderReflection.Binding("", id, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, true, 3));
}
var ssboSetExpected = new ShaderReflection.Set(ssboBindings);

Expand Down Expand Up @@ -394,7 +393,7 @@ public void renderPostShadows(List<VGImage> outImgs, Camera camera, ShaderStorag
sets[record.ssboSet] = ssboSet.set;
cmd.addTransientResource(ssboSet);
}
pipeline.bindDSet(cmd, sets);
pipeline.bindDSets(cmd, sets);
pipeline.trace(cmd, outImgs.get(0).width, outImgs.get(0).height, 1);

// Barrier on the output images
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package me.cortex.vulkanite.client.rendering.srp.api;

public interface VirtualResourceMapper {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.cortex.vulkanite.client.rendering.srp.api.execution;

import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.client.rendering.srp.api.VirtualResourceMapper;
import me.cortex.vulkanite.client.rendering.srp.api.layout.Layout;
import me.cortex.vulkanite.client.rendering.srp.graph.resource.Resource;
import me.cortex.vulkanite.lib.cmd.VCmdBuff;
import me.cortex.vulkanite.lib.descriptors.DescriptorSetLayoutBuilder;
import me.cortex.vulkanite.lib.descriptors.VDescriptorSet;
import me.cortex.vulkanite.lib.descriptors.VDescriptorSetLayout;
import me.cortex.vulkanite.lib.descriptors.VTypedDescriptorPool;

import java.util.List;

import static org.lwjgl.vulkan.VK10.VK_SHADER_STAGE_ALL;
import static org.lwjgl.vulkan.VK12.VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT;

//TODO: make into a cache thing so that it can reuse descriptor sets and VTypedDescriptorPool
public class DescriptorSetBuilder {
public static class BatchedUpdateDescriptorSet {
private final VTypedDescriptorPool pool;
public BatchedUpdateDescriptorSet(VTypedDescriptorPool pool) {
this.pool = pool;
}

public long updateAndGetSet(VCmdBuff cmd) {
var set = pool.allocateSet();
cmd.addTransientResource(set);
return set.set;
}
}

public static BatchedUpdateDescriptorSet createDescriptorSet(VirtualResourceMapper mapper, Layout layout, List<List<Resource<?>>> bindings) {
var layoutObj = createLayoutObject(layout);
var pool = new VTypedDescriptorPool(Vulkanite.INSTANCE.getCtx(), layoutObj, 0);
return new BatchedUpdateDescriptorSet(pool);
}

private static VDescriptorSetLayout createLayoutObject(Layout layout) {
if (layout.hasUnsizedArrays()) {
throw new IllegalStateException("User defined layouts that are runtime sized are not supported");
}

var builder = new DescriptorSetLayoutBuilder();
for (var binding : layout.getBindings()) {
if (binding.arraySize > 0) {
builder.binding(binding.index, binding.type, binding.arraySize, VK_SHADER_STAGE_ALL);
builder.setBindingFlags(binding.index, VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT);
} else {
builder.binding(binding.index, binding.type, VK_SHADER_STAGE_ALL);
}
}

return builder.build(Vulkanite.INSTANCE.getCtx());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.cortex.vulkanite.client.rendering.srp.api.execution;

import me.cortex.vulkanite.lib.base.VContext;
import me.cortex.vulkanite.lib.cmd.VCmdBuff;
import me.cortex.vulkanite.lib.other.sync.VFence;
import net.coderbot.iris.uniforms.CommonUniforms;
import net.minecraft.client.MinecraftClient;
import org.joml.Vector2i;
import org.joml.Vector3i;

public class ExecutionConstants {
public static ExecutionConstants INSTANCE = new ExecutionConstants();

public Vector2i getScreenSize() {
return new Vector2i(MinecraftClient.getInstance().getFramebuffer().textureWidth, MinecraftClient.getInstance().getFramebuffer().textureHeight);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
package me.cortex.vulkanite.client.rendering.srp.api.execution;

import me.cortex.vulkanite.client.rendering.srp.graph.resource.AccelerationResource;
import me.cortex.vulkanite.client.rendering.srp.graph.resource.BufferResource;
import me.cortex.vulkanite.client.rendering.srp.graph.resource.ExternalImageResource;
import me.cortex.vulkanite.client.rendering.srp.graph.resource.ImageResource;
import me.cortex.vulkanite.lib.memory.VAccelerationStructure;
import me.cortex.vulkanite.lib.memory.VBuffer;
import me.cortex.vulkanite.lib.memory.VImage;
import me.cortex.vulkanite.lib.base.VContext;
import me.cortex.vulkanite.lib.cmd.VCmdBuff;
import me.cortex.vulkanite.lib.other.sync.VFence;

//Provides mappings from virtual resource definitions to concrete objects
public class ExecutionContext {
public VAccelerationStructure getConcreteAcceleration(AccelerationResource resource) {
return null;
}

public VImage getConcreteExternalImage(ExternalImageResource resource) {
return null;
}

public VImage getConcreteImage(ImageResource resource) {
return null;
}

public VBuffer getConcreteBuffer(BufferResource resource) {
return null;
}
public final VContext ctx;
public final VCmdBuff cmd;
public final VFence fence;

public VBuffer getConcreteExternalBuffer(BufferResource resource) {
return null;
public ExecutionContext(VContext ctx, VCmdBuff cmd, VFence fence) {
this.ctx = ctx;
this.cmd = cmd;
this.fence = fence;
}
}
Loading

0 comments on commit 815c635

Please sign in to comment.