Skip to content

Commit d93ac5e

Browse files
clayjohndarksylinc
andcommitted
Restore using VMA to create buffers and images when we don't need to extra gpu memory tracking.
VMA handles memory allocation on certain devices better than our custom VK code, so we might as well use it Co-authored-by: Matias N. Goldberg <[email protected]>
1 parent 1a0bf54 commit d93ac5e

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

drivers/vulkan/rendering_device_driver_vulkan.cpp

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,9 @@ RDD::BufferID RenderingDeviceDriverVulkan::buffer_create(uint64_t p_size, BitFie
15221522
create_info.usage = p_usage;
15231523
create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
15241524

1525+
VmaMemoryUsage vma_usage = VMA_MEMORY_USAGE_UNKNOWN;
1526+
uint32_t vma_flags_to_remove = 0;
1527+
15251528
VmaAllocationCreateInfo alloc_create_info = {};
15261529
switch (p_allocation_type) {
15271530
case MEMORY_ALLOCATION_TYPE_CPU: {
@@ -1531,15 +1534,19 @@ RDD::BufferID RenderingDeviceDriverVulkan::buffer_create(uint64_t p_size, BitFie
15311534
// Looks like a staging buffer: CPU maps, writes sequentially, then GPU copies to VRAM.
15321535
alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
15331536
alloc_create_info.preferredFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1537+
vma_flags_to_remove |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
15341538
}
15351539
if (is_dst && !is_src) {
15361540
// Looks like a readback buffer: GPU copies from VRAM, then CPU maps and reads.
15371541
alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
15381542
alloc_create_info.preferredFlags |= VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
1543+
vma_flags_to_remove |= VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
15391544
}
1545+
vma_usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
15401546
alloc_create_info.requiredFlags = (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
15411547
} break;
15421548
case MEMORY_ALLOCATION_TYPE_GPU: {
1549+
vma_usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
15431550
alloc_create_info.preferredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
15441551
if (p_size <= SMALL_ALLOCATION_MAX_SIZE) {
15451552
uint32_t mem_type_index = 0;
@@ -1553,12 +1560,19 @@ RDD::BufferID RenderingDeviceDriverVulkan::buffer_create(uint64_t p_size, BitFie
15531560
VmaAllocation allocation = nullptr;
15541561
VmaAllocationInfo alloc_info = {};
15551562

1556-
VkResult err = vkCreateBuffer(vk_device, &create_info, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER), &vk_buffer);
1557-
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't create buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1558-
err = vmaAllocateMemoryForBuffer(allocator, vk_buffer, &alloc_create_info, &allocation, &alloc_info);
1559-
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't allocate memory for buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1560-
err = vmaBindBufferMemory2(allocator, allocation, 0, vk_buffer, nullptr);
1561-
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't bind memory to buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1563+
if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) {
1564+
alloc_create_info.preferredFlags &= ~vma_flags_to_remove;
1565+
alloc_create_info.usage = vma_usage;
1566+
VkResult err = vmaCreateBuffer(allocator, &create_info, &alloc_create_info, &vk_buffer, &allocation, &alloc_info);
1567+
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't create buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1568+
} else {
1569+
VkResult err = vkCreateBuffer(vk_device, &create_info, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER), &vk_buffer);
1570+
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't create buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1571+
err = vmaAllocateMemoryForBuffer(allocator, vk_buffer, &alloc_create_info, &allocation, &alloc_info);
1572+
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't allocate memory for buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1573+
err = vmaBindBufferMemory2(allocator, allocation, 0, vk_buffer, nullptr);
1574+
ERR_FAIL_COND_V_MSG(err, BufferID(), "Can't bind memory to buffer of size: " + itos(p_size) + ", error " + itos(err) + ".");
1575+
}
15621576

15631577
// Bookkeep.
15641578
BufferInfo *buf_info = VersatileResource::allocate<BufferInfo>(resources_allocator);
@@ -1593,8 +1607,12 @@ void RenderingDeviceDriverVulkan::buffer_free(BufferID p_buffer) {
15931607
vkDestroyBufferView(vk_device, buf_info->vk_view, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER_VIEW));
15941608
}
15951609

1596-
vkDestroyBuffer(vk_device, buf_info->vk_buffer, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER));
1597-
vmaFreeMemory(allocator, buf_info->allocation.handle);
1610+
if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) {
1611+
vmaDestroyBuffer(allocator, buf_info->vk_buffer, buf_info->allocation.handle);
1612+
} else {
1613+
vkDestroyBuffer(vk_device, buf_info->vk_buffer, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER));
1614+
vmaFreeMemory(allocator, buf_info->allocation.handle);
1615+
}
15981616

15991617
VersatileResource::free(resources_allocator, buf_info);
16001618
}
@@ -1808,12 +1826,18 @@ RDD::TextureID RenderingDeviceDriverVulkan::texture_create(const TextureFormat &
18081826
VmaAllocation allocation = nullptr;
18091827
VmaAllocationInfo alloc_info = {};
18101828

1811-
VkResult err = vkCreateImage(vk_device, &create_info, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE), &vk_image);
1812-
ERR_FAIL_COND_V_MSG(err, TextureID(), "vkCreateImage failed with error " + itos(err) + ".");
1813-
err = vmaAllocateMemoryForImage(allocator, vk_image, &alloc_create_info, &allocation, &alloc_info);
1814-
ERR_FAIL_COND_V_MSG(err, TextureID(), "Can't allocate memory for image, error: " + itos(err) + ".");
1815-
err = vmaBindImageMemory2(allocator, allocation, 0, vk_image, nullptr);
1816-
ERR_FAIL_COND_V_MSG(err, TextureID(), "Can't bind memory to image, error: " + itos(err) + ".");
1829+
if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) {
1830+
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
1831+
VkResult err = vmaCreateImage(allocator, &create_info, &alloc_create_info, &vk_image, &allocation, &alloc_info);
1832+
ERR_FAIL_COND_V_MSG(err, TextureID(), "vmaCreateImage failed with error " + itos(err) + ".");
1833+
} else {
1834+
VkResult err = vkCreateImage(vk_device, &create_info, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE), &vk_image);
1835+
ERR_FAIL_COND_V_MSG(err, TextureID(), "vkCreateImage failed with error " + itos(err) + ".");
1836+
err = vmaAllocateMemoryForImage(allocator, vk_image, &alloc_create_info, &allocation, &alloc_info);
1837+
ERR_FAIL_COND_V_MSG(err, TextureID(), "Can't allocate memory for image, error: " + itos(err) + ".");
1838+
err = vmaBindImageMemory2(allocator, allocation, 0, vk_image, nullptr);
1839+
ERR_FAIL_COND_V_MSG(err, TextureID(), "Can't bind memory to image, error: " + itos(err) + ".");
1840+
}
18171841

18181842
// Create view.
18191843

@@ -1845,10 +1869,15 @@ RDD::TextureID RenderingDeviceDriverVulkan::texture_create(const TextureFormat &
18451869
}
18461870

18471871
VkImageView vk_image_view = VK_NULL_HANDLE;
1848-
err = vkCreateImageView(vk_device, &image_view_create_info, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE_VIEW), &vk_image_view);
1872+
VkResult err = vkCreateImageView(vk_device, &image_view_create_info, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE_VIEW), &vk_image_view);
18491873
if (err) {
1850-
vkDestroyImage(vk_device, vk_image, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE));
1851-
vmaFreeMemory(allocator, allocation);
1874+
if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) {
1875+
vmaDestroyImage(allocator, vk_image, allocation);
1876+
} else {
1877+
vkDestroyImage(vk_device, vk_image, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE));
1878+
vmaFreeMemory(allocator, allocation);
1879+
}
1880+
18521881
ERR_FAIL_COND_V_MSG(err, TextureID(), "vkCreateImageView failed with error " + itos(err) + ".");
18531882
}
18541883

@@ -2023,8 +2052,12 @@ void RenderingDeviceDriverVulkan::texture_free(TextureID p_texture) {
20232052
TextureInfo *tex_info = (TextureInfo *)p_texture.id;
20242053
vkDestroyImageView(vk_device, tex_info->vk_view, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_IMAGE_VIEW));
20252054
if (tex_info->allocation.handle) {
2026-
vkDestroyImage(vk_device, tex_info->vk_image, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER));
2027-
vmaFreeMemory(allocator, tex_info->allocation.handle);
2055+
if (!Engine::get_singleton()->is_extra_gpu_memory_tracking_enabled()) {
2056+
vmaDestroyImage(allocator, tex_info->vk_view_create_info.image, tex_info->allocation.handle);
2057+
} else {
2058+
vkDestroyImage(vk_device, tex_info->vk_image, VKC::get_allocation_callbacks(VK_OBJECT_TYPE_BUFFER));
2059+
vmaFreeMemory(allocator, tex_info->allocation.handle);
2060+
}
20282061
}
20292062
VersatileResource::free(resources_allocator, tex_info);
20302063
}

0 commit comments

Comments
 (0)