vulkan_test2

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <alloca.h>
5 #include <vulkan/vulkan.h>
7 const char *get_device_name(VkPhysicalDeviceType type);
8 const char *get_queue_flag_string(VkQueueFlagBits flags);
9 int ver_major(uint32_t ver);
10 int ver_minor(uint32_t ver);
11 int ver_patch(uint32_t ver);
13 int main(void)
14 {
15 int i, j;
16 VkInstance vk;
17 VkInstanceCreateInfo inst_info;
18 VkPhysicalDevice *devices;
19 VkDevice vkdev;
20 VkDeviceCreateInfo dev_info;
21 VkDeviceQueueCreateInfo queue_info;
22 uint32_t num_devices;
23 int sel_dev = -1;
24 int sel_qfamily = -1;
25 float qprio = 0.0f;
27 memset(&inst_info, 0, sizeof inst_info);
28 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
30 if(vkCreateInstance(&inst_info, 0, &vk) != 0) {
31 fprintf(stderr, "failed to create vulkan instance\n");
32 return 1;
33 }
34 printf("created vulkan instance\n");
36 if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) {
37 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
38 return 1;
39 }
40 devices = alloca(num_devices * sizeof *devices);
41 if(vkEnumeratePhysicalDevices(vk, &num_devices, devices) != 0) {
42 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
43 return 1;
44 }
45 printf("found %u physical device(s)\n", (unsigned int)num_devices);
47 for(i=0; i<(int)num_devices; i++) {
48 VkPhysicalDeviceProperties dev_prop;
49 VkQueueFamilyProperties *qprop;
50 uint32_t qprop_count;
52 vkGetPhysicalDeviceProperties(devices[i], &dev_prop);
54 printf("Device %d: %s\n", i, dev_prop.deviceName);
55 printf(" type: %s\n", get_device_name(dev_prop.deviceType));
56 printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
57 ver_patch(dev_prop.apiVersion));
58 printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
59 ver_patch(dev_prop.driverVersion));
60 printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
62 vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, 0);
63 if(qprop_count <= 0) {
64 continue;
65 }
66 qprop = malloc(qprop_count * sizeof *qprop);
67 vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, qprop);
69 for(j=0; j<qprop_count; j++) {
70 printf(" Queue family %d:\n", j);
71 printf(" flags: %s\n", get_queue_flag_string(qprop[j].queueFlags));
72 printf(" num queues: %u\n", qprop[j].queueCount);
74 if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
75 sel_dev = i;
76 sel_qfamily = j;
77 }
78 }
79 free(qprop);
80 }
82 if(sel_dev < 0 || sel_qfamily < 0) {
83 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
84 vkDestroyDevice(vkdev, 0);
85 return 1;
86 }
88 memset(&queue_info, 0, sizeof queue_info);
89 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
90 queue_info.queueFamilyIndex = sel_qfamily;
91 queue_info.queueCount = 1;
92 queue_info.pQueuePriorities = &qprio;
94 memset(&dev_info, 0, sizeof dev_info);
95 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
96 dev_info.queueCreateInfoCount = 1;
97 dev_info.pQueueCreateInfos = &queue_info;
99 if(vkCreateDevice(devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
100 fprintf(stderr, "failed to create device %d\n", sel_dev);
101 return 1;
102 }
103 printf("created device %d\n", sel_dev);
105 vkDestroyDevice(vkdev, 0);
106 vkDestroyInstance(vk, 0);
107 return 0;
108 }
111 const char *get_device_name(VkPhysicalDeviceType type)
112 {
113 switch(type) {
114 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
115 return "integrated GPU";
116 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
117 return "discrete GPU";
118 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
119 return "virtual GPU";
120 case VK_PHYSICAL_DEVICE_TYPE_CPU:
121 return "CPU";
122 default:
123 break;
124 }
125 return "unknown";
126 }
128 const char *get_queue_flag_string(VkQueueFlagBits flags)
129 {
130 static char str[128];
132 str[0] = 0;
133 if(flags & VK_QUEUE_GRAPHICS_BIT) {
134 strcat(str, "graphics ");
135 }
136 if(flags & VK_QUEUE_COMPUTE_BIT) {
137 strcat(str, "compute ");
138 }
139 if(flags & VK_QUEUE_TRANSFER_BIT) {
140 strcat(str, "transfer ");
141 }
142 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
143 strcat(str, "sparse-binding ");
144 }
145 return str;
146 }
148 int ver_major(uint32_t ver)
149 {
150 return (ver >> 22) & 0x3ff;
151 }
153 int ver_minor(uint32_t ver)
154 {
155 return (ver >> 12) & 0x3ff;
156 }
158 int ver_patch(uint32_t ver)
159 {
160 return ver & 0xfff;
161 }