vk_test1

view main.c @ 1:8cb584143df3

comment
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Apr 2016 07:14:47 +0300
parents a13895a5f673
children
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 // create device & command queue
89 memset(&queue_info, 0, sizeof queue_info);
90 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
91 queue_info.queueFamilyIndex = sel_qfamily;
92 queue_info.queueCount = 1;
93 queue_info.pQueuePriorities = &qprio;
95 memset(&dev_info, 0, sizeof dev_info);
96 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
97 dev_info.queueCreateInfoCount = 1;
98 dev_info.pQueueCreateInfos = &queue_info;
100 if(vkCreateDevice(devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
101 fprintf(stderr, "failed to create device %d\n", sel_dev);
102 return 1;
103 }
104 printf("created device %d\n", sel_dev);
106 vkDestroyDevice(vkdev, 0);
107 vkDestroyInstance(vk, 0);
108 return 0;
109 }
112 const char *get_device_name(VkPhysicalDeviceType type)
113 {
114 switch(type) {
115 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
116 return "integrated GPU";
117 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
118 return "discrete GPU";
119 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
120 return "virtual GPU";
121 case VK_PHYSICAL_DEVICE_TYPE_CPU:
122 return "CPU";
123 default:
124 break;
125 }
126 return "unknown";
127 }
129 const char *get_queue_flag_string(VkQueueFlagBits flags)
130 {
131 static char str[128];
133 str[0] = 0;
134 if(flags & VK_QUEUE_GRAPHICS_BIT) {
135 strcat(str, "graphics ");
136 }
137 if(flags & VK_QUEUE_COMPUTE_BIT) {
138 strcat(str, "compute ");
139 }
140 if(flags & VK_QUEUE_TRANSFER_BIT) {
141 strcat(str, "transfer ");
142 }
143 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
144 strcat(str, "sparse-binding ");
145 }
146 return str;
147 }
149 int ver_major(uint32_t ver)
150 {
151 return (ver >> 22) & 0x3ff;
152 }
154 int ver_minor(uint32_t ver)
155 {
156 return (ver >> 12) & 0x3ff;
157 }
159 int ver_patch(uint32_t ver)
160 {
161 return ver & 0xfff;
162 }