Skip to content

Commit

Permalink
VMA is internally synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
bobcao3 committed Sep 23, 2023
1 parent 39ee189 commit fca1c34
Showing 1 changed file with 40 additions and 93 deletions.
133 changes: 40 additions & 93 deletions src/main/java/me/cortex/vulkanite/lib/memory/VmaAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ public VmaAllocator(VkDevice device, boolean enableDeviceAddresses, long sharedB
}
}

// TODO: FIXME: find a better way to synchronize this, since it needs to be very
// fast
private static final Lock ALLOCATOR_LOCK = new ReentrantLock();

// NOTE: SHOULD ONLY BE USED TO ALLOCATE SHARED MEMORY AND STUFF, not
// recommended
SharedBufferAllocation allocShared(VkBufferCreateInfo bufferCreateInfo,
Expand All @@ -125,17 +121,13 @@ SharedBufferAllocation allocShared(VkBufferCreateInfo bufferCreateInfo,

long allocation = 0;
VmaAllocationInfo vai = VmaAllocationInfo.calloc();
ALLOCATOR_LOCK.lock();
try {
PointerBuffer pAllocation = stack.mallocPointer(1);
_CHECK_(
vmaAllocateMemoryForBuffer(allocator, buffer, allocationCreateInfo, pAllocation, vai),
"Failed to allocate memory for buffer");
allocation = pAllocation.get(0);
_CHECK_(vmaBindBufferMemory(allocator, allocation, buffer), "failed to bind buffer memory");
} finally {
ALLOCATOR_LOCK.unlock();
}

PointerBuffer pAllocation = stack.mallocPointer(1);
_CHECK_(
vmaAllocateMemoryForBuffer(allocator, buffer, allocationCreateInfo, pAllocation, vai),
"Failed to allocate memory for buffer");
allocation = pAllocation.get(0);
_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);
Expand All @@ -153,20 +145,15 @@ BufferAllocation alloc(long pool, VkBufferCreateInfo bufferCreateInfo, VmaAlloca
LongBuffer pb = stack.mallocLong(1);
PointerBuffer pa = stack.mallocPointer(1);
VmaAllocationInfo vai = VmaAllocationInfo.calloc();
ALLOCATOR_LOCK.lock();
try {
_CHECK_(
vmaCreateBufferWithAlignment(allocator,
bufferCreateInfo,
allocationCreateInfo.pool(pool),
alignment,
pb,
pa,
vai),
"Failed to allocate buffer");
} finally {
ALLOCATOR_LOCK.unlock();
}
_CHECK_(
vmaCreateBufferWithAlignment(allocator,
bufferCreateInfo,
allocationCreateInfo.pool(pool),
alignment,
pb,
pa,
vai),
"Failed to allocate buffer");
return new BufferAllocation(pb.get(0), pa.get(0), vai,
(bufferCreateInfo.usage() & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) != 0);
}
Expand All @@ -190,17 +177,12 @@ SharedImageAllocation allocShared(VkImageCreateInfo imageCreateInfo, VmaAllocati

long allocation = 0;
VmaAllocationInfo vai = VmaAllocationInfo.calloc();
ALLOCATOR_LOCK.lock();
try {
PointerBuffer pAllocation = stack.mallocPointer(1);
_CHECK_(
vmaAllocateMemoryForImage(allocator, image, allocationCreateInfo, pAllocation, vai),
"Failed to allocate memory for image");
allocation = pAllocation.get(0);
_CHECK_(vmaBindImageMemory(allocator, allocation, image), "failed to bind image memory");
} finally {
ALLOCATOR_LOCK.unlock();
}
PointerBuffer pAllocation = stack.mallocPointer(1);
_CHECK_(
vmaAllocateMemoryForImage(allocator, image, allocationCreateInfo, pAllocation, vai),
"Failed to allocate memory for image");
allocation = pAllocation.get(0);
_CHECK_(vmaBindImageMemory(allocator, allocation, image), "failed to bind image memory");

return new SharedImageAllocation(image, allocation, vai);
}
Expand Down Expand Up @@ -266,13 +248,8 @@ public BufferAllocation(long buffer, long allocation, VmaAllocationInfo info, bo
@Override
public void free() {
// vkFreeMemory();
ALLOCATOR_LOCK.lock();
try {
vmaDestroyBuffer(allocator, buffer, allocation);
super.free();
} finally {
ALLOCATOR_LOCK.unlock();
}
vmaDestroyBuffer(allocator, buffer, allocation);
super.free();
}

public VkDevice getDevice() {
Expand All @@ -281,25 +258,15 @@ public VkDevice getDevice() {

// TODO: Maybe put the following 3 in VBuffer
public long map() {
ALLOCATOR_LOCK.lock();
try {
try (var stack = stackPush()) {
PointerBuffer res = stack.callocPointer(1);
_CHECK_(vmaMapMemory(allocator, allocation, res));
return res.get(0);
}
} finally {
ALLOCATOR_LOCK.unlock();
try (var stack = stackPush()) {
PointerBuffer res = stack.callocPointer(1);
_CHECK_(vmaMapMemory(allocator, allocation, res));
return res.get(0);
}
}

public void unmap() {
ALLOCATOR_LOCK.lock();
try {
vmaUnmapMemory(allocator, allocation);
} finally {
ALLOCATOR_LOCK.unlock();
}
vmaUnmapMemory(allocator, allocation);
}

public void flush(long offset, long size) {
Expand All @@ -314,12 +281,7 @@ public void flush(long offset, long size) {
* .size(size)
* .offset(ai.offset()+offset)));
*/
ALLOCATOR_LOCK.lock();
try {
vmaFlushAllocation(allocator, allocation, offset, size);
} finally {
ALLOCATOR_LOCK.unlock();
}
vmaFlushAllocation(allocator, allocation, offset, size);
}
}

Expand All @@ -332,15 +294,10 @@ public SharedBufferAllocation(long buffer, long allocation, VmaAllocationInfo in

@Override
public void free() {
ALLOCATOR_LOCK.lock();
try {
vkDestroyBuffer(device, buffer, null);
vmaFreeMemory(allocator, allocation);
free0();
ai.free();
} finally {
ALLOCATOR_LOCK.unlock();
}
vkDestroyBuffer(device, buffer, null);
vmaFreeMemory(allocator, allocation);
free0();
ai.free();
}
}

Expand All @@ -355,13 +312,8 @@ public ImageAllocation(long image, long allocation, VmaAllocationInfo info) {
@Override
public void free() {
// vkFreeMemory();
ALLOCATOR_LOCK.lock();
try {
vmaDestroyImage(allocator, image, allocation);
super.free();
} finally {
ALLOCATOR_LOCK.unlock();
}
vmaDestroyImage(allocator, image, allocation);
super.free();
}
}

Expand All @@ -373,15 +325,10 @@ public SharedImageAllocation(long image, long allocation, VmaAllocationInfo info
@Override
public void free() {
// vkFreeMemory();
ALLOCATOR_LOCK.lock();
try {
vkDestroyImage(device, image, null);
vmaFreeMemory(allocator, allocation);
free0();
ai.free();
} finally {
ALLOCATOR_LOCK.unlock();
}
vkDestroyImage(device, image, null);
vmaFreeMemory(allocator, allocation);
free0();
ai.free();
}
}

Expand Down

0 comments on commit fca1c34

Please sign in to comment.