Skip to content

Commit

Permalink
Merge pull request #26 from bobcao3/pipelines
Browse files Browse the repository at this point in the history
Pipeline validation & iris rendertargets
  • Loading branch information
MCRcortex authored Oct 7, 2023
2 parents de2f925 + e112e31 commit 2e31131
Show file tree
Hide file tree
Showing 15 changed files with 660 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//Multithreaded acceleration manager, builds blas's in a seperate queue,
// then memory copies over to main, while doing compaction

import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.compat.IAccelerationBuildResult;
import me.cortex.vulkanite.lib.base.VContext;
import me.cortex.vulkanite.lib.cmd.VCmdBuff;
Expand Down Expand Up @@ -443,7 +444,6 @@ public void enqueue(List<ChunkBuildOutput> batch) {
for (var entry : acbr.entrySet()) {
int flag = entry.getKey() == DefaultTerrainRenderPasses.SOLID ? VK_GEOMETRY_OPAQUE_BIT_KHR : 0;
buildData.add(new BLASTriangleData(entry.getValue().quadCount(), flag));
// TODO: Just don't create this data in the first place

var geometry = cbr.getMesh(entry.getKey());
if (geometry.getVertexData().getLength() == 0) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/me/cortex/vulkanite/client/Vulkanite.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import me.cortex.vulkanite.client.rendering.VulkanPipeline;
import me.cortex.vulkanite.lib.base.VContext;
import me.cortex.vulkanite.lib.base.initalizer.VInitializer;
import me.cortex.vulkanite.lib.descriptors.VDescriptorPool;
import me.cortex.vulkanite.lib.descriptors.VDescriptorSetLayout;
import me.cortex.vulkanite.lib.descriptors.VTypedDescriptorPool;
import me.jellysquid.mods.sodium.client.render.chunk.RenderSection;
import me.jellysquid.mods.sodium.client.render.chunk.compile.ChunkBuildOutput;
import net.minecraft.util.Util;
import org.lwjgl.vulkan.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.lwjgl.vulkan.EXTDebugUtils.VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
Expand Down Expand Up @@ -50,6 +54,7 @@ public class Vulkanite {
private final ArbitarySyncPointCallback fencedCallback = new ArbitarySyncPointCallback();

private final AccelerationManager accelerationManager;
private final HashMap<VDescriptorSetLayout, VTypedDescriptorPool> descriptorPools = new HashMap<>();

public Vulkanite() {
ctx = createVulkanContext();
Expand All @@ -70,6 +75,24 @@ public void upload(List<ChunkBuildOutput> results) {
accelerationManager.chunkBuilds(results);
}

public VTypedDescriptorPool getPoolByLayout(VDescriptorSetLayout layout) {
synchronized (descriptorPools) {
if (!descriptorPools.containsKey(layout)) {
descriptorPools.put(layout, new VTypedDescriptorPool(ctx, layout, 0));
}
return descriptorPools.get(layout);
}
}

public void removePoolByLayout(VDescriptorSetLayout layout) {
synchronized (descriptorPools) {
if (descriptorPools.containsKey(layout)) {
descriptorPools.get(layout).free();
descriptorPools.remove(layout);
}
}
}

public void sectionRemove(RenderSection section) {
accelerationManager.sectionRemove(section);
}
Expand All @@ -92,6 +115,9 @@ public void addSyncedCallback(Runnable callback) {
}

public void destroy() {
for (var pool : descriptorPools.values()) {
pool.free();
}
accelerationManager.cleanup();
}

Expand Down
290 changes: 181 additions & 109 deletions src/main/java/me/cortex/vulkanite/client/rendering/VulkanPipeline.java

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions src/main/java/me/cortex/vulkanite/lib/cmd/VCmdBuff.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@
import static org.lwjgl.vulkan.KHRAccelerationStructure.VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
import static org.lwjgl.vulkan.KHRAccelerationStructure.VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;

//TODO: Track with TrackedResourceObject but need to be careful due to how the freeing works
public class VCmdBuff extends TrackedResourceObject implements Pointer {
private final VCommandPool pool;
public final VkCommandBuffer buffer;

private LinkedList<VBuffer> transientBuffers;
private HashSet<TrackedResourceObject> transientResources;

VCmdBuff(VCommandPool pool, VkCommandBuffer buff) {
this.pool = pool;
this.buffer = buff;
this.transientBuffers = new LinkedList<>();
this.transientResources = new HashSet<>();
}

//Enqueues the pool to be freed by the owning thread
Expand Down Expand Up @@ -69,7 +71,7 @@ public void encodeDataUpload(MemoryManager manager, long src, VBuffer dest, long
vkCmdCopyBuffer(buffer, staging.buffer(), dest.buffer(), copy);
}

transientBuffers.add(staging);
transientResources.add(staging);
}

public void encodeImageUpload(MemoryManager manager, long src, VImage dest, long srcSize, int destLayout) {
Expand All @@ -89,7 +91,7 @@ public void encodeImageUpload(MemoryManager manager, long src, VImage dest, long
vkCmdCopyBufferToImage(buffer, staging.buffer(), dest.image(), destLayout, copy);
}

transientBuffers.add(staging);
transientResources.add(staging);
}

public void encodeMemoryBarrier() {
Expand All @@ -102,6 +104,10 @@ public void encodeMemoryBarrier() {
}
}

public void addTransientResource(TrackedResourceObject resource) {
transientResources.add(resource);
}

public static int dstStageToAccess(int dstStage) {
switch (dstStage) {
case VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT:
Expand Down Expand Up @@ -200,8 +206,7 @@ public void free() {
void freeInternal() {
free0();
vkFreeCommandBuffers(pool.device, pool.pool, buffer);
while (!transientBuffers.isEmpty()) {
transientBuffers.removeFirst().free();
}
transientResources.forEach(TrackedResourceObject::free);
transientResources.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.cortex.vulkanite.lib.memory.VBuffer;
import me.cortex.vulkanite.lib.other.VImageView;
import me.cortex.vulkanite.lib.other.VSampler;
import me.cortex.vulkanite.lib.shader.reflection.ShaderReflection;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkDescriptorBufferInfo;
import org.lwjgl.vulkan.VkDescriptorImageInfo;
Expand All @@ -21,6 +22,8 @@ public class DescriptorUpdateBuilder {
private final MemoryStack stack;
private final VkWriteDescriptorSet.Buffer updates;
private final VImageView placeholderImageView;
private ShaderReflection.Set refSet = null;

public DescriptorUpdateBuilder(VContext ctx, int maxUpdates) {
this(ctx, maxUpdates, null);
}
Expand All @@ -32,6 +35,15 @@ public DescriptorUpdateBuilder(VContext ctx, int maxUpdates, VImageView placehol
this.placeholderImageView = placeholderImageView;
}

public DescriptorUpdateBuilder(VContext ctx, ShaderReflection.Set refSet) {
this(ctx, refSet, null);
}

public DescriptorUpdateBuilder(VContext ctx, ShaderReflection.Set refSet, VImageView placeholderImageView) {
this(ctx, refSet.bindings().size(), placeholderImageView);
this.refSet = refSet;
}

private long viewOrPlaceholder(VImageView v) {
if (v == null && placeholderImageView == null) return 0;
return v == null ? placeholderImageView.view : v.view;
Expand All @@ -47,6 +59,9 @@ public DescriptorUpdateBuilder buffer(int binding, VBuffer buffer) {
return buffer(binding, buffer, 0, VK_WHOLE_SIZE);
}
public DescriptorUpdateBuilder buffer(int binding, VBuffer buffer, long offset, long range) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
updates.get()
.sType$Default()
.dstBinding(binding)
Expand All @@ -63,6 +78,9 @@ public DescriptorUpdateBuilder buffer(int binding, VBuffer buffer, long offset,
}

public DescriptorUpdateBuilder buffer(int binding, int dstArrayElement, List<VBuffer> buffers) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
var bufInfo = VkDescriptorBufferInfo.calloc(buffers.size(), stack);
for (int i = 0; i < buffers.size(); i++) {
bufInfo.get(i)
Expand All @@ -87,6 +105,9 @@ public DescriptorUpdateBuilder uniform(int binding, VBuffer buffer) {
return uniform(binding, buffer, 0, VK_WHOLE_SIZE);
}
public DescriptorUpdateBuilder uniform(int binding, VBuffer buffer, long offset, long range) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
updates.get()
.sType$Default()
.dstBinding(binding)
Expand All @@ -102,6 +123,9 @@ public DescriptorUpdateBuilder uniform(int binding, VBuffer buffer, long offset,
}

public DescriptorUpdateBuilder acceleration(int binding, VAccelerationStructure... structures) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
var buff = stack.mallocLong(structures.length);
for (var structure : structures) {
buff.put(structure.structure);
Expand All @@ -119,10 +143,32 @@ public DescriptorUpdateBuilder acceleration(int binding, VAccelerationStructure.
return this;
}

public DescriptorUpdateBuilder imageStore(int binding, int dstArrayElement, List<VImageView> views) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
var imgInfo = VkDescriptorImageInfo.calloc(views.size(), stack);
for (int i = 0; i < views.size(); i++) {
imgInfo.get(i)
.imageLayout(VK_IMAGE_LAYOUT_GENERAL)
.imageView(viewOrPlaceholder(views.get(i)));
}
updates.get()
.sType$Default()
.dstBinding(binding)
.dstSet(set)
.descriptorType(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
.descriptorCount(views.size())
.pImageInfo(imgInfo);
return this;
}
public DescriptorUpdateBuilder imageStore(int binding, VImageView view) {
return imageStore(binding, VK_IMAGE_LAYOUT_GENERAL, view);
}
public DescriptorUpdateBuilder imageStore(int binding, int layout, VImageView view) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
updates.get()
.sType$Default()
.dstBinding(binding)
Expand All @@ -141,6 +187,9 @@ public DescriptorUpdateBuilder imageSampler(int binding, VImageView view, VSampl
}

public DescriptorUpdateBuilder imageSampler(int binding, int layout, VImageView view, VSampler sampler) {
if (refSet != null && refSet.getBindingAt(binding) == null) {
return this;
}
updates.get()
.sType$Default()
.dstBinding(binding)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.cortex.vulkanite.lib.descriptors;

import me.cortex.vulkanite.lib.base.TrackedResourceObject;

public class VDescriptorSet extends TrackedResourceObject {
private final VTypedDescriptorPool pool;
public final long poolHandle;
public final long set;

VDescriptorSet(VTypedDescriptorPool pool, long poolHandle, long set) {
this.pool = pool;
this.poolHandle = poolHandle;
this.set = set;
}

@Override
public void free() {
free0();
pool.freeSet(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.cortex.vulkanite.lib.descriptors;

import me.cortex.vulkanite.client.Vulkanite;
import me.cortex.vulkanite.lib.base.TrackedResourceObject;
import me.cortex.vulkanite.lib.base.VContext;
import org.lwjgl.system.Pointer;
Expand Down Expand Up @@ -32,6 +33,7 @@ public long address() {

@Override
public void free() {
Vulkanite.INSTANCE.removePoolByLayout(this);
free0();
vkDestroyDescriptorSetLayout(ctx.device, layout, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package me.cortex.vulkanite.lib.descriptors;

import me.cortex.vulkanite.lib.base.TrackedResourceObject;
import me.cortex.vulkanite.lib.base.VContext;
import org.lwjgl.vulkan.VkDescriptorPoolCreateInfo;
import org.lwjgl.vulkan.VkDescriptorPoolSize;
import org.lwjgl.vulkan.VkDescriptorSetAllocateInfo;

import java.nio.LongBuffer;
import java.util.ArrayList;

import static me.cortex.vulkanite.lib.other.VUtil._CHECK_;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.vulkan.VK10.*;

public class VTypedDescriptorPool extends TrackedResourceObject {
private final VContext ctx;
private final ArrayList<Long> pools = new ArrayList<>();
private final ArrayList<Integer> poolFreeSizes = new ArrayList<>();
private final VDescriptorSetLayout layout;
private final int flags;

private static final int nSetsPerPool = 16;

public VTypedDescriptorPool(VContext ctx, VDescriptorSetLayout layout, int flags) {
this.ctx = ctx;
this.layout = layout;
this.flags = flags;
}

private void createNewPool() {
try (var stack = stackPush()) {
var sizes = VkDescriptorPoolSize.calloc(layout.types.length, stack);
for (int i = 0; i < layout.types.length; i++) {
sizes.get(i).type(layout.types[i]).descriptorCount(nSetsPerPool);
}
LongBuffer pPool = stack.mallocLong(1);
_CHECK_(vkCreateDescriptorPool(ctx.device, VkDescriptorPoolCreateInfo.calloc(stack)
.sType$Default()
.flags(flags | VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)
.maxSets(nSetsPerPool)
.pPoolSizes(sizes), null, pPool));
pools.add(pPool.get(0));
poolFreeSizes.add(nSetsPerPool);
}
}

public VDescriptorSet allocateSet() {
if (poolFreeSizes.isEmpty() || poolFreeSizes.get(pools.size() - 1) == 0) {
createNewPool();
}
long pool = pools.get(pools.size() - 1);
long set;
poolFreeSizes.set(pools.size() - 1, poolFreeSizes.get(pools.size() - 1) - 1);
try (var stack = stackPush()) {
var pSet = stack.mallocLong(1);
_CHECK_(vkAllocateDescriptorSets(ctx.device, VkDescriptorSetAllocateInfo.calloc(stack)
.sType$Default()
.descriptorPool(pool)
.pSetLayouts(stack.mallocLong(1).put(0, layout.layout)), pSet));
set = pSet.get(0);
}
return new VDescriptorSet(this, pool, set);
}

public void freeSet(VDescriptorSet set) {
int index = pools.indexOf(set.poolHandle);
try (var stack = stackPush()) {
var pDescriptorSets = stack.mallocLong(1).put(0, set.set);
_CHECK_(vkFreeDescriptorSets(ctx.device, set.poolHandle, pDescriptorSets));
}
poolFreeSizes.set(index, poolFreeSizes.get(index) + 1);
if (poolFreeSizes.get(index) == nSetsPerPool) {
vkDestroyDescriptorPool(ctx.device, set.poolHandle, null);
pools.remove(index);
poolFreeSizes.remove(index);
}
}

@Override
public void free() {
free0();
for (long pool : pools) {
vkDestroyDescriptorPool(ctx.device, pool, null);
}
}
}
Loading

0 comments on commit 2e31131

Please sign in to comment.