From ba70017f68262ef9fa9877e2b8ecae591116d72e Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Mon, 30 Jan 2023 20:11:54 -0600 Subject: [PATCH] nvk: Add internal helpers for device memory allocation Part-of: --- src/nouveau/vulkan/nvk_device_memory.c | 65 ++++++++++++++++++-------- src/nouveau/vulkan/nvk_device_memory.h | 11 +++++ 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/nouveau/vulkan/nvk_device_memory.c b/src/nouveau/vulkan/nvk_device_memory.c index 184031d4996..28c8f7382f6 100644 --- a/src/nouveau/vulkan/nvk_device_memory.c +++ b/src/nouveau/vulkan/nvk_device_memory.c @@ -69,14 +69,12 @@ zero_vram(struct nvk_device *dev, struct nouveau_ws_bo *bo) return VK_SUCCESS; } -VKAPI_ATTR VkResult VKAPI_CALL -nvk_AllocateMemory( - VkDevice _device, - const VkMemoryAllocateInfo *pAllocateInfo, - const VkAllocationCallbacks *pAllocator, - VkDeviceMemory *pMem) +VkResult +nvk_allocate_memory(struct nvk_device *device, + const VkMemoryAllocateInfo *pAllocateInfo, + const VkAllocationCallbacks *pAllocator, + struct nvk_device_memory **mem_out) { - VK_FROM_HANDLE(nvk_device, device, _device); VkMemoryType *type = &device->pdev->mem_types[pAllocateInfo->memoryTypeIndex]; struct nvk_device_memory *mem; @@ -122,7 +120,7 @@ nvk_AllocateMemory( list_addtail(&mem->link, &device->memory_objects); simple_mtx_unlock(&device->memory_objects_lock); - *pMem = nvk_device_memory_to_handle(mem); + *mem_out = mem; return VK_SUCCESS; @@ -132,20 +130,13 @@ fail_bo: return result; } -VKAPI_ATTR void VKAPI_CALL -nvk_FreeMemory( - VkDevice _device, - VkDeviceMemory _mem, - const VkAllocationCallbacks *pAllocator) +void +nvk_free_memory(struct nvk_device *device, + struct nvk_device_memory *mem, + const VkAllocationCallbacks *pAllocator) { - VK_FROM_HANDLE(nvk_device, device, _device); - VK_FROM_HANDLE(nvk_device_memory, mem, _mem); - - if (!mem) - return; - if (mem->map) - nvk_UnmapMemory(_device, _mem); + munmap(mem->map, mem->bo->size); simple_mtx_lock(&device->memory_objects_lock); list_del(&mem->link); @@ -156,6 +147,40 @@ nvk_FreeMemory( vk_object_free(&device->vk, pAllocator, mem); } +VKAPI_ATTR VkResult VKAPI_CALL +nvk_AllocateMemory( + VkDevice _device, + const VkMemoryAllocateInfo *pAllocateInfo, + const VkAllocationCallbacks *pAllocator, + VkDeviceMemory *pMem) +{ + VK_FROM_HANDLE(nvk_device, device, _device); + struct nvk_device_memory *mem; + VkResult result; + + result = nvk_allocate_memory(device, pAllocateInfo, pAllocator, &mem); + if (result != VK_SUCCESS) + return result; + + *pMem = nvk_device_memory_to_handle(mem); + + return VK_SUCCESS; +} + +VKAPI_ATTR void VKAPI_CALL +nvk_FreeMemory(VkDevice _device, + VkDeviceMemory _mem, + const VkAllocationCallbacks *pAllocator) +{ + VK_FROM_HANDLE(nvk_device, device, _device); + VK_FROM_HANDLE(nvk_device_memory, mem, _mem); + + if (!mem) + return; + + nvk_free_memory(device, mem, pAllocator); +} + VKAPI_ATTR VkResult VKAPI_CALL nvk_MapMemory( VkDevice _device, diff --git a/src/nouveau/vulkan/nvk_device_memory.h b/src/nouveau/vulkan/nvk_device_memory.h index d24947ee1af..0a5b6d65451 100644 --- a/src/nouveau/vulkan/nvk_device_memory.h +++ b/src/nouveau/vulkan/nvk_device_memory.h @@ -5,6 +5,8 @@ #include "util/list.h" +struct nvk_device; + struct nvk_device_memory { struct vk_object_base base; @@ -17,4 +19,13 @@ struct nvk_device_memory { VK_DEFINE_HANDLE_CASTS(nvk_device_memory, base, VkDeviceMemory, VK_OBJECT_TYPE_DEVICE_MEMORY) +VkResult nvk_allocate_memory(struct nvk_device *device, + const VkMemoryAllocateInfo *pAllocateInfo, + const VkAllocationCallbacks *pAllocator, + struct nvk_device_memory **mem_out); + +void nvk_free_memory(struct nvk_device *device, + struct nvk_device_memory *mem, + const VkAllocationCallbacks *pAllocator); + #endif