nuclear@3: #include nuclear@3: #include nuclear@3: #include nuclear@4: #include nuclear@3: #include "vku.h" nuclear@3: nuclear@3: static const char *get_device_name(VkPhysicalDeviceType type); nuclear@3: static const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags); nuclear@3: static const char *get_queue_flag_string(VkQueueFlagBits flags); nuclear@3: static int ver_major(uint32_t ver); nuclear@3: static int ver_minor(uint32_t ver); nuclear@3: static int ver_patch(uint32_t ver); nuclear@3: static const char *mem_size_str(long sz); nuclear@3: nuclear@3: VkInstance vk; nuclear@3: VkDevice vkdev; nuclear@3: VkQueue vkq; nuclear@3: nuclear@4: static VkPhysicalDevice *phys_devices; nuclear@4: static int sel_dev, sel_qfamily; nuclear@4: nuclear@4: static VkExtensionProperties *vkext, *vkdevext; nuclear@4: static uint32_t vkext_count, vkdevext_count; nuclear@4: nuclear@4: nuclear@4: int vku_have_extension(const char *name) nuclear@4: { nuclear@4: int i; nuclear@4: nuclear@4: if(!vkext) { nuclear@4: vkext_count = 0; nuclear@4: vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0); nuclear@4: if(vkext_count) { nuclear@4: if(!(vkext = malloc(vkext_count * sizeof *vkext))) { nuclear@4: perror("failed to allocate instance extension list"); nuclear@4: return 0; nuclear@4: } nuclear@4: vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext); nuclear@4: nuclear@4: printf("instance extensions:\n"); nuclear@4: for(i=0; i<(int)vkext_count; i++) { nuclear@4: printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion); nuclear@4: } nuclear@4: } nuclear@4: } nuclear@4: nuclear@4: for(i=0; i<(int)vkext_count; i++) { nuclear@4: if(strcmp(vkext[i].extensionName, name) == 0) { nuclear@4: return 1; nuclear@4: } nuclear@4: } nuclear@4: return 0; nuclear@4: } nuclear@4: nuclear@4: int vku_have_device_extension(const char *name) nuclear@4: { nuclear@4: int i; nuclear@4: nuclear@4: if(sel_dev < 0) return 0; nuclear@4: nuclear@4: if(!vkdevext) { nuclear@4: vkdevext_count = 0; nuclear@4: vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0); nuclear@4: if(vkdevext_count) { nuclear@4: if(!(vkdevext = malloc(vkdevext_count * sizeof *vkdevext))) { nuclear@4: perror("failed to allocate device extension list"); nuclear@4: return 0; nuclear@4: } nuclear@4: vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext); nuclear@4: nuclear@4: printf("selected device extensions:\n"); nuclear@4: for(i=0; i<(int)vkdevext_count; i++) { nuclear@4: printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion); nuclear@4: } nuclear@4: } nuclear@4: } nuclear@4: nuclear@4: for(i=0; i<(int)vkdevext_count; i++) { nuclear@4: if(strcmp(vkdevext[i].extensionName, name) == 0) { nuclear@4: return 1; nuclear@4: } nuclear@4: } nuclear@4: return 0; nuclear@4: } nuclear@4: nuclear@3: int vku_create_dev(void) nuclear@3: { nuclear@3: int i, j; nuclear@3: VkInstanceCreateInfo inst_info; nuclear@3: VkDeviceCreateInfo dev_info; nuclear@3: VkDeviceQueueCreateInfo queue_info; nuclear@3: VkCommandPoolCreateInfo cmdpool_info; nuclear@3: uint32_t num_devices; nuclear@3: float qprio = 0.0f; nuclear@3: nuclear@4: static const char *ext_names[] = { nuclear@4: #ifdef VK_USE_PLATFORM_XLIB_KHR nuclear@4: "VK_KHR_xlib_surface", nuclear@4: #endif nuclear@4: "VK_KHR_surface" nuclear@4: }; nuclear@5: static const char *devext_names[] = { nuclear@5: "VK_KHR_swapchain" nuclear@5: }; nuclear@4: nuclear@4: sel_dev = -1; nuclear@4: sel_qfamily = -1; nuclear@4: nuclear@4: for(i=0; ibuf) != 0) { nuclear@3: fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage); nuclear@3: return 0; nuclear@3: } nuclear@3: // TODO back with memory nuclear@3: return buf; nuclear@3: } nuclear@3: nuclear@4: void vku_destroy_buffer(struct vku_buffer *buf) nuclear@3: { nuclear@3: if(buf) { nuclear@3: vkDestroyBuffer(vkdev, buf->buf, 0); nuclear@3: free(buf); nuclear@3: } nuclear@3: } nuclear@3: nuclear@4: void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs, nuclear@4: VkBuffer src, int soffs, int size) nuclear@4: { nuclear@4: VkBufferCopy copy; nuclear@4: copy.size = size; nuclear@4: copy.srcOffset = soffs; nuclear@4: copy.dstOffset = doffs; nuclear@4: nuclear@4: vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©); nuclear@4: } nuclear@4: nuclear@4: #ifdef VK_USE_PLATFORM_XLIB_KHR nuclear@5: nuclear@4: int vku_xlib_usable_visual(Display *dpy, VisualID vid) nuclear@4: { nuclear@4: return vkGetPhysicalDeviceXlibPresentationSupportKHR(phys_devices[sel_dev], nuclear@4: sel_qfamily, dpy, vid); nuclear@4: } nuclear@5: nuclear@5: VkSurfaceKHR vku_xlib_create_surface(Display *dpy, Window win) nuclear@5: { nuclear@5: VkSurfaceKHR surf; nuclear@5: VkXlibSurfaceCreateInfoKHR inf; nuclear@5: nuclear@5: memset(&inf, 0, sizeof inf); nuclear@5: inf.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; nuclear@5: inf.dpy = dpy; nuclear@5: inf.window = win; nuclear@5: nuclear@5: if(vkCreateXlibSurfaceKHR(vk, &inf, 0, &surf) != 0) { nuclear@5: return 0; nuclear@5: } nuclear@5: return surf; nuclear@5: } nuclear@5: nuclear@4: #endif /* VK_USE_PLATFORM_XLIB_KHR */ nuclear@4: nuclear@3: static const char *get_device_name(VkPhysicalDeviceType type) nuclear@3: { nuclear@3: switch(type) { nuclear@3: case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: nuclear@3: return "integrated GPU"; nuclear@3: case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: nuclear@3: return "discrete GPU"; nuclear@3: case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: nuclear@3: return "virtual GPU"; nuclear@3: case VK_PHYSICAL_DEVICE_TYPE_CPU: nuclear@3: return "CPU"; nuclear@3: default: nuclear@3: break; nuclear@3: } nuclear@3: return "unknown"; nuclear@3: } nuclear@3: nuclear@3: static const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags) nuclear@3: { nuclear@3: static char str[128]; nuclear@3: nuclear@3: str[0] = 0; nuclear@3: if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { nuclear@3: strcat(str, "device-local "); nuclear@3: } nuclear@3: if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { nuclear@3: strcat(str, "host-visible "); nuclear@3: } nuclear@3: if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) { nuclear@3: strcat(str, "host-coherent "); nuclear@3: } nuclear@3: if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { nuclear@3: strcat(str, "host-cached "); nuclear@3: } nuclear@3: if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { nuclear@3: strcat(str, "lazily-allocated "); nuclear@3: } nuclear@3: nuclear@3: if(!*str) { nuclear@3: strcat(str, "-"); nuclear@3: } nuclear@3: return str; nuclear@3: } nuclear@3: nuclear@3: static const char *get_queue_flag_string(VkQueueFlagBits flags) nuclear@3: { nuclear@3: static char str[128]; nuclear@3: nuclear@3: str[0] = 0; nuclear@3: if(flags & VK_QUEUE_GRAPHICS_BIT) { nuclear@3: strcat(str, "graphics "); nuclear@3: } nuclear@3: if(flags & VK_QUEUE_COMPUTE_BIT) { nuclear@3: strcat(str, "compute "); nuclear@3: } nuclear@3: if(flags & VK_QUEUE_TRANSFER_BIT) { nuclear@3: strcat(str, "transfer "); nuclear@3: } nuclear@3: if(flags & VK_QUEUE_SPARSE_BINDING_BIT) { nuclear@3: strcat(str, "sparse-binding "); nuclear@3: } nuclear@3: if(!*str) { nuclear@3: strcat(str, "-"); nuclear@3: } nuclear@3: return str; nuclear@3: } nuclear@3: nuclear@3: static int ver_major(uint32_t ver) nuclear@3: { nuclear@3: return (ver >> 22) & 0x3ff; nuclear@3: } nuclear@3: nuclear@3: static int ver_minor(uint32_t ver) nuclear@3: { nuclear@3: return (ver >> 12) & 0x3ff; nuclear@3: } nuclear@3: nuclear@3: static int ver_patch(uint32_t ver) nuclear@3: { nuclear@3: return ver & 0xfff; nuclear@3: } nuclear@3: nuclear@3: static const char *mem_size_str(long sz) nuclear@3: { nuclear@3: static char str[64]; nuclear@3: static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 }; nuclear@3: int uidx = 0; nuclear@3: sz *= 10; nuclear@3: nuclear@3: while(sz >= 10240 && unitstr[uidx + 1]) { nuclear@3: sz /= 1024; nuclear@3: ++uidx; nuclear@3: } nuclear@3: sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]); nuclear@3: return str; nuclear@3: }