X-Git-Url: https://eleni.mutantstargoat.com/git/?a=blobdiff_plain;f=src%2Fvulkan%2Fvkutil.cc;h=c1ca1d56d856e6e2bef5ae63660a777683c9d3da;hb=86c912d603be75ac8b2fdb2229f1696e9c0c01d9;hp=051ee4dc7174b101c6301ba2924046ec7a55f34e;hpb=9148ff7614b1dc22fd5d1dcf6d74e2fb4ff13706;p=demo diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc index 051ee4d..c1ca1d5 100644 --- a/src/vulkan/vkutil.cc +++ b/src/vulkan/vkutil.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,21 +7,19 @@ #include #include +#include "allocator.h" #include "vkutil.h" /* global variables */ - -VkSwapchainKHR vkswapchain; -VkImage *vkswapchain_images; -int vknext_swapchain_image; -VkSurfaceKHR vksurface; -VkInstance vkinst; -VkPhysicalDevice vkpdev; -VkDevice vkdev; -VkQueue vkq; -VkCommandPool vkcmdpool; -VkCommandBuffer vkcmdbuf; /* primary command buffer */ -int vkqfamily; +VkPhysicalDevice vk_physical; +VkDevice vk_device; +VkInstance vk_instance; +VkQueue vk_queue; +int vk_qfamily; +VkCommandPool vk_pool; +VkSurfaceKHR vk_surface; +VkSwapchainKHR vk_swapchain; +VkDescriptorPool vk_dpool; /* static functions */ static const char *get_device_name_str(VkPhysicalDeviceType type); @@ -46,10 +45,7 @@ bool vku_have_extension(const char *name) vkext_count = 0; vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0); if(vkext_count) { - if(!(vkext = (VkExtensionProperties *)malloc(vkext_count * sizeof *vkext))) { - perror("failed to allocate instance extension list"); - return false; - } + vkext = new VkExtensionProperties[vkext_count]; vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext); printf("instance extensions:\n"); @@ -75,10 +71,7 @@ bool vku_have_device_extension(const char *name) vkdevext_count = 0; vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0); if(vkdevext_count) { - if(!(vkdevext = (VkExtensionProperties *)malloc(vkdevext_count * sizeof *vkdevext))) { - perror("failed to allocate device extension list"); - return false; - } + vkdevext = new VkExtensionProperties[vkdevext_count]; vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext); printf("selected device extensions:\n"); @@ -128,17 +121,17 @@ bool vku_create_device() inst_info.ppEnabledExtensionNames = ext_names; inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names; - if(vkCreateInstance(&inst_info, 0, &vkinst) != 0) { + if(vkCreateInstance(&inst_info, 0, &vk_instance) != 0) { fprintf(stderr, "failed to create vulkan instance\n"); return false; } printf("created vulkan instance\n"); - if(vkEnumeratePhysicalDevices(vkinst, &num_devices, 0) != 0) { + if(vkEnumeratePhysicalDevices(vk_instance, &num_devices, 0) != 0) { fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; } - phys_devices = (VkPhysicalDevice *)malloc(num_devices * sizeof *phys_devices); - if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) { + phys_devices = new VkPhysicalDevice[num_devices]; + if(vkEnumeratePhysicalDevices(vk_instance, &num_devices, phys_devices) != 0) { fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; } @@ -179,7 +172,7 @@ bool vku_create_device() if(qprop_count <= 0) { continue; } - qprop = (VkQueueFamilyProperties *)malloc(qprop_count * sizeof *qprop); + qprop = new VkQueueFamilyProperties[qprop_count]; vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop); for(unsigned int j=0; jformat = VK_FORMAT_B8G8R8_UNORM; } - vkGetSwapchainImagesKHR(vkdev, sc, &nimg, images); - if(count) *count = (int)nimg; - return images; + delete [] formats; + return true; } + int vku_get_next_image(VkSwapchainKHR sc) { uint32_t next; - if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, 0, 0, &next) != 0) { + if(vkAcquireNextImageKHR(vk_device, sc, UINT64_MAX, 0, 0, &next) != 0) { return -1; } return (int)next; @@ -391,7 +391,7 @@ void vku_present(VkSwapchainKHR sc, int img_idx) inf.pImageIndices = &index; inf.pResults = &res; - vkQueuePresentKHR(vkq, &inf); + vkQueuePresentKHR(vk_queue, &inf); } struct vku_buffer *vku_create_buffer(int sz, unsigned int usage) @@ -399,10 +399,7 @@ struct vku_buffer *vku_create_buffer(int sz, unsigned int usage) struct vku_buffer *buf; VkBufferCreateInfo binfo; - if(!(buf = (vku_buffer *)malloc(sizeof *buf))) { - perror("failed to allocate vk_buffer structure"); - return 0; - } + buf = new vku_buffer; memset(&binfo, 0, sizeof binfo); binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; @@ -410,283 +407,57 @@ struct vku_buffer *vku_create_buffer(int sz, unsigned int usage) binfo.usage = usage; binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - if(vkCreateBuffer(vkdev, &binfo, 0, &buf->buf) != 0) { + if(vkCreateBuffer(vk_device, &binfo, 0, &buf->buf) != 0) { fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage); return 0; } - // TODO back with memory - return buf; -} -void vku_destroy_buffer(struct vku_buffer *buf) -{ - if(buf) { - vkDestroyBuffer(vkdev, buf->buf, 0); - free(buf); - } -} + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(vk_device, buf->buf, &mr); -void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs, - VkBuffer src, int soffs, int size) -{ - VkBufferCopy copy; - copy.size = size; - copy.srcOffset = soffs; - copy.dstOffset = doffs; + DevMemBlock block; + if(!vku_allocate(mr.size, &block)) + return 0; - vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©); -} + buf->mem_pool = block.dev_mem; -/* paste + return buf; +} -static bool create_instance() +void vku_destroy_buffer(struct vku_buffer *buf) { - uint32_t layer_count = 0; - std::vector enabled_layers; - - if(vkEnumerateInstanceLayerProperties(&layer_count, 0) != VK_SUCCESS) { - fprintf(stderr, "Failed to query layer properties.\n"); - return false; - } - - if(layer_count > 0) { - VkLayerProperties *layers = (VkLayerProperties *)alloca(layer_count * sizeof *layers); - vkEnumerateInstanceLayerProperties(&layer_count, layers); - for(uint32_t i=0; i enabled_extensions; - - if(vkEnumerateInstanceExtensionProperties(0, &extensions_count, 0) != VK_SUCCESS) { - fprintf(stderr, "Failed to enumerate instance extension properties\n"); - return false; - } - - if(extensions_count > 0) { - VkExtensionProperties *extensions = (VkExtensionProperties *)alloca(extensions_count * sizeof *extensions); - vkEnumerateInstanceExtensionProperties(0, &extensions_count, extensions); - - for(uint32_t i=0; imem_pool); - if(vkCreateInstance(&create_info, 0, &inst) != VK_SUCCESS) { - fprintf(stderr, "Failed to create instance.\n"); - return false; + vkDestroyBuffer(vk_device, buf->buf, 0); + delete buf; } - - if(!create_device()) - return false; - - return true; } -static bool create_device() +bool vku_update_buffer(vku_buffer *buf, int size, void *data) { - int qfam_idx = -1; - int pdev_idx = -1; - - uint32_t dev_count; - if(vkEnumeratePhysicalDevices(inst, &dev_count, 0) != VK_SUCCESS) { - fprintf(stderr, "Failed to enumerate physical devices.\n"); + uint8_t *pdata; + if(vkMapMemory(vk_device, buf->mem_pool, 0, size, 0, (void**)&pdata) != VK_SUCCESS) { + fprintf(stderr, "Failed to map memory.\n"); return false; } - printf("%u devices found.\n", (unsigned int)dev_count); - - VkPhysicalDevice *phys_dev = (VkPhysicalDevice *)alloca(dev_count * sizeof *phys_dev); - vkEnumeratePhysicalDevices(inst, &dev_count, phys_dev); - VkPhysicalDeviceMemoryProperties memprop; - - for(uint32_t i=0; i 0) { - VkLayerProperties *layers = (VkLayerProperties*)alloca(layer_count * sizeof *layers); - vkEnumerateDeviceLayerProperties(pdev, &layer_count, layers); - printf("%u layers found.\n", layer_count); - for(uint32_t i=0; imem_pool); return true; } -static const char *dev_type_str(VkPhysicalDeviceType type) -{ - switch(type) { - case VK_PHYSICAL_DEVICE_TYPE_OTHER: - return "other"; - case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: - return "integrated GPU"; - case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: - return "discrete GPU"; - case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: - return "virtual GPU"; - case VK_PHYSICAL_DEVICE_TYPE_CPU: - return "CPU"; - default: - break; - } - return "unknown"; -} - -static const char *heap_flags_str(VkMemoryHeapFlags flags) -{ - static std::string str; - str.clear(); - if(flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) { - str += "device-local "; - } - if(!str.empty()) - str.pop_back(); - return str.c_str(); -} - -static const char *memtype_flags_str(VkMemoryPropertyFlags flags) +void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs, + VkBuffer src, int soffs, int size) { - static std::string str; - str.clear(); - if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { - str += "device-local "; - } - if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - str += "host-visible "; - } - if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) { - str += "host-coherent "; - } - if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { - str += "host-cached "; - } - if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { - str += "lazily-allocated "; - } - if(!str.empty()) - str.pop_back(); - return str.c_str(); -} + VkBufferCopy copy; + copy.size = size; + copy.srcOffset = soffs; + copy.dstOffset = doffs; -static const char *queue_flags_str(VkQueueFlags flags) -{ - static std::string str; - str.clear(); - if(flags & VK_QUEUE_GRAPHICS_BIT) - str += "graphics "; - if(flags & VK_QUEUE_COMPUTE_BIT) - str += "compute "; - if(flags & VK_QUEUE_TRANSFER_BIT) - str += "transfer "; - if(flags & VK_QUEUE_SPARSE_BINDING_BIT) - str += "sparse-binding "; - if(!str.empty()) - str.pop_back(); - return str.c_str(); + vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©); } -*/ const char *vku_get_vulkan_error_str(VkResult error) { @@ -733,7 +504,6 @@ const char *vku_get_vulkan_error_str(VkResult error) break; case VK_ERROR_FEATURE_NOT_PRESENT: errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT"); - break; case VK_ERROR_INCOMPATIBLE_DRIVER: errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER"); break; @@ -754,6 +524,7 @@ const char *vku_get_vulkan_error_str(VkResult error) return errmsg.c_str(); } + static const char *get_device_name_str(VkPhysicalDeviceType type) { switch(type) { @@ -850,3 +621,108 @@ static const char *get_mem_size_str(long sz) sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]); return str; } + +vku_descriptor *vku_create_descriptor(VkDescriptorType type, + VkFlags stage, int binding_point, + int size, int num_desc) +{ + vku_descriptor *desc = new vku_descriptor; + + desc->type = type; + desc->size = size; + desc->stage_flags = stage; + desc->binding_point = binding_point; + + VkDescriptorSetLayoutBinding dslb; + memset(&dslb, 0, sizeof dslb); + + dslb.binding = binding_point; + dslb.descriptorType = type; + dslb.descriptorCount = num_desc; + dslb.stageFlags = stage; + + VkDescriptorSetLayoutCreateInfo dslinf; + memset(&dslinf, 0, sizeof dslinf); + + dslinf.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + dslinf.bindingCount = 1; + dslinf.pBindings = &dslb; + + if(vkCreateDescriptorSetLayout(vk_device, &dslinf, 0, &desc->layout) != VK_SUCCESS) { + fprintf(stderr, "Failed to create vku_descriptor.\n"); + return 0; + } + + return desc; +} + +void vku_destroy_descriptor(vku_descriptor *desc) +{ + vkDestroyDescriptorSetLayout(vk_device, desc->layout, 0); + delete desc; +} + +bool vku_create_descriptor_pool(vku_descriptor **descriptors, int num_desc) +{ + if(vk_dpool) + vkDestroyDescriptorPool(vk_device, vk_dpool, 0); + + VkDescriptorPoolCreateInfo dpinf; + memset(&dpinf, 0, sizeof dpinf); + dpinf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + dpinf.maxSets = 1; + + dpinf.poolSizeCount = num_desc; + + std::vector sizes; + for(int i=0; itype; + psz.descriptorCount = 1; + sizes.push_back(psz); + } + + dpinf.pPoolSizes = sizes.data(); + + if(vkCreateDescriptorPool(vk_device, &dpinf, 0, &vk_dpool) != VK_SUCCESS) { + fprintf(stderr, "Failed to create descriptor pool.\n"); + return false; + } + + return true; +} + +void vku_destroy_descriptor_pool() +{ + vkDestroyDescriptorPool(vk_device, vk_dpool, 0); +} + +bool vku_allocate_descriptor_sets(vku_descriptor **desc, int num_desc, VkDescriptorSet set) +{ + VkDescriptorSetAllocateInfo dainf; + memset(&dainf, 0, sizeof dainf); + + dainf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + dainf.descriptorPool = vk_dpool; + dainf.descriptorSetCount = num_desc; + + std::vector layouts; + for(int i=0; ilayout); + } + + dainf.pSetLayouts = layouts.data(); + + if(vkAllocateDescriptorSets(vk_device, &dainf, &set) != VK_SUCCESS) { + fprintf(stderr, "Failed to allocate descriptor sets.\n"); + return false; + } + return true; +} + +void vku_free_descriptor_sets(VkDescriptorSet *sets, int num) +{ + vkFreeDescriptorSets(vk_device, vk_dpool, num, sets); +} + +