vulkan_test2

diff main.c @ 0:a13895a5f673

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Apr 2016 07:13:47 +0300
parents
children 8cb584143df3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/main.c	Fri Apr 01 07:13:47 2016 +0300
     1.3 @@ -0,0 +1,161 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <alloca.h>
     1.8 +#include <vulkan/vulkan.h>
     1.9 +
    1.10 +const char *get_device_name(VkPhysicalDeviceType type);
    1.11 +const char *get_queue_flag_string(VkQueueFlagBits flags);
    1.12 +int ver_major(uint32_t ver);
    1.13 +int ver_minor(uint32_t ver);
    1.14 +int ver_patch(uint32_t ver);
    1.15 +
    1.16 +int main(void)
    1.17 +{
    1.18 +	int i, j;
    1.19 +	VkInstance vk;
    1.20 +	VkInstanceCreateInfo inst_info;
    1.21 +	VkPhysicalDevice *devices;
    1.22 +	VkDevice vkdev;
    1.23 +	VkDeviceCreateInfo dev_info;
    1.24 +	VkDeviceQueueCreateInfo queue_info;
    1.25 +	uint32_t num_devices;
    1.26 +	int sel_dev = -1;
    1.27 +	int sel_qfamily = -1;
    1.28 +	float qprio = 0.0f;
    1.29 +
    1.30 +	memset(&inst_info, 0, sizeof inst_info);
    1.31 +	inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    1.32 +
    1.33 +	if(vkCreateInstance(&inst_info, 0, &vk) != 0) {
    1.34 +		fprintf(stderr, "failed to create vulkan instance\n");
    1.35 +		return 1;
    1.36 +	}
    1.37 +	printf("created vulkan instance\n");
    1.38 +
    1.39 +	if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) {
    1.40 +		fprintf(stderr, "failed to enumerate vulkan physical devices\n");
    1.41 +		return 1;
    1.42 +	}
    1.43 +	devices = alloca(num_devices * sizeof *devices);
    1.44 +	if(vkEnumeratePhysicalDevices(vk, &num_devices, devices) != 0) {
    1.45 +		fprintf(stderr, "failed to enumerate vulkan physical devices\n");
    1.46 +		return 1;
    1.47 +	}
    1.48 +	printf("found %u physical device(s)\n", (unsigned int)num_devices);
    1.49 +
    1.50 +	for(i=0; i<(int)num_devices; i++) {
    1.51 +		VkPhysicalDeviceProperties dev_prop;
    1.52 +		VkQueueFamilyProperties *qprop;
    1.53 +		uint32_t qprop_count;
    1.54 +
    1.55 +		vkGetPhysicalDeviceProperties(devices[i], &dev_prop);
    1.56 +
    1.57 +		printf("Device %d: %s\n", i, dev_prop.deviceName);
    1.58 +		printf("  type: %s\n", get_device_name(dev_prop.deviceType));
    1.59 +		printf("  API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
    1.60 +				ver_patch(dev_prop.apiVersion));
    1.61 +		printf("  driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
    1.62 +				ver_patch(dev_prop.driverVersion));
    1.63 +		printf("  vendor id: %x  device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
    1.64 +
    1.65 +		vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, 0);
    1.66 +		if(qprop_count <= 0) {
    1.67 +			continue;
    1.68 +		}
    1.69 +		qprop = malloc(qprop_count * sizeof *qprop);
    1.70 +		vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, qprop);
    1.71 +
    1.72 +		for(j=0; j<qprop_count; j++) {
    1.73 +			printf("  Queue family %d:\n", j);
    1.74 +			printf("    flags: %s\n", get_queue_flag_string(qprop[j].queueFlags));
    1.75 +			printf("    num queues: %u\n", qprop[j].queueCount);
    1.76 +
    1.77 +			if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
    1.78 +				sel_dev = i;
    1.79 +				sel_qfamily = j;
    1.80 +			}
    1.81 +		}
    1.82 +		free(qprop);
    1.83 +	}
    1.84 +
    1.85 +	if(sel_dev < 0 || sel_qfamily < 0) {
    1.86 +		fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
    1.87 +		vkDestroyDevice(vkdev, 0);
    1.88 +		return 1;
    1.89 +	}
    1.90 +
    1.91 +	memset(&queue_info, 0, sizeof queue_info);
    1.92 +	queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    1.93 +	queue_info.queueFamilyIndex = sel_qfamily;
    1.94 +	queue_info.queueCount = 1;
    1.95 +	queue_info.pQueuePriorities = &qprio;
    1.96 +
    1.97 +	memset(&dev_info, 0, sizeof dev_info);
    1.98 +	dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    1.99 +	dev_info.queueCreateInfoCount = 1;
   1.100 +	dev_info.pQueueCreateInfos = &queue_info;
   1.101 +
   1.102 +	if(vkCreateDevice(devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
   1.103 +		fprintf(stderr, "failed to create device %d\n", sel_dev);
   1.104 +		return 1;
   1.105 +	}
   1.106 +	printf("created device %d\n", sel_dev);
   1.107 +
   1.108 +	vkDestroyDevice(vkdev, 0);
   1.109 +	vkDestroyInstance(vk, 0);
   1.110 +	return 0;
   1.111 +}
   1.112 +
   1.113 +
   1.114 +const char *get_device_name(VkPhysicalDeviceType type)
   1.115 +{
   1.116 +	switch(type) {
   1.117 +	case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
   1.118 +		return "integrated GPU";
   1.119 +	case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
   1.120 +		return "discrete GPU";
   1.121 +	case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
   1.122 +		return "virtual GPU";
   1.123 +	case VK_PHYSICAL_DEVICE_TYPE_CPU:
   1.124 +		return "CPU";
   1.125 +	default:
   1.126 +		break;
   1.127 +	}
   1.128 +	return "unknown";
   1.129 +}
   1.130 +
   1.131 +const char *get_queue_flag_string(VkQueueFlagBits flags)
   1.132 +{
   1.133 +	static char str[128];
   1.134 +
   1.135 +	str[0] = 0;
   1.136 +	if(flags & VK_QUEUE_GRAPHICS_BIT) {
   1.137 +		strcat(str, "graphics ");
   1.138 +	}
   1.139 +	if(flags & VK_QUEUE_COMPUTE_BIT) {
   1.140 +		strcat(str, "compute ");
   1.141 +	}
   1.142 +	if(flags & VK_QUEUE_TRANSFER_BIT) {
   1.143 +		strcat(str, "transfer ");
   1.144 +	}
   1.145 +	if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
   1.146 +		strcat(str, "sparse-binding ");
   1.147 +	}
   1.148 +	return str;
   1.149 +}
   1.150 +
   1.151 +int ver_major(uint32_t ver)
   1.152 +{
   1.153 +	return (ver >> 22) & 0x3ff;
   1.154 +}
   1.155 +
   1.156 +int ver_minor(uint32_t ver)
   1.157 +{
   1.158 +	return (ver >> 12) & 0x3ff;
   1.159 +}
   1.160 +
   1.161 +int ver_patch(uint32_t ver)
   1.162 +{
   1.163 +	return ver & 0xfff;
   1.164 +}