vulkan_test2

view main.c @ 2:5b2ae06283aa

more queries
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 18 Dec 2016 09:15:16 +0200
parents 8cb584143df3
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_mem_prop_flag_string(VkMemoryPropertyFlags flags);
9 const char *get_queue_flag_string(VkQueueFlagBits flags);
10 int ver_major(uint32_t ver);
11 int ver_minor(uint32_t ver);
12 int ver_patch(uint32_t ver);
13 const char *mem_size_str(long sz);
15 int main(void)
16 {
17 int i, j;
18 VkInstance vk;
19 VkInstanceCreateInfo inst_info;
20 VkPhysicalDevice *devices;
21 VkDevice vkdev;
22 VkDeviceCreateInfo dev_info;
23 VkDeviceQueueCreateInfo queue_info;
24 uint32_t num_devices;
25 int sel_dev = -1;
26 int sel_qfamily = -1;
27 float qprio = 0.0f;
29 memset(&inst_info, 0, sizeof inst_info);
30 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
32 if(vkCreateInstance(&inst_info, 0, &vk) != 0) {
33 fprintf(stderr, "failed to create vulkan instance\n");
34 return 1;
35 }
36 printf("created vulkan instance\n");
38 if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) {
39 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
40 return 1;
41 }
42 devices = alloca(num_devices * sizeof *devices);
43 if(vkEnumeratePhysicalDevices(vk, &num_devices, devices) != 0) {
44 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
45 return 1;
46 }
47 printf("found %u physical device(s)\n", (unsigned int)num_devices);
49 for(i=0; i<(int)num_devices; i++) {
50 VkPhysicalDeviceProperties dev_prop;
51 VkPhysicalDeviceMemoryProperties mem_prop;
52 VkQueueFamilyProperties *qprop;
53 uint32_t qprop_count;
55 vkGetPhysicalDeviceProperties(devices[i], &dev_prop);
57 printf("Device %d: %s\n", i, dev_prop.deviceName);
58 printf(" type: %s\n", get_device_name(dev_prop.deviceType));
59 printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
60 ver_patch(dev_prop.apiVersion));
61 printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
62 ver_patch(dev_prop.driverVersion));
63 printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
66 vkGetPhysicalDeviceMemoryProperties(devices[i], &mem_prop);
67 printf(" %d memory heaps:\n", mem_prop.memoryHeapCount);
68 for(j=0; j<mem_prop.memoryHeapCount; j++) {
69 VkMemoryHeap heap = mem_prop.memoryHeaps[j];
70 printf(" Heap %d - size: %s, flags: %s\n", j, mem_size_str(heap.size),
71 heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
72 }
73 printf(" %d memory types:\n", mem_prop.memoryTypeCount);
74 for(j=0; j<mem_prop.memoryTypeCount; j++) {
75 VkMemoryType type = mem_prop.memoryTypes[j];
76 printf(" Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
77 get_mem_prop_flag_string(type.propertyFlags));
78 }
80 vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, 0);
81 if(qprop_count <= 0) {
82 continue;
83 }
84 qprop = malloc(qprop_count * sizeof *qprop);
85 vkGetPhysicalDeviceQueueFamilyProperties(devices[i], &qprop_count, qprop);
87 for(j=0; j<qprop_count; j++) {
88 printf(" Queue family %d:\n", j);
89 printf(" flags: %s\n", get_queue_flag_string(qprop[j].queueFlags));
90 printf(" num queues: %u\n", qprop[j].queueCount);
92 if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
93 sel_dev = i;
94 sel_qfamily = j;
95 }
96 }
97 free(qprop);
98 }
100 if(sel_dev < 0 || sel_qfamily < 0) {
101 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
102 vkDestroyDevice(vkdev, 0);
103 return 1;
104 }
106 // create device & command queue
107 memset(&queue_info, 0, sizeof queue_info);
108 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
109 queue_info.queueFamilyIndex = sel_qfamily;
110 queue_info.queueCount = 1;
111 queue_info.pQueuePriorities = &qprio;
113 memset(&dev_info, 0, sizeof dev_info);
114 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
115 dev_info.queueCreateInfoCount = 1;
116 dev_info.pQueueCreateInfos = &queue_info;
118 if(vkCreateDevice(devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
119 fprintf(stderr, "failed to create device %d\n", sel_dev);
120 return 1;
121 }
122 printf("created device %d\n", sel_dev);
124 vkDestroyDevice(vkdev, 0);
125 vkDestroyInstance(vk, 0);
126 return 0;
127 }
130 const char *get_device_name(VkPhysicalDeviceType type)
131 {
132 switch(type) {
133 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
134 return "integrated GPU";
135 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
136 return "discrete GPU";
137 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
138 return "virtual GPU";
139 case VK_PHYSICAL_DEVICE_TYPE_CPU:
140 return "CPU";
141 default:
142 break;
143 }
144 return "unknown";
145 }
147 const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags)
148 {
149 static char str[128];
151 str[0] = 0;
152 if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
153 strcat(str, "device-local ");
154 }
155 if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
156 strcat(str, "host-visible ");
157 }
158 if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
159 strcat(str, "host-coherent ");
160 }
161 if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
162 strcat(str, "host-cached ");
163 }
164 if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
165 strcat(str, "lazily-allocated ");
166 }
168 if(!*str) {
169 strcat(str, "-");
170 }
171 return str;
172 }
174 const char *get_queue_flag_string(VkQueueFlagBits flags)
175 {
176 static char str[128];
178 str[0] = 0;
179 if(flags & VK_QUEUE_GRAPHICS_BIT) {
180 strcat(str, "graphics ");
181 }
182 if(flags & VK_QUEUE_COMPUTE_BIT) {
183 strcat(str, "compute ");
184 }
185 if(flags & VK_QUEUE_TRANSFER_BIT) {
186 strcat(str, "transfer ");
187 }
188 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
189 strcat(str, "sparse-binding ");
190 }
191 if(!*str) {
192 strcat(str, "-");
193 }
194 return str;
195 }
197 int ver_major(uint32_t ver)
198 {
199 return (ver >> 22) & 0x3ff;
200 }
202 int ver_minor(uint32_t ver)
203 {
204 return (ver >> 12) & 0x3ff;
205 }
207 int ver_patch(uint32_t ver)
208 {
209 return ver & 0xfff;
210 }
212 const char *mem_size_str(long sz)
213 {
214 static char str[64];
215 static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
216 int uidx = 0;
217 sz *= 10;
219 while(sz >= 10240 && unitstr[uidx + 1]) {
220 sz /= 1024;
221 ++uidx;
222 }
223 sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
224 return str;
225 }