Skip to content

Commit

Permalink
Merge pull request #19 from bobcao3/master
Browse files Browse the repository at this point in the history
Make TLAS queues concurrent & shared images only on dedicated allocations
  • Loading branch information
MCRcortex authored Sep 23, 2023
2 parents dc54ca7 + 0a05a6f commit f37a28e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.lwjgl.vulkan.*;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;

import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.util.vma.Vma.VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
Expand Down Expand Up @@ -336,8 +337,8 @@ private final class TLASSectionManager extends TLASGeometryManager {
private record DescUpdateJob(int binding, int dstArrayElement, List<VBuffer> buffers) {}
private record ArenaDeallocJob(int index, int count, List<VBuffer> geometryBuffers) {}

private final Deque<DescUpdateJob> descUpdateJobs = new ArrayDeque<>();
private final Deque<ArenaDeallocJob> arenaDeallocJobs = new ArrayDeque<>();
private final ConcurrentLinkedDeque<DescUpdateJob> descUpdateJobs = new ConcurrentLinkedDeque<>();
private final ConcurrentLinkedDeque<ArenaDeallocJob> arenaDeallocJobs = new ConcurrentLinkedDeque<>();
private final Deque<VDescriptorPool> descPoolsToRelease = new ArrayDeque<>();

public void resizeBindlessSet(int newSize, VFence fence) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public record HandleDescriptorTracked(HandleDescriptor desc, int refCount) {
private static final HashMap<Long, HandleDescriptorTracked> MEMORY_TO_HANDLES = new HashMap<>();

// Get the GL memory associated with the given vulkan memory object
public static int acquire(VmaAllocator.Allocation allocation, VkDevice device) {
public static int acquire(VmaAllocator.Allocation allocation, VkDevice device, boolean dedicated) {
synchronized (MEMORY_TO_HANDLES) {
long vkMemory = allocation.ai.deviceMemory();
if (!MEMORY_TO_HANDLES.containsKey(vkMemory)) {
Expand Down Expand Up @@ -90,7 +90,7 @@ public static int acquire(VmaAllocator.Allocation allocation, VkDevice device) {

int newMemoryObject = glCreateMemoryObjectsEXT();
// Everything larger than the shared block size must be dedicated allocation
long memorySize = Long.max(allocation.ai.offset() + allocation.ai.size(), sharedBlockSize);
long memorySize = dedicated ? (allocation.ai.offset() + allocation.ai.size()) : sharedBlockSize;

if (memorySize > sharedBlockSize) {
// Section 6.2 of the OpenGL 4.5 spec
Expand Down Expand Up @@ -169,7 +169,7 @@ public VGBuffer createSharedBuffer(long size, int usage, int properties) {

var alloc = allocator.allocShared(bufferCreateInfo, allocationCreateInfo);

int memoryObject = ExternalMemoryTracker.acquire(alloc, device);
int memoryObject = ExternalMemoryTracker.acquire(alloc, device, alloc.isDedicated());

int glId = glCreateBuffers();
glNamedBufferStorageMemEXT(glId, size, memoryObject, alloc.ai.offset());
Expand Down Expand Up @@ -214,7 +214,7 @@ public VGImage createSharedImage(int width, int height, int depth, int mipLevels

var alloc = allocator.allocShared(createInfo, allocInfo);

int memoryObject = ExternalMemoryTracker.acquire(alloc, device);
int memoryObject = ExternalMemoryTracker.acquire(alloc, device, alloc.isDedicated());

int glId = glCreateTextures(glImageType);

Expand Down
34 changes: 24 additions & 10 deletions src/main/java/me/cortex/vulkanite/lib/memory/VmaAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ SharedBufferAllocation allocShared(VkBufferCreateInfo bufferCreateInfo,

var memReq = VkMemoryRequirements.calloc(stack);
vkGetBufferMemoryRequirements(device, buffer, memReq);
allocationCreateInfo.memoryTypeBits(memReq.memoryTypeBits());

if (memReq.size() > sharedBlockSize) {
allocationCreateInfo.flags(VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT);
Expand All @@ -130,7 +131,8 @@ SharedBufferAllocation allocShared(VkBufferCreateInfo bufferCreateInfo,
_CHECK_(vmaBindBufferMemory(allocator, allocation, buffer), "failed to bind buffer memory");

boolean hasDeviceAddress = ((bufferCreateInfo.usage() & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) > 0);
return new SharedBufferAllocation(buffer, allocation, vai, hasDeviceAddress);
return new SharedBufferAllocation(buffer, allocation, vai, hasDeviceAddress,
memReq.size() > sharedBlockSize);
}
}

Expand Down Expand Up @@ -168,12 +170,9 @@ SharedImageAllocation allocShared(VkImageCreateInfo imageCreateInfo, VmaAllocati
var memReq = VkMemoryRequirements.calloc(stack);
vkGetImageMemoryRequirements(device, image, memReq);

if (memReq.size() > sharedBlockSize) {
allocationCreateInfo.flags(VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT);
allocationCreateInfo.pool(sharedDedicatedPool);
} else {
allocationCreateInfo.pool(sharedPool);
}
allocationCreateInfo.flags(VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT)
.pool(sharedDedicatedPool)
.memoryTypeBits(memReq.memoryTypeBits());

long allocation = 0;
VmaAllocationInfo vai = VmaAllocationInfo.calloc();
Expand All @@ -184,7 +183,7 @@ SharedImageAllocation allocShared(VkImageCreateInfo imageCreateInfo, VmaAllocati
allocation = pAllocation.get(0);
_CHECK_(vmaBindImageMemory(allocator, allocation, image), "failed to bind image memory");

return new SharedImageAllocation(image, allocation, vai);
return new SharedImageAllocation(image, allocation, vai, true);
}
}

Expand Down Expand Up @@ -288,8 +287,12 @@ public void flush(long offset, long size) {
}

public class SharedBufferAllocation extends BufferAllocation {
public SharedBufferAllocation(long buffer, long allocation, VmaAllocationInfo info, boolean hasDeviceAddress) {
private final boolean dedicated;

public SharedBufferAllocation(long buffer, long allocation, VmaAllocationInfo info, boolean hasDeviceAddress,
boolean dedicated) {
super(buffer, allocation, info, hasDeviceAddress);
this.dedicated = dedicated;
}

@Override
Expand All @@ -299,6 +302,10 @@ public void free() {
free0();
ai.free();
}

boolean isDedicated() {
return dedicated;
}
}

public class ImageAllocation extends Allocation {
Expand All @@ -318,8 +325,11 @@ public void free() {
}

public class SharedImageAllocation extends ImageAllocation {
public SharedImageAllocation(long image, long allocation, VmaAllocationInfo info) {
private final boolean dedicated;

public SharedImageAllocation(long image, long allocation, VmaAllocationInfo info, boolean dedicated) {
super(image, allocation, info);
this.dedicated = dedicated;
}

@Override
Expand All @@ -330,6 +340,10 @@ public void free() {
free0();
ai.free();
}

boolean isDedicated() {
return dedicated;
}
}

public String dumpJson(boolean detailed) {
Expand Down

0 comments on commit f37a28e

Please sign in to comment.