vulkan_test2

changeset 0:a13895a5f673

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Apr 2016 07:13:47 +0300
parents
children 8cb584143df3
files Makefile main.c
diffstat 2 files changed, 173 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Fri Apr 01 07:13:47 2016 +0300
     1.3 @@ -0,0 +1,12 @@
     1.4 +obj = main.o
     1.5 +bin = test
     1.6 +
     1.7 +CFLAGS = -pedantic -Wall -g
     1.8 +LDFLAGS = -lvulkan
     1.9 +
    1.10 +$(bin): $(obj)
    1.11 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.12 +
    1.13 +.PHONY: clean
    1.14 +clean:
    1.15 +	rm -f $(obj) $(bin)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/main.c	Fri Apr 01 07:13:47 2016 +0300
     2.3 @@ -0,0 +1,161 @@
     2.4 +#include <stdio.h>
     2.5 +#include <stdlib.h>
     2.6 +#include <string.h>
     2.7 +#include <alloca.h>
     2.8 +#include <vulkan/vulkan.h>
     2.9 +
    2.10 +const char *get_device_name(VkPhysicalDeviceType type);
    2.11 +const char *get_queue_flag_string(VkQueueFlagBits flags);
    2.12 +int ver_major(uint32_t ver);
    2.13 +int ver_minor(uint32_t ver);
    2.14 +int ver_patch(uint32_t ver);
    2.15 +
    2.16 +int main(void)
    2.17 +{
    2.18 +	int i, j;
    2.19 +	VkInstance vk;
    2.20 +	VkInstanceCreateInfo inst_info;
    2.21 +	VkPhysicalDevice *devices;
    2.22 +	VkDevice vkdev;
    2.23 +	VkDeviceCreateInfo dev_info;
    2.24 +	VkDeviceQueueCreateInfo queue_info;
    2.25 +	uint32_t num_devices;
    2.26 +	int sel_dev = -1;
    2.27 +	int sel_qfamily = -1;
    2.28 +	float qprio = 0.0f;
    2.29 +
    2.30 +	memset(&inst_info, 0, sizeof inst_info);
    2.31 +	inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    2.32 +
    2.33 +	if(vkCreateInstance(&inst_info, 0, &vk) != 0) {
    2.34 +		fprintf(stderr, "failed to create vulkan instance\n");
    2.35 +		return 1;
    2.36 +	}
    2.37 +	printf("created vulkan instance\n");
    2.38 +
    2.39 +	if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) {
    2.40 +		fprintf(stderr, "failed to enumerate vulkan physical devices\n");
    2.41 +		return 1;
    2.42 +	}
    2.43 +	devices = alloca(num_devices * sizeof *devices);
    2.44 +	if(vkEnumeratePhysicalDevices(vk, &num_devices, devices) != 0) {
    2.45 +		fprintf(stderr, "failed to enumerate vulkan physical devices\n");
    2.46 +		return 1;
    2.47 +	}
    2.48 +	printf("found %u physical device(s)\n", (unsigned int)num_devices);
    2.49 +
    2.50 +	for(i=0; i<(int)num_devices; i++) {
    2.51 +		VkPhysicalDeviceProperties dev_prop;
    2.52 +		VkQueueFamilyProperties *qprop;
    2.53 +		uint32_t qprop_count;
    2.54 +
    2.55 +		vkGetPhysicalDeviceProperties(devices[i], &dev_prop);
    2.56 +
    2.57 +		printf("Device %d: %s\n", i, dev_prop.deviceName);
    2.58 +		printf("  type: %s\n", get_device_name(dev_prop.deviceType));
    2.59 +		printf("  API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
    2.60 +				ver_patch(dev_prop.apiVersion));
    2.61 +		printf("  driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
    2.62 +				ver_patch(dev_prop.driverVersion));
    2.63 +		printf("  vendor id: %x  device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
    2.64 +
    2.65 +		vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, 0);
    2.66 +		if(qprop_count <= 0) {
    2.67 +			continue;
    2.68 +		}
    2.69 +		qprop = malloc(qprop_count * sizeof *qprop);
    2.70 +		vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, qprop);
    2.71 +
    2.72 +		for(j=0; j<qprop_count; j++) {
    2.73 +			printf("  Queue family %d:\n", j);
    2.74 +			printf("    flags: %s\n", get_queue_flag_string(qprop[j].queueFlags));
    2.75 +			printf("    num queues: %u\n", qprop[j].queueCount);
    2.76 +
    2.77 +			if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
    2.78 +				sel_dev = i;
    2.79 +				sel_qfamily = j;
    2.80 +			}
    2.81 +		}
    2.82 +		free(qprop);
    2.83 +	}
    2.84 +
    2.85 +	if(sel_dev < 0 || sel_qfamily < 0) {
    2.86 +		fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
    2.87 +		vkDestroyDevice(vkdev, 0);
    2.88 +		return 1;
    2.89 +	}
    2.90 +
    2.91 +	memset(&queue_info, 0, sizeof queue_info);
    2.92 +	queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    2.93 +	queue_info.queueFamilyIndex = sel_qfamily;
    2.94 +	queue_info.queueCount = 1;
    2.95 +	queue_info.pQueuePriorities = &qprio;
    2.96 +
    2.97 +	memset(&dev_info, 0, sizeof dev_info);
    2.98 +	dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    2.99 +	dev_info.queueCreateInfoCount = 1;
   2.100 +	dev_info.pQueueCreateInfos = &queue_info;
   2.101 +
   2.102 +	if(vkCreateDevice(devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
   2.103 +		fprintf(stderr, "failed to create device %d\n", sel_dev);
   2.104 +		return 1;
   2.105 +	}
   2.106 +	printf("created device %d\n", sel_dev);
   2.107 +
   2.108 +	vkDestroyDevice(vkdev, 0);
   2.109 +	vkDestroyInstance(vk, 0);
   2.110 +	return 0;
   2.111 +}
   2.112 +
   2.113 +
   2.114 +const char *get_device_name(VkPhysicalDeviceType type)
   2.115 +{
   2.116 +	switch(type) {
   2.117 +	case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
   2.118 +		return "integrated GPU";
   2.119 +	case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
   2.120 +		return "discrete GPU";
   2.121 +	case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
   2.122 +		return "virtual GPU";
   2.123 +	case VK_PHYSICAL_DEVICE_TYPE_CPU:
   2.124 +		return "CPU";
   2.125 +	default:
   2.126 +		break;
   2.127 +	}
   2.128 +	return "unknown";
   2.129 +}
   2.130 +
   2.131 +const char *get_queue_flag_string(VkQueueFlagBits flags)
   2.132 +{
   2.133 +	static char str[128];
   2.134 +
   2.135 +	str[0] = 0;
   2.136 +	if(flags & VK_QUEUE_GRAPHICS_BIT) {
   2.137 +		strcat(str, "graphics ");
   2.138 +	}
   2.139 +	if(flags & VK_QUEUE_COMPUTE_BIT) {
   2.140 +		strcat(str, "compute ");
   2.141 +	}
   2.142 +	if(flags & VK_QUEUE_TRANSFER_BIT) {
   2.143 +		strcat(str, "transfer ");
   2.144 +	}
   2.145 +	if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
   2.146 +		strcat(str, "sparse-binding ");
   2.147 +	}
   2.148 +	return str;
   2.149 +}
   2.150 +
   2.151 +int ver_major(uint32_t ver)
   2.152 +{
   2.153 +	return (ver >> 22) & 0x3ff;
   2.154 +}
   2.155 +
   2.156 +int ver_minor(uint32_t ver)
   2.157 +{
   2.158 +	return (ver >> 12) & 0x3ff;
   2.159 +}
   2.160 +
   2.161 +int ver_patch(uint32_t ver)
   2.162 +{
   2.163 +	return ver & 0xfff;
   2.164 +}