X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fvulkan%2Fallocator.cc;h=ba1b47be1909122a194565ed497a94a8e395d421;hp=5228a722308351e27e8a0e1660baae737cb9e6ac;hb=c3d7c1b7f8a7f4bcfc7661b6baf166ce0648083a;hpb=8fb0cca684e078cd2537070c53ad970ebbc2e9a7 diff --git a/src/vulkan/allocator.cc b/src/vulkan/allocator.cc index 5228a72..ba1b47b 100644 --- a/src/vulkan/allocator.cc +++ b/src/vulkan/allocator.cc @@ -5,7 +5,7 @@ #include "vk.h" #include "vkutil.h" -VkDeviceMemory vk_allocate(int size) +bool vku_allocate(int size, DevMemBlock *block) { VkDeviceMemory gpu_mem; @@ -15,9 +15,33 @@ VkDeviceMemory vk_allocate(int size) gpu_alloc_inf.allocationSize = size; if(vkAllocateMemory(vk_device, &gpu_alloc_inf, 0, &gpu_mem) != VK_SUCCESS) { - fprintf(stderr, "Failed to allocate device memory, mem size: %d\n"); - return 0; + fprintf(stderr, "Failed to allocate device memory, mem size: %d\n", size); + return false; } - return gpu_mem; + block->dev_mem = gpu_mem; + block->offset = 0; + block->size = size; + + return true; +} + +void vku_free(VkDeviceMemory gpu_memory) +{ + vkFreeMemory(vk_device, gpu_memory, 0); +} + +bool vku_write_memory(VkDeviceMemory gpu_mem, int size, void *data) +{ + uint8_t *pdata; + VkResult res = vkMapMemory(vk_device, gpu_mem, 0, size, 0, (void**)&pdata); + if(res != VK_SUCCESS) { + fprintf(stderr, "Failed to map memory to write data.\n"); + return false; + } + + memcpy(pdata, data, size); + vkUnmapMemory(vk_device, gpu_mem); + + return true; }