1 #ifndef HALIDE_RUNTIME_VULKAN_MEMORY_H
2 #define HALIDE_RUNTIME_VULKAN_MEMORY_H
54 VkDevice dev, VkPhysicalDevice phys_dev,
66 bool collect(
void *user_context);
67 int release(
void *user_context);
68 int destroy(
void *user_context);
80 return this->physical_device;
83 return this->alloc_callbacks;
108 VkDevice dev, VkPhysicalDevice phys_dev,
114 uint32_t select_memory_type(
void *user_context,
115 VkPhysicalDevice physical_device,
121 size_t block_byte_count = 0;
122 size_t block_count = 0;
123 size_t region_byte_count = 0;
124 size_t region_count = 0;
125 void *owner_context =
nullptr;
127 VkDevice device =
nullptr;
128 VkPhysicalDevice physical_device =
nullptr;
139 if (system_allocator.
allocate ==
nullptr) {
140 error(user_context) <<
"VulkanBlockAllocator: Unable to create instance! Missing system allocator interface!\n";
147 if (result ==
nullptr) {
148 error(user_context) <<
"VulkanMemoryAllocator: Failed to create instance! Out of memory!\n";
152 result->initialize(user_context, cfg, dev, phys_dev, system_allocator, alloc_callbacks);
157 if (instance ==
nullptr) {
158 error(user_context) <<
"VulkanBlockAllocator: Unable to destroy instance! Invalide instance pointer!\n";
162 instance->
destroy(user_context);
165 error(user_context) <<
"VulkanBlockAllocator: Unable to destroy instance! Missing system allocator interface!\n";
172 int VulkanMemoryAllocator::initialize(
void *user_context,
177 owner_context = user_context;
180 physical_device = phys_dev;
183 region_byte_count = 0;
185 block_byte_count = 0;
187 allocators.
system = system_allocator;
197 if (block_allocator ==
nullptr) {
198 error(user_context) <<
"VulkanMemoryAllocator: Failed to create BlockAllocator! Out of memory?!\n";
211 #if defined(HL_VK_DEBUG_MEM)
212 debug(
nullptr) <<
"VulkanMemoryAllocator: Reserving memory ("
213 <<
"user_context=" << user_context <<
" "
214 <<
"block_allocator=" << (
void *)(block_allocator) <<
" "
216 <<
"device=" << (
void *)(device) <<
" "
217 <<
"physical_device=" << (
void *)(physical_device) <<
") ...\n";
220 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
221 error(user_context) <<
"VulkanMemoryAllocator: Unable to reserve memory! Invalid device handle!\n";
225 if (block_allocator ==
nullptr) {
226 error(user_context) <<
"VulkanMemoryAllocator: Unable to reserve memory! Invalid block allocator!\n";
230 return block_allocator->
reserve(
this, request);
234 #if defined(HL_VK_DEBUG_MEM)
235 debug(
nullptr) <<
"VulkanMemoryAllocator: Mapping region ("
236 <<
"user_context=" << user_context <<
" "
237 <<
"device=" << (
void *)(device) <<
" "
238 <<
"physical_device=" << (
void *)(physical_device) <<
" "
239 <<
"region=" << (
void *)(region) <<
" "
244 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
245 error(user_context) <<
"VulkanMemoryAllocator: Unable to map memory! Invalid device handle!\n";
249 if (block_allocator ==
nullptr) {
250 error(user_context) <<
"VulkanMemoryAllocator: Unable to map memory! Invalid block allocator!\n";
256 if (region_allocator ==
nullptr) {
257 error(user_context) <<
"VulkanMemoryAllocator: Unable to map region! Invalid region allocator handle!\n";
262 if (block_resource ==
nullptr) {
263 error(user_context) <<
"VulkanMemoryAllocator: Unable to map region! Invalid block resource handle!\n";
267 VkDeviceMemory *device_memory =
reinterpret_cast<VkDeviceMemory *
>(block_resource->
memory.
handle);
268 if (device_memory ==
nullptr) {
269 error(user_context) <<
"VulkanMemoryAllocator: Unable to map region! Invalid device memory handle!\n";
273 void *mapped_ptr =
nullptr;
277 error(user_context) <<
"VulkanMemoryAllocator: Unable to map region! Invalid memory range !\n";
280 #if defined(HL_VK_DEBUG_MEM)
281 debug(
nullptr) <<
"VulkanMemoryAllocator: MapMemory ("
282 <<
"user_context=" << user_context <<
"\n"
287 <<
" memory_offset=" << (
uint32_t)memory_offset <<
"\n"
288 <<
" memory_size=" << (
uint32_t)memory_size <<
"\n)\n";
290 VkResult result =
vkMapMemory(device, *device_memory, memory_offset, memory_size, 0, (
void **)(&mapped_ptr));
292 error(user_context) <<
"VulkanMemoryAllocator: Mapping region failed! vkMapMemory returned error code: " << vk_get_error_name(result) <<
"\n";
300 #if defined(HL_VK_DEBUG_MEM)
301 debug(
nullptr) <<
"VulkanMemoryAllocator: Unmapping region ("
302 <<
"user_context=" << user_context <<
" "
303 <<
"device=" << (
void *)(device) <<
" "
304 <<
"physical_device=" << (
void *)(physical_device) <<
" "
305 <<
"region=" << (
void *)(region) <<
" "
310 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
311 error(user_context) <<
"VulkanMemoryAllocator: Unable to unmap region! Invalid device handle!\n";
317 if (region_allocator ==
nullptr) {
318 error(user_context) <<
"VulkanMemoryAllocator: Unable to unmap region! Invalid region allocator handle!\n";
323 if (block_resource ==
nullptr) {
324 error(user_context) <<
"VulkanMemoryAllocator: Unable to unmap region! Invalid block resource handle!\n";
328 VkDeviceMemory *device_memory =
reinterpret_cast<VkDeviceMemory *
>(block_resource->
memory.
handle);
329 if (device_memory ==
nullptr) {
330 error(user_context) <<
"VulkanMemoryAllocator: Unable to unmap region! Invalid device memory handle!\n";
339 #if defined(HL_VK_DEBUG_MEM)
340 debug(
nullptr) <<
"VulkanMemoryAllocator: Cropping region ("
341 <<
"user_context=" << user_context <<
" "
342 <<
"device=" << (
void *)(device) <<
" "
343 <<
"physical_device=" << (
void *)(physical_device) <<
" "
344 <<
"region=" << (
void *)(region) <<
" "
347 <<
"crop_offset=" << (
int64_t)offset <<
") ...\n";
349 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
350 error(user_context) <<
"VulkanMemoryAllocator: Unable to crop region! Invalid device handle!\n";
356 if (region_allocator ==
nullptr) {
357 error(user_context) <<
"VulkanMemoryAllocator: Unable to unmap region! Invalid region allocator handle!\n";
362 int error_code = region_allocator->
retain(
this, owner);
364 error(user_context) <<
"VulkanMemoryAllocator: Unable to crop region! Failed to retain memory region!\n";
371 error(user_context) <<
"VulkanMemoryAllocator: Unable to create crop! Missing system allocator interface!\n";
378 if (memory_region ==
nullptr) {
379 error(user_context) <<
"VulkanMemoryAllocator: Failed to allocate memory region! Out of memory!\n";
386 memory_region->
handle = (
void *)owner;
388 return memory_region;
392 if (region ==
nullptr) {
393 error(user_context) <<
"VulkanMemoryAllocator: Failed to destroy crop! Invalid memory region!\n";
399 if (region_allocator ==
nullptr) {
400 error(user_context) <<
"VulkanMemoryAllocator: Unable to destroy crop region! Invalid region allocator handle!\n";
405 int error_code = region_allocator->
release(
this, owner);
407 error(user_context) <<
"VulkanBlockAllocator: Unable to destroy crop region! Region allocator failed to release memory region!\n";
414 error(user_context) <<
"VulkanBlockAllocator: Unable to destroy crop region! Missing system allocator interface!\n";
431 #if defined(HL_VK_DEBUG_MEM)
432 debug(
nullptr) <<
"VulkanMemoryAllocator: Releasing region ("
433 <<
"user_context=" << user_context <<
" "
434 <<
"region=" << (
void *)(region) <<
" "
438 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
439 error(user_context) <<
"VulkanMemoryAllocator: Unable to release region! Invalid device handle!\n";
442 if (block_allocator ==
nullptr) {
443 error(user_context) <<
"VulkanMemoryAllocator: Unable to release region! Invalid block allocator!\n";
446 return block_allocator->
release(
this, region);
450 #if defined(HL_VK_DEBUG_MEM)
451 debug(
nullptr) <<
"VulkanMemoryAllocator: Reclaiming region ("
452 <<
"user_context=" << user_context <<
" "
453 <<
"region=" << (
void *)(region) <<
" "
457 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
458 error(user_context) <<
"VulkanMemoryAllocator: Unable to reclaim region! Invalid device handle!\n";
461 if (block_allocator ==
nullptr) {
462 error(user_context) <<
"VulkanMemoryAllocator: Unable to reclaim region! Invalid block allocator!\n";
465 return block_allocator->
reclaim(
this, region);
469 #if defined(HL_VK_DEBUG_MEM)
470 debug(
nullptr) <<
"VulkanMemoryAllocator: Retaining region ("
471 <<
"user_context=" << user_context <<
" "
472 <<
"region=" << (
void *)(region) <<
" "
476 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
477 error(user_context) <<
"VulkanMemoryAllocator: Unable to retain region! Invalid device handle!\n";
480 if (block_allocator ==
nullptr) {
481 error(user_context) <<
"VulkanMemoryAllocator: Unable to retain region! Invalid block allocator!\n";
484 return block_allocator->
retain(
this, region);
488 #if defined(HL_VK_DEBUG_MEM)
489 debug(
nullptr) <<
"VulkanMemoryAllocator: Collecting unused memory ("
490 <<
"user_context=" << user_context <<
") ... \n";
492 if ((device ==
nullptr) || (physical_device ==
nullptr) || (block_allocator ==
nullptr)) {
495 return block_allocator->
collect(
this);
499 #if defined(HL_VK_DEBUG_MEM)
500 debug(
nullptr) <<
"VulkanMemoryAllocator: Releasing block allocator ("
501 <<
"user_context=" << user_context <<
") ... \n";
503 if ((device ==
nullptr) || (physical_device ==
nullptr)) {
504 error(user_context) <<
"VulkanMemoryAllocator: Unable to release allocator! Invalid device handle!\n";
507 if (block_allocator ==
nullptr) {
508 error(user_context) <<
"VulkanMemoryAllocator: Unable to release allocator! Invalid block allocator!\n";
512 return block_allocator->
release(
this);
516 #if defined(HL_VK_DEBUG_MEM)
517 debug(
nullptr) <<
"VulkanMemoryAllocator: Destroying allocator ("
518 <<
"user_context=" << user_context <<
") ... \n";
520 if (block_allocator !=
nullptr) {
521 block_allocator->
destroy(
this);
524 region_byte_count = 0;
526 block_byte_count = 0;
537 int VulkanMemoryAllocator::lookup_requirements(
void *user_context,
size_t size,
uint32_t usage_flags,
VkMemoryRequirements *memory_requirements) {
538 #if defined(HL_VK_DEBUG_MEM)
539 debug(
nullptr) <<
"VulkanMemoryAllocator: Looking up requirements ("
540 <<
"user_context=" << user_context <<
" "
541 <<
"size=" << (
uint32_t)block->size <<
", "
542 <<
"usage_flags=" << usage_flags <<
") ... \n";
554 VkBuffer buffer = {0};
557 #if defined(HL_VK_DEBUG_MEM)
558 debug(
nullptr) <<
"VulkanMemoryAllocator: Failed to create buffer to find requirements!\n\t"
559 <<
"vkCreateBuffer returned: " << vk_get_error_name(result) <<
"\n";
569 int VulkanMemoryAllocator::conform_block_request(
void *instance_ptr,
MemoryRequest *request) {
572 if (instance ==
nullptr) {
576 void *user_context = instance->owner_context;
577 #if defined(HL_VK_DEBUG_MEM)
578 debug(
nullptr) <<
"VulkanMemoryAllocator: Conforming block request ("
579 <<
"user_context=" << user_context <<
" "
580 <<
"request=" << (
void *)(request) <<
") ... \n";
583 if ((instance->device ==
nullptr) || (instance->physical_device ==
nullptr)) {
584 error(user_context) <<
"VulkanRegionAllocator: Unable to conform block request! Invalid device handle!\n";
589 uint32_t usage_flags = instance->select_memory_usage(user_context, request->
properties);
590 int error_code = instance->lookup_requirements(user_context, request->
size, usage_flags, &memory_requirements);
592 error(user_context) <<
"VulkanRegionAllocator: Failed to conform block request! Unable to lookup requirements!\n";
596 #if defined(HL_VK_DEBUG_MEM)
597 debug(
nullptr) <<
"VulkanMemoryAllocator: Block allocated ("
600 <<
"required_size=" << (
uint32_t)memory_requirements.
size <<
", "
603 <<
"dedicated=" << (request->
dedicated ?
"true" :
"false") <<
")\n";
606 request->
size = memory_requirements.
size;
611 int VulkanMemoryAllocator::allocate_block(
void *instance_ptr,
MemoryBlock *block) {
613 if (instance ==
nullptr) {
617 void *user_context = instance->owner_context;
618 if ((instance->device ==
nullptr) || (instance->physical_device ==
nullptr)) {
619 error(user_context) <<
"VulkanBlockAllocator: Unable to deallocate block! Invalid device handle!\n";
623 if (block ==
nullptr) {
624 error(user_context) <<
"VulkanBlockAllocator: Unable to deallocate block! Invalid pointer!\n";
628 #if defined(HL_VK_DEBUG_MEM)
629 debug(
nullptr) <<
"VulkanMemoryAllocator: Allocating block ("
630 <<
"user_context=" << user_context <<
" "
631 <<
"block=" << (
void *)(block) <<
" "
633 <<
"dedicated=" << (block->
dedicated ?
"true" :
"false") <<
" "
640 uint32_t memory_type = instance->select_memory_type(user_context, instance->physical_device, block->
properties, 0);
641 if (memory_type == invalid_memory_type) {
642 error(user_context) <<
"VulkanMemoryAllocator: Unable to find appropriate memory type for device!\n";
654 VkDeviceMemory *device_memory = (VkDeviceMemory *)vk_host_malloc(
nullptr,
sizeof(VkDeviceMemory), 0,
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT, instance->alloc_callbacks);
655 if (device_memory ==
nullptr) {
656 debug(
nullptr) <<
"VulkanBlockAllocator: Unable to allocate block! Failed to allocate device memory handle!\n";
662 debug(
nullptr) <<
"VulkanMemoryAllocator: Allocation failed! vkAllocateMemory returned: " << vk_get_error_name(result) <<
"\n";
666 debug(
nullptr) <<
"vkAllocateMemory: Allocated memory for device region (" << (
uint64_t)block->
size <<
" bytes) ...\n";
669 block->
handle = (
void *)device_memory;
670 instance->block_byte_count += block->
size;
671 instance->block_count++;
675 int VulkanMemoryAllocator::deallocate_block(
void *instance_ptr,
MemoryBlock *block) {
677 if (instance ==
nullptr) {
681 void *user_context = instance->owner_context;
682 #if defined(HL_VK_DEBUG_MEM)
683 debug(
nullptr) <<
"VulkanMemoryAllocator: Deallocating block ("
684 <<
"user_context=" << user_context <<
" "
685 <<
"block=" << (
void *)(block) <<
") ... \n";
688 if ((instance->device ==
nullptr) || (instance->physical_device ==
nullptr)) {
689 error(user_context) <<
"VulkanBlockAllocator: Unable to deallocate block! Invalid device handle!\n";
693 if (block ==
nullptr) {
694 error(user_context) <<
"VulkanBlockAllocator: Unable to deallocate block! Invalid pointer!\n";
698 #if defined(HL_VK_DEBUG_MEM)
699 debug(
nullptr) <<
"VulkanBlockAllocator: deallocating block ("
701 <<
"dedicated=" << (block->
dedicated ?
"true" :
"false") <<
" "
707 if (block->
handle ==
nullptr) {
708 error(user_context) <<
"VulkanBlockAllocator: Unable to deallocate block! Invalid handle!\n";
712 VkDeviceMemory *device_memory =
reinterpret_cast<VkDeviceMemory *
>(block->
handle);
713 if (device_memory ==
nullptr) {
714 error(user_context) <<
"VulkanBlockAllocator: Unable to deallocate block! Invalid device memory handle!\n";
718 vkFreeMemory(instance->device, *device_memory, instance->alloc_callbacks);
720 debug(
nullptr) <<
"vkFreeMemory: Deallocated memory for device region (" << (
uint64_t)block->
size <<
" bytes) ...\n";
723 if (instance->block_count > 0) {
724 instance->block_count--;
726 error(
nullptr) <<
"VulkanRegionAllocator: Block counter invalid ... reseting to zero!\n";
727 instance->block_count = 0;
731 instance->block_byte_count -= block->
size;
733 error(
nullptr) <<
"VulkanRegionAllocator: Block byte counter invalid ... reseting to zero!\n";
734 instance->block_byte_count = 0;
738 vk_host_free(
nullptr, device_memory, instance->alloc_callbacks);
739 device_memory =
nullptr;
743 size_t VulkanMemoryAllocator::blocks_allocated()
const {
747 size_t VulkanMemoryAllocator::bytes_allocated_for_blocks()
const {
748 return block_byte_count;
751 uint32_t VulkanMemoryAllocator::select_memory_type(
void *user_context,
752 VkPhysicalDevice physical_device,
775 error(
nullptr) <<
"VulkanMemoryAllocator: Unable to convert type! Invalid memory visibility request!\n\t"
777 return invalid_memory_type;
801 error(user_context) <<
"VulkanMemoryAllocator: Unable to convert type! Invalid memory caching request!\n\t"
803 return invalid_memory_type;
809 uint32_t result = invalid_memory_type;
813 if (required_flags) {
814 if (((required_flags >> i) & 1) == 0) {
821 if ((properties & need_flags) != need_flags) {
827 if ((properties & want_flags) != want_flags) {
836 if (result == invalid_memory_type) {
837 error(user_context) <<
"VulkanBlockAllocator: Failed to find appropriate memory type for given properties:\n\t"
841 return invalid_memory_type;
849 int VulkanMemoryAllocator::conform(
void *user_context,
MemoryRequest *request) {
859 int error_code = lookup_requirements(user_context, request->
size, usage_flags, &memory_requirements);
861 error(user_context) <<
"VulkanRegionAllocator: Failed to conform block request! Unable to lookup requirements!\n";
865 #if defined(HL_VK_DEBUG_MEM)
866 debug(
nullptr) <<
"VulkanMemoryAllocator: Buffer requirements ("
867 <<
"requested_size=" << (
uint32_t)region->size <<
", "
869 <<
"required_size=" << (
uint32_t)memory_requirements.
size <<
")\n";
874 if ((request->
alignment % this->physical_device_limits.minStorageBufferOffsetAlignment) != 0) {
875 request->
alignment = this->physical_device_limits.minStorageBufferOffsetAlignment;
878 if ((request->
alignment % this->physical_device_limits.minUniformBufferOffsetAlignment) != 0) {
879 request->
alignment = this->physical_device_limits.minUniformBufferOffsetAlignment;
884 if (request->
alignment > config.nearest_multiple) {
892 #if defined(HL_VK_DEBUG_MEM)
893 if ((request->
size != actual_size) || (request->
alignment != actual_alignment) || (request->
offset != actual_offset)) {
894 debug(
nullptr) <<
"VulkanMemoryAllocator: Adjusting request to match requirements (\n"
898 <<
" required.size = " << (
uint64_t)memory_requirements.
size <<
",\n"
899 <<
" required.alignment = " << (
uint64_t)memory_requirements.
alignment <<
"\n)\n";
902 request->
size = actual_size;
904 request->
offset = actual_offset;
909 int VulkanMemoryAllocator::conform_region_request(
void *instance_ptr,
MemoryRequest *request) {
912 if (instance ==
nullptr) {
916 void *user_context = instance->owner_context;
917 #if defined(HL_VK_DEBUG_MEM)
918 debug(
nullptr) <<
"VulkanMemoryAllocator: Conforming region request ("
919 <<
"user_context=" << user_context <<
" "
920 <<
"request=" << (
void *)(region) <<
") ... \n";
923 if ((instance->device ==
nullptr) || (instance->physical_device ==
nullptr)) {
924 error(user_context) <<
"VulkanRegionAllocator: Unable to conform region request! Invalid device handle!\n";
928 #if defined(HL_VK_DEBUG_MEM)
929 debug(
nullptr) <<
"VulkanRegionAllocator: Conforming region request ("
932 <<
"dedicated=" << (request->
dedicated ?
"true" :
"false") <<
" "
938 return instance->
conform(user_context, request);
941 int VulkanMemoryAllocator::allocate_region(
void *instance_ptr,
MemoryRegion *region) {
944 if (instance ==
nullptr) {
948 void *user_context = instance->owner_context;
949 #if defined(HL_VK_DEBUG_MEM)
950 debug(
nullptr) <<
"VulkanMemoryAllocator: Allocating region ("
951 <<
"user_context=" << user_context <<
" "
952 <<
"region=" << (
void *)(region) <<
") ... \n";
955 if ((instance->device ==
nullptr) || (instance->physical_device ==
nullptr)) {
956 error(user_context) <<
"VulkanRegionAllocator: Unable to allocate region! Invalid device handle!\n";
960 if (region ==
nullptr) {
961 error(user_context) <<
"VulkanRegionAllocator: Unable to allocate region! Invalid pointer!\n";
965 #if defined(HL_VK_DEBUG_MEM)
966 debug(
nullptr) <<
"VulkanRegionAllocator: Allocating region ("
969 <<
"dedicated=" << (region->
dedicated ?
"true" :
"false") <<
" "
975 uint32_t usage_flags = instance->select_memory_usage(user_context, region->
properties);
987 if (buffer ==
nullptr) {
988 error(user_context) <<
"VulkanRegionAllocator: Unable to allocate region! Failed to allocate buffer handle!\n";
994 error(user_context) <<
"VulkanRegionAllocator: Failed to create buffer!\n\t"
995 <<
"vkCreateBuffer returned: " << vk_get_error_name(result) <<
"\n";
1007 #if defined(HL_VK_DEBUG_MEM)
1008 debug(
nullptr) <<
"VulkanMemoryAllocator: Buffer requirements ("
1011 <<
"required_size=" << (
uint32_t)memory_requirements.
size <<
")\n";
1014 if (memory_requirements.
size > region->
size) {
1015 vkDestroyBuffer(instance->device, *buffer, instance->alloc_callbacks);
1016 #ifdef DEBUG_RUNTIME
1017 debug(
nullptr) <<
"VulkanMemoryAllocator: Reallocating buffer to match required size ("
1020 create_info.
size = memory_requirements.
size;
1023 error(user_context) <<
"VulkanRegionAllocator: Failed to recreate buffer!\n\t"
1024 <<
"vkCreateBuffer returned: " << vk_get_error_name(result) <<
"\n";
1029 #ifdef DEBUG_RUNTIME
1030 debug(
nullptr) <<
"vkCreateBuffer: Created buffer for device region (" << (
uint64_t)region->
size <<
" bytes) ...\n";
1034 if (region_allocator ==
nullptr) {
1035 error(user_context) <<
"VulkanBlockAllocator: Unable to allocate region! Invalid region allocator!\n";
1039 BlockResource *block_resource = region_allocator->block_resource();
1040 if (block_resource ==
nullptr) {
1041 error(user_context) <<
"VulkanBlockAllocator: Unable to allocate region! Invalid block resource handle!\n";
1045 VkDeviceMemory *device_memory =
reinterpret_cast<VkDeviceMemory *
>(block_resource->
memory.
handle);
1046 if (device_memory ==
nullptr) {
1047 error(user_context) <<
"VulkanBlockAllocator: Unable to allocate region! Invalid device memory handle!\n";
1054 error(user_context) <<
"VulkanRegionAllocator: Failed to bind buffer!\n\t"
1055 <<
"vkBindBufferMemory returned: " << vk_get_error_name(result) <<
"\n";
1059 region->
handle = (
void *)buffer;
1061 instance->region_byte_count += region->
size;
1062 instance->region_count++;
1066 int VulkanMemoryAllocator::deallocate_region(
void *instance_ptr,
MemoryRegion *region) {
1068 if (instance ==
nullptr) {
1072 void *user_context = instance->owner_context;
1073 #if defined(HL_VK_DEBUG_MEM)
1074 debug(
nullptr) <<
"VulkanMemoryAllocator: Deallocating region ("
1075 <<
"user_context=" << user_context <<
" "
1076 <<
"region=" << (
void *)(region) <<
") ... \n";
1079 if ((instance->device ==
nullptr) || (instance->physical_device ==
nullptr)) {
1080 error(user_context) <<
"VulkanRegionAllocator: Unable to deallocate region! Invalid device handle!\n";
1084 if (region ==
nullptr) {
1085 error(user_context) <<
"VulkanRegionAllocator: Unable to deallocate region! Invalid pointer!\n";
1089 #if defined(HL_VK_DEBUG_MEM)
1090 debug(
nullptr) <<
"VulkanRegionAllocator: Deallocating region ("
1093 <<
"dedicated=" << (region->
dedicated ?
"true" :
"false") <<
" "
1099 if (region->
handle ==
nullptr) {
1100 error(user_context) <<
"VulkanRegionAllocator: Unable to deallocate region! Invalid handle!\n";
1104 VkBuffer *buffer =
reinterpret_cast<VkBuffer *
>(region->
handle);
1105 if (buffer ==
nullptr) {
1106 error(user_context) <<
"VulkanRegionAllocator: Unable to deallocate region! Invalid buffer handle!\n";
1110 vkDestroyBuffer(instance->device, *buffer, instance->alloc_callbacks);
1111 #ifdef DEBUG_RUNTIME
1112 debug(
nullptr) <<
"vkDestroyBuffer: Destroyed buffer for device region (" << (
uint64_t)region->
size <<
" bytes) ...\n";
1114 region->
handle =
nullptr;
1115 if (instance->region_count > 0) {
1116 instance->region_count--;
1118 error(
nullptr) <<
"VulkanRegionAllocator: Region counter invalid ... reseting to zero!\n";
1119 instance->region_count = 0;
1124 instance->region_byte_count -= region->
size;
1126 error(
nullptr) <<
"VulkanRegionAllocator: Region byte counter invalid ... reseting to zero!\n";
1127 instance->region_byte_count = 0;
1130 vk_host_free(
nullptr, buffer, instance->alloc_callbacks);
1135 size_t VulkanMemoryAllocator::regions_allocated()
const {
1136 return region_count;
1139 size_t VulkanMemoryAllocator::bytes_allocated_for_regions()
const {
1140 return region_byte_count;
1145 switch (properties.
usage) {
1165 error(user_context) <<
"VulkanRegionAllocator: Unable to convert type! Invalid memory usage request!\n\t"
1167 return invalid_usage_flags;
1170 if (result == invalid_usage_flags) {
1171 error(user_context) <<
"VulkanRegionAllocator: Failed to find appropriate memory usage for given properties:\n\t"
1175 return invalid_usage_flags;
1187 void *vk_system_malloc(
void *user_context,
size_t size) {
1191 void vk_system_free(
void *user_context,
void *ptr) {
1198 return callbacks->
pfnAllocation(user_context, size, alignment, scope);
1200 return vk_system_malloc(user_context, size);
1206 return callbacks->
pfnFree(user_context, ptr);
1208 return vk_system_free(user_context, ptr);
1212 VulkanMemoryAllocator *vk_create_memory_allocator(
void *user_context,
1214 VkPhysicalDevice physical_device,
1227 const char *
alloc_config = vk_get_alloc_config_internal(user_context);
1231 if (alloc_config_values.
size() > 0) {
1232 config.maximum_pool_size =
atoi(alloc_config_values[0]) * 1024 * 1024;
1233 print(user_context) <<
"Vulkan: Configuring allocator with " << (
uint32_t)config.maximum_pool_size <<
" for maximum pool size (in bytes)\n";
1235 if (alloc_config_values.
size() > 1) {
1236 config.minimum_block_size =
atoi(alloc_config_values[1]) * 1024 * 1024;
1237 print(user_context) <<
"Vulkan: Configuring allocator with " << (
uint32_t)config.minimum_block_size <<
" for minimum block size (in bytes)\n";
1239 if (alloc_config_values.
size() > 2) {
1240 config.maximum_block_size =
atoi(alloc_config_values[2]) * 1024 * 1024;
1241 print(user_context) <<
"Vulkan: Configuring allocator with " << (
uint32_t)config.maximum_block_size <<
" for maximum block size (in bytes)\n";
1243 if (alloc_config_values.
size() > 3) {
1244 config.maximum_block_count =
atoi(alloc_config_values[3]);
1245 print(user_context) <<
"Vulkan: Configuring allocator with " << (
uint32_t)config.maximum_block_count <<
" for maximum block count\n";
1247 if (alloc_config_values.
size() > 4) {
1248 config.nearest_multiple =
atoi(alloc_config_values[4]);
1249 print(user_context) <<
"Vulkan: Configuring allocator with " << (
uint32_t)config.nearest_multiple <<
" for nearest multiple\n";
1253 return VulkanMemoryAllocator::create(user_context,
1254 config, device, physical_device,
1255 system_allocator, alloc_callbacks);
1258 int vk_destroy_memory_allocator(
void *user_context, VulkanMemoryAllocator *allocator) {
1259 if (allocator !=
nullptr) {
1261 allocator =
nullptr;
1268 int vk_clear_device_buffer(
void *user_context,
1269 VulkanMemoryAllocator *allocator,
1270 VkCommandPool command_pool,
1271 VkQueue command_queue,
1272 VkBuffer device_buffer) {
1274 #ifdef DEBUG_RUNTIME
1276 <<
" vk_clear_device_buffer (user_context: " << user_context <<
", "
1277 <<
"allocator: " << (
void *)allocator <<
", "
1278 <<
"command_pool: " << (
void *)command_pool <<
", "
1279 <<
"command_queue: " << (
void *)command_queue <<
", "
1280 <<
"device_buffer: " << (
void *)device_buffer <<
")\n";
1284 VkCommandBuffer command_buffer;
1285 int error_code = vk_create_command_buffer(user_context, allocator, command_pool, &command_buffer);
1287 error(user_context) <<
"Vulkan: Failed to create command buffer!\n";
1302 error(user_context) <<
"Vulkan: vkBeginCommandBuffer returned " << vk_get_error_name(result) <<
"\n";
1312 error(user_context) <<
"Vulkan: vkEndCommandBuffer returned " << vk_get_error_name(result) <<
"\n";
1332 error(user_context) <<
"Vulkan: vkQueueSubmit returned " << vk_get_error_name(result) <<
"\n";
1339 error(user_context) <<
"Vulkan: vkQueueWaitIdle returned " << vk_get_error_name(result) <<
"\n";
1343 error_code = vk_destroy_command_buffer(user_context, allocator, command_pool, command_buffer);
1345 error(user_context) <<
"Vulkan: Failed to destroy command buffer!\n";
@ halide_error_code_internal_error
There is a bug in the Halide compiler.
@ halide_error_code_generic_error
An uncategorized error occurred.
@ halide_error_code_success
There was no error.
@ halide_error_code_device_malloc_failed
The Halide runtime encountered an error while trying to allocate memory on device.
@ halide_error_code_out_of_memory
A call to halide_malloc returned NULL.
Allocator class interface for managing large contiguous blocks of memory, which are then sub-allocate...
bool collect(void *user_context)
MemoryRegion * reserve(void *user_context, const MemoryRequest &request)
int release(void *user_context, MemoryRegion *region)
int retain(void *user_context, MemoryRegion *region)
static void destroy(void *user_context, BlockAllocator *block_allocator)
static BlockAllocator * create(void *user_context, const Config &config, const MemoryAllocators &allocators)
int reclaim(void *user_context, MemoryRegion *region)
const MemoryAllocators & current_allocators() const
Allocator class interface for sub-allocating a contiguous memory block into smaller regions of memory...
static RegionAllocator * find_allocator(void *user_context, MemoryRegion *memory_region)
int retain(void *user_context, MemoryRegion *memory_region)
int release(void *user_context, MemoryRegion *memory_region)
BlockResource * block_resource() const
size_t parse(void *user_context, const char *str, const char *delim)
Vulkan Memory Allocator class interface for managing large memory requests stored as contiguous block...
int reclaim(void *user_context, MemoryRegion *region)
static const VulkanMemoryConfig & default_config()
MemoryRegion * reserve(void *user_context, const MemoryRequest &request)
VulkanMemoryAllocator(const VulkanMemoryAllocator &)=delete
int release(void *user_context, MemoryRegion *region)
static int allocate_block(void *instance_ptr, MemoryBlock *block)
int conform(void *user_context, MemoryRequest *request)
static int deallocate_region(void *instance_ptr, MemoryRegion *region)
static int destroy(void *user_context, VulkanMemoryAllocator *allocator)
int unmap(void *user_context, MemoryRegion *region)
const VkAllocationCallbacks * callbacks() const
static int conform_region_request(void *instance_ptr, MemoryRequest *request)
~VulkanMemoryAllocator()=delete
MemoryRegion * create_crop(void *user_context, MemoryRegion *region, uint64_t offset)
int destroy_crop(void *user_context, MemoryRegion *region)
size_t regions_allocated() const
size_t blocks_allocated() const
static int allocate_region(void *instance_ptr, MemoryRegion *region)
static int conform_block_request(void *instance_ptr, MemoryRequest *request)
MemoryRegion * owner_of(void *user_context, MemoryRegion *region)
VkDevice current_device() const
bool collect(void *user_context)
size_t bytes_allocated_for_blocks() const
static int deallocate_block(void *instance_ptr, MemoryBlock *block)
VulkanMemoryAllocator()=delete
void * map(void *user_context, MemoryRegion *region)
VulkanMemoryAllocator & operator=(const VulkanMemoryAllocator &)=delete
VkPhysicalDevice current_physical_device() const
size_t bytes_allocated_for_regions() const
int retain(void *user_context, MemoryRegion *region)
static VulkanMemoryAllocator * create(void *user_context, const VulkanMemoryConfig &config, VkDevice dev, VkPhysicalDevice phys_dev, const SystemMemoryAllocatorFns &system_allocator, const VkAllocationCallbacks *alloc_callbacks=nullptr)
WEAK const char * halide_memory_usage_name(MemoryUsage value)
WEAK const char * halide_memory_visibility_name(MemoryVisibility value)
WEAK const char * halide_memory_caching_name(MemoryCaching value)
VKAPI_ATTR VkResult VKAPI_CALL vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset)
VkFlags VkMemoryPropertyFlags
VKAPI_ATTR void VKAPI_CALL vkCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size, uint32_t data)
VKAPI_ATTR VkResult VKAPI_CALL vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory)
#define VK_MAX_MEMORY_TYPES
@ VK_SHARING_MODE_EXCLUSIVE
@ VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
@ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
@ VK_MEMORY_PROPERTY_HOST_CACHED_BIT
@ VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
VKAPI_ATTR void VKAPI_CALL vkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements *pMemoryRequirements)
@ VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
VKAPI_ATTR VkResult VKAPI_CALL vkQueueWaitIdle(VkQueue queue)
VKAPI_ATTR void VKAPI_CALL vkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *pAllocator)
@ VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties)
VKAPI_ATTR void VKAPI_CALL vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties *pMemoryProperties)
VKAPI_ATTR VkResult VKAPI_CALL vkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer)
VKAPI_ATTR VkResult VKAPI_CALL vkQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence)
VKAPI_ATTR VkResult VKAPI_CALL vkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void **ppData)
@ VK_BUFFER_USAGE_TRANSFER_DST_BIT
@ VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
@ VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
@ VK_BUFFER_USAGE_TRANSFER_SRC_BIT
VKAPI_ATTR void VKAPI_CALL vkUnmapMemory(VkDevice device, VkDeviceMemory memory)
VKAPI_ATTR VkResult VKAPI_CALL vkEndCommandBuffer(VkCommandBuffer commandBuffer)
VKAPI_ATTR void VKAPI_CALL vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator)
@ VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
@ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO
@ VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
@ VK_STRUCTURE_TYPE_SUBMIT_INFO
VKAPI_ATTR VkResult VKAPI_CALL vkBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo)
WEAK VulkanMemoryConfig memory_allocator_config
WEAK const VkAllocationCallbacks * custom_allocation_callbacks
WEAK ScopedSpinLock::AtomicFlag custom_allocation_callbacks_lock
WEAK char alloc_config[1024]
ALWAYS_INLINE size_t conform_alignment(size_t requested, size_t required)
ALWAYS_INLINE size_t conform_size(size_t offset, size_t size, size_t alignment, size_t nearest_multiple)
ALWAYS_INLINE size_t aligned_offset(size_t offset, size_t alignment)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
void * memset(void *s, int val, size_t n)
unsigned __INT32_TYPE__ uint32_t
void * memcpy(void *s1, const void *s2, size_t n)
size_t maximum_block_count
size_t minimum_block_size
size_t maximum_block_size
MemoryRegionAllocatorFns region
MemoryBlockAllocatorFns block
SystemMemoryAllocatorFns system
MemoryProperties properties
MemoryVisibility visibility
MemoryProperties properties
MemoryProperties properties
static bool is_empty(const char *str)
DeallocateSystemFn deallocate
AllocateSystemFn allocate
size_t maximum_block_size
size_t minimum_block_size
size_t maximum_block_count
PFN_vkAllocationFunction pfnAllocation
PFN_vkFreeFunction pfnFree
VkMemoryPropertyFlags propertyFlags
VkDeviceSize minStorageBufferOffsetAlignment
VkDeviceSize minUniformBufferOffsetAlignment
VkMemoryType memoryTypes[VK_MAX_MEMORY_TYPES]
VkPhysicalDeviceLimits limits
WEAK void halide_vulkan_set_allocation_callbacks(const VkAllocationCallbacks *callbacks)
WEAK const VkAllocationCallbacks * halide_vulkan_get_allocation_callbacks(void *user_context)