-/* paste
-
-static bool create_instance()
-{
- uint32_t layer_count = 0;
- std::vector<const char *> 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<layer_count; i++) {
- if(strcmp(layers[i].layerName, "VK_LAYER_LUNARG_standard_validation")) {
- enabled_layers.push_back(layers[i].layerName);
- }
- }
- }
-
- uint32_t extensions_count = 0;
- std::vector<const char *> 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; i<extensions_count; i++) {
- printf("Extension %u: %s %u.\n", i, extensions[i].extensionName, extensions[i].specVersion);
- //enable all the available extensions
- enabled_extensions.push_back(extensions[i].extensionName);
- enabled_extension_names.push_back(std::string(extensions[i].extensionName));
- }
- }
-
- VkInstanceCreateInfo create_info;
- memset(&create_info, 0, sizeof create_info);
- create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
-
- if(!enabled_layers.empty()) {
- create_info.enabledLayerCount = enabled_layers.size();
- create_info.ppEnabledLayerNames = enabled_layers.data();
- }
-
- if(!enabled_extensions.empty()) {
- create_info.enabledExtensionCount = enabled_extensions.size();
- create_info.ppEnabledExtensionNames = enabled_extensions.data();
- }
-
- if(vkCreateInstance(&create_info, 0, &inst) != VK_SUCCESS) {
- fprintf(stderr, "Failed to create instance.\n");
- return false;
- }
-
- if(!create_device())
- return false;
-
- return true;
-}
-
-static bool create_device()
-{
- 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");
- 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<dev_count; i++) {
- VkPhysicalDeviceProperties dev_props;
- vkGetPhysicalDeviceProperties(phys_dev[i], &dev_props);
-
- //memory heaps:
- vkGetPhysicalDeviceMemoryProperties(phys_dev[i], &memprop);
- printf("\tNumber of heaps: %u\n", memprop.memoryHeapCount);
- for(uint32_t j=0; j<memprop.memoryHeapCount; j++) {
- printf("\t\tHeap %u size: %lu\n", j, (unsigned long)memprop.memoryHeaps[j].size);
- printf("\t\tHeap %u flags: %s\n", j, heap_flags_str(memprop.memoryHeaps[j].flags));
- }
- //memory types
- printf("\tMemory types: %u\n", memprop.memoryTypeCount);
- for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
- printf("\t\tType %u heap index: %u\n", j, memprop.memoryTypes[j].heapIndex);
- printf("\t\tType %u flags: %s\n", j, memtype_flags_str(memprop.memoryTypes[j].propertyFlags));
- }
-
- //supported features
- VkPhysicalDeviceFeatures features;
- vkGetPhysicalDeviceFeatures(phys_dev[i], &features);
-
- //queue families
- uint32_t qfam_count;
- vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, 0);
- printf("\tQueue Families: %u\n", qfam_count);
- VkQueueFamilyProperties *qfam_props = new VkQueueFamilyProperties[qfam_count];
- vkGetPhysicalDeviceQueueFamilyProperties(phys_dev[i], &qfam_count, qfam_props);
- for(uint32_t j=0; j<qfam_count; j++) {
- printf("\t\tFamily %u flags: %s\n", j, queue_flags_str(qfam_props[j].queueFlags));
- printf("\t\tFamily %u number of queues: %u\n", j, qfam_props[j].queueCount);
-
- if((qfam_props[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) && (pdev_idx == -1)) {
- pdev_idx = i;
- qfam_idx = j;
- num_queues = qfam_props[j].queueCount;
- }
- }
- delete [] qfam_props;
- }
-
- if(pdev_idx == -1) {
- fprintf(stderr, "No suitable devices found.\n");
- return false;
- }
-
- pdev = *(phys_dev + pdev_idx);
- qfamily_idx = qfam_idx;
-
- uint32_t layer_count;
- if(vkEnumerateDeviceLayerProperties(pdev, &layer_count, 0) != VK_SUCCESS) {
- fprintf(stderr, "Failed to enumerate device layers.\n");
- return false;
- }
- if(layer_count > 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; i<layer_count; i++) {
- printf("Layer %u: %s (%u, %u)\n", i, layers[i].layerName,
- layers[i].specVersion, layers[i].implementationVersion);
- printf("\tDesc: %s\n", layers[i].description);
- }
- }
-
- VkDeviceCreateInfo dev_info;
- memset(&dev_info, 0, sizeof dev_info);
- dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
-
- VkDeviceQueueCreateInfo dev_qinfo;
- memset(&dev_qinfo, 0, sizeof dev_qinfo);
- dev_qinfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
- dev_qinfo.queueFamilyIndex = qfam_idx;
- dev_qinfo.queueCount = 1;
-
- dev_info.queueCreateInfoCount = 1;
- dev_info.pQueueCreateInfos = &dev_qinfo;
-
- if(vkCreateDevice(pdev, &dev_info, 0, &device) != VK_SUCCESS) {
- fprintf(stderr, "Failed to create logical device.\n");
- return false;
- }
-
- vkGetPhysicalDeviceMemoryProperties(pdev, &memprop);
- for(uint32_t j=0; j<memprop.memoryTypeCount; j++) {
- if(memprop.memoryTypes[j].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
- device_mem_idx = j;
- printf("Selected device memory index: %u\n", device_mem_idx);
- break;
- }
- }
-
- 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)
-{
- 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();
-}
-
-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();
-}
-*/
-