-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
967 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 145 additions & 3 deletions
148
src/main/java/me/cortex/vulkanite/client/rendering/VkPipeline2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/main/java/me/cortex/vulkanite/client/rendering/srp/SRPContext.java
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
src/main/java/me/cortex/vulkanite/client/rendering/srp/api/VirtualResourceMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
56 changes: 56 additions & 0 deletions
56
...ain/java/me/cortex/vulkanite/client/rendering/srp/api/execution/DescriptorSetBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/me/cortex/vulkanite/client/rendering/srp/api/execution/ExecutionConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
35 changes: 10 additions & 25 deletions
35
src/main/java/me/cortex/vulkanite/client/rendering/srp/api/execution/ExecutionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.