# HG changeset patch # User John Tsiombikas # Date 1506031270 -10800 # Node ID 68e1c437343f4347314293177b672cc6015561cf # Parent 5b2ae06283aa250f6d442b8424fbd232e5cdc4e9 more vulkan diff -r 5b2ae06283aa -r 68e1c437343f Makefile --- a/Makefile Sun Dec 18 09:15:16 2016 +0200 +++ b/Makefile Fri Sep 22 01:01:10 2017 +0300 @@ -1,4 +1,5 @@ -obj = main.o +src = $(wildcard src/*.c) +obj = $(src:.c=.o) bin = test CFLAGS = -pedantic -Wall -g diff -r 5b2ae06283aa -r 68e1c437343f main.c --- a/main.c Sun Dec 18 09:15:16 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,225 +0,0 @@ -#include -#include -#include -#include -#include - -const char *get_device_name(VkPhysicalDeviceType type); -const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags); -const char *get_queue_flag_string(VkQueueFlagBits flags); -int ver_major(uint32_t ver); -int ver_minor(uint32_t ver); -int ver_patch(uint32_t ver); -const char *mem_size_str(long sz); - -int main(void) -{ - int i, j; - VkInstance vk; - VkInstanceCreateInfo inst_info; - VkPhysicalDevice *devices; - VkDevice vkdev; - VkDeviceCreateInfo dev_info; - VkDeviceQueueCreateInfo queue_info; - uint32_t num_devices; - int sel_dev = -1; - int sel_qfamily = -1; - float qprio = 0.0f; - - memset(&inst_info, 0, sizeof inst_info); - inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; - - if(vkCreateInstance(&inst_info, 0, &vk) != 0) { - fprintf(stderr, "failed to create vulkan instance\n"); - return 1; - } - printf("created vulkan instance\n"); - - if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) { - fprintf(stderr, "failed to enumerate vulkan physical devices\n"); - return 1; - } - devices = alloca(num_devices * sizeof *devices); - if(vkEnumeratePhysicalDevices(vk, &num_devices, devices) != 0) { - fprintf(stderr, "failed to enumerate vulkan physical devices\n"); - return 1; - } - printf("found %u physical device(s)\n", (unsigned int)num_devices); - - for(i=0; i<(int)num_devices; i++) { - VkPhysicalDeviceProperties dev_prop; - VkPhysicalDeviceMemoryProperties mem_prop; - VkQueueFamilyProperties *qprop; - uint32_t qprop_count; - - vkGetPhysicalDeviceProperties(devices[i], &dev_prop); - - printf("Device %d: %s\n", i, dev_prop.deviceName); - printf(" type: %s\n", get_device_name(dev_prop.deviceType)); - printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion), - ver_patch(dev_prop.apiVersion)); - printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion), - ver_patch(dev_prop.driverVersion)); - printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID); - - - vkGetPhysicalDeviceMemoryProperties(devices[i], &mem_prop); - printf(" %d memory heaps:\n", mem_prop.memoryHeapCount); - for(j=0; j> 22) & 0x3ff; -} - -int ver_minor(uint32_t ver) -{ - return (ver >> 12) & 0x3ff; -} - -int ver_patch(uint32_t ver) -{ - return ver & 0xfff; -} - -const char *mem_size_str(long sz) -{ - static char str[64]; - static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 }; - int uidx = 0; - sz *= 10; - - while(sz >= 10240 && unitstr[uidx + 1]) { - sz /= 1024; - ++uidx; - } - sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]); - return str; -} diff -r 5b2ae06283aa -r 68e1c437343f src/main.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.c Fri Sep 22 01:01:10 2017 +0300 @@ -0,0 +1,13 @@ +#include +#include +#include "vku.h" + +int main(void) +{ + if(vku_create_dev() == -1) { + return -1; + } + + vku_cleanup(); + return 0; +} diff -r 5b2ae06283aa -r 68e1c437343f src/vku.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vku.c Fri Sep 22 01:01:10 2017 +0300 @@ -0,0 +1,281 @@ +#include +#include +#include +#include +#include "vku.h" + +static const char *get_device_name(VkPhysicalDeviceType type); +static const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags); +static const char *get_queue_flag_string(VkQueueFlagBits flags); +static int ver_major(uint32_t ver); +static int ver_minor(uint32_t ver); +static int ver_patch(uint32_t ver); +static const char *mem_size_str(long sz); + + +VkInstance vk; +VkDevice vkdev; +VkQueue vkq; + +int vku_create_dev(void) +{ + int i, j; + VkInstanceCreateInfo inst_info; + VkPhysicalDevice *devices; + VkDeviceCreateInfo dev_info; + VkDeviceQueueCreateInfo queue_info; + VkCommandPoolCreateInfo cmdpool_info; + uint32_t num_devices; + int sel_dev = -1; + int sel_qfamily = -1; + float qprio = 0.0f; + + memset(&inst_info, 0, sizeof inst_info); + inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + + if(vkCreateInstance(&inst_info, 0, &vk) != 0) { + fprintf(stderr, "failed to create vulkan instance\n"); + return -1; + } + printf("created vulkan instance\n"); + + if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) { + fprintf(stderr, "failed to enumerate vulkan physical devices\n"); + return -1; + } + devices = alloca(num_devices * sizeof *devices); + if(vkEnumeratePhysicalDevices(vk, &num_devices, devices) != 0) { + fprintf(stderr, "failed to enumerate vulkan physical devices\n"); + return -1; + } + printf("found %u physical device(s)\n", (unsigned int)num_devices); + + for(i=0; i<(int)num_devices; i++) { + VkPhysicalDeviceProperties dev_prop; + VkPhysicalDeviceMemoryProperties mem_prop; + VkQueueFamilyProperties *qprop; + uint32_t qprop_count; + + vkGetPhysicalDeviceProperties(devices[i], &dev_prop); + + printf("Device %d: %s\n", i, dev_prop.deviceName); + printf(" type: %s\n", get_device_name(dev_prop.deviceType)); + printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion), + ver_patch(dev_prop.apiVersion)); + printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion), + ver_patch(dev_prop.driverVersion)); + printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID); + + + vkGetPhysicalDeviceMemoryProperties(devices[i], &mem_prop); + printf(" %d memory heaps:\n", mem_prop.memoryHeapCount); + for(j=0; jbuf) != 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 vk_buffer *buf) +{ + if(buf) { + vkDestroyBuffer(vkdev, buf->buf, 0); + free(buf); + } +} + +static const char *get_device_name(VkPhysicalDeviceType type) +{ + switch(type) { + 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 *get_mem_prop_flag_string(VkMemoryPropertyFlags flags) +{ + static char str[128]; + + str[0] = 0; + if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { + strcat(str, "device-local "); + } + if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { + strcat(str, "host-visible "); + } + if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) { + strcat(str, "host-coherent "); + } + if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { + strcat(str, "host-cached "); + } + if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) { + strcat(str, "lazily-allocated "); + } + + if(!*str) { + strcat(str, "-"); + } + return str; +} + +static const char *get_queue_flag_string(VkQueueFlagBits flags) +{ + static char str[128]; + + str[0] = 0; + if(flags & VK_QUEUE_GRAPHICS_BIT) { + strcat(str, "graphics "); + } + if(flags & VK_QUEUE_COMPUTE_BIT) { + strcat(str, "compute "); + } + if(flags & VK_QUEUE_TRANSFER_BIT) { + strcat(str, "transfer "); + } + if(flags & VK_QUEUE_SPARSE_BINDING_BIT) { + strcat(str, "sparse-binding "); + } + if(!*str) { + strcat(str, "-"); + } + return str; +} + +static int ver_major(uint32_t ver) +{ + return (ver >> 22) & 0x3ff; +} + +static int ver_minor(uint32_t ver) +{ + return (ver >> 12) & 0x3ff; +} + +static int ver_patch(uint32_t ver) +{ + return ver & 0xfff; +} + +static const char *mem_size_str(long sz) +{ + static char str[64]; + static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 }; + int uidx = 0; + sz *= 10; + + while(sz >= 10240 && unitstr[uidx + 1]) { + sz /= 1024; + ++uidx; + } + sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]); + return str; +} diff -r 5b2ae06283aa -r 68e1c437343f src/vku.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vku.h Fri Sep 22 01:01:10 2017 +0300 @@ -0,0 +1,24 @@ +#ifndef VKU_H_ +#define VKU_H_ + +#include + +VkInstance vk; +VkDevice vkdev; +VkQueue vkq; +VkCommandPool vkcmdpool; + +struct vk_buffer { + VkBuffer buf; + VkDeviceMemory mem_pool; + int mem_start, mem_size; +}; + +int vku_create_dev(void); +void vku_cleanup(void); + + +struct vk_buffer *vku_create_buffer(int sz, unsigned int usage); +void vku_destroy_buffer(struct vk_buffer *buf); + +#endif /* VKU_H_ */