vulkan_test2

view src/vku.c @ 5:cec4b0e7fce8

created swapchain
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Sep 2017 17:48:18 +0300
parents c31c4115d44a
children 1dd2c7398afc
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include "vku.h"
7 static const char *get_device_name(VkPhysicalDeviceType type);
8 static const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags);
9 static const char *get_queue_flag_string(VkQueueFlagBits flags);
10 static int ver_major(uint32_t ver);
11 static int ver_minor(uint32_t ver);
12 static int ver_patch(uint32_t ver);
13 static const char *mem_size_str(long sz);
15 VkInstance vk;
16 VkDevice vkdev;
17 VkQueue vkq;
19 static VkPhysicalDevice *phys_devices;
20 static int sel_dev, sel_qfamily;
22 static VkExtensionProperties *vkext, *vkdevext;
23 static uint32_t vkext_count, vkdevext_count;
26 int vku_have_extension(const char *name)
27 {
28 int i;
30 if(!vkext) {
31 vkext_count = 0;
32 vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0);
33 if(vkext_count) {
34 if(!(vkext = malloc(vkext_count * sizeof *vkext))) {
35 perror("failed to allocate instance extension list");
36 return 0;
37 }
38 vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext);
40 printf("instance extensions:\n");
41 for(i=0; i<(int)vkext_count; i++) {
42 printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion);
43 }
44 }
45 }
47 for(i=0; i<(int)vkext_count; i++) {
48 if(strcmp(vkext[i].extensionName, name) == 0) {
49 return 1;
50 }
51 }
52 return 0;
53 }
55 int vku_have_device_extension(const char *name)
56 {
57 int i;
59 if(sel_dev < 0) return 0;
61 if(!vkdevext) {
62 vkdevext_count = 0;
63 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0);
64 if(vkdevext_count) {
65 if(!(vkdevext = malloc(vkdevext_count * sizeof *vkdevext))) {
66 perror("failed to allocate device extension list");
67 return 0;
68 }
69 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext);
71 printf("selected device extensions:\n");
72 for(i=0; i<(int)vkdevext_count; i++) {
73 printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion);
74 }
75 }
76 }
78 for(i=0; i<(int)vkdevext_count; i++) {
79 if(strcmp(vkdevext[i].extensionName, name) == 0) {
80 return 1;
81 }
82 }
83 return 0;
84 }
86 int vku_create_dev(void)
87 {
88 int i, j;
89 VkInstanceCreateInfo inst_info;
90 VkDeviceCreateInfo dev_info;
91 VkDeviceQueueCreateInfo queue_info;
92 VkCommandPoolCreateInfo cmdpool_info;
93 uint32_t num_devices;
94 float qprio = 0.0f;
96 static const char *ext_names[] = {
97 #ifdef VK_USE_PLATFORM_XLIB_KHR
98 "VK_KHR_xlib_surface",
99 #endif
100 "VK_KHR_surface"
101 };
102 static const char *devext_names[] = {
103 "VK_KHR_swapchain"
104 };
106 sel_dev = -1;
107 sel_qfamily = -1;
109 for(i=0; i<sizeof ext_names / sizeof *ext_names; i++) {
110 if(!vku_have_extension(ext_names[i])) {
111 fprintf(stderr, "required extension (%s) not found\n", ext_names[i]);
112 return -1;
113 }
114 }
116 memset(&inst_info, 0, sizeof inst_info);
117 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
118 inst_info.ppEnabledExtensionNames = ext_names;
119 inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
121 if(vkCreateInstance(&inst_info, 0, &vk) != 0) {
122 fprintf(stderr, "failed to create vulkan instance\n");
123 return -1;
124 }
125 printf("created vulkan instance\n");
127 if(vkEnumeratePhysicalDevices(vk, &num_devices, 0) != 0) {
128 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
129 return -1;
130 }
131 phys_devices = malloc(num_devices * sizeof *phys_devices);
132 if(vkEnumeratePhysicalDevices(vk, &num_devices, phys_devices) != 0) {
133 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
134 return -1;
135 }
136 printf("found %u physical device(s)\n", (unsigned int)num_devices);
138 for(i=0; i<(int)num_devices; i++) {
139 VkPhysicalDeviceProperties dev_prop;
140 VkPhysicalDeviceMemoryProperties mem_prop;
141 VkQueueFamilyProperties *qprop;
142 uint32_t qprop_count;
144 vkGetPhysicalDeviceProperties(phys_devices[i], &dev_prop);
146 printf("Device %d: %s\n", i, dev_prop.deviceName);
147 printf(" type: %s\n", get_device_name(dev_prop.deviceType));
148 printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
149 ver_patch(dev_prop.apiVersion));
150 printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
151 ver_patch(dev_prop.driverVersion));
152 printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
155 vkGetPhysicalDeviceMemoryProperties(phys_devices[i], &mem_prop);
156 printf(" %d memory heaps:\n", mem_prop.memoryHeapCount);
157 for(j=0; j<mem_prop.memoryHeapCount; j++) {
158 VkMemoryHeap heap = mem_prop.memoryHeaps[j];
159 printf(" Heap %d - size: %s, flags: %s\n", j, mem_size_str(heap.size),
160 heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
161 }
162 printf(" %d memory types:\n", mem_prop.memoryTypeCount);
163 for(j=0; j<mem_prop.memoryTypeCount; j++) {
164 VkMemoryType type = mem_prop.memoryTypes[j];
165 printf(" Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
166 get_mem_prop_flag_string(type.propertyFlags));
167 }
169 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, 0);
170 if(qprop_count <= 0) {
171 continue;
172 }
173 qprop = malloc(qprop_count * sizeof *qprop);
174 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop);
176 for(j=0; j<qprop_count; j++) {
177 printf(" Queue family %d:\n", j);
178 printf(" flags: %s\n", get_queue_flag_string(qprop[j].queueFlags));
179 printf(" num queues: %u\n", qprop[j].queueCount);
181 if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
182 sel_dev = i;
183 sel_qfamily = j;
184 }
185 }
186 free(qprop);
187 }
189 if(sel_dev < 0 || sel_qfamily < 0) {
190 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
191 vkDestroyDevice(vkdev, 0);
192 return -1;
193 }
195 for(i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
196 if(!vku_have_device_extension(devext_names[i])) {
197 fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
198 ext_names[i], sel_dev);
199 return -1;
200 }
201 }
203 /* create device & command queue */
204 memset(&queue_info, 0, sizeof queue_info);
205 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
206 queue_info.queueFamilyIndex = sel_qfamily;
207 queue_info.queueCount = 1;
208 queue_info.pQueuePriorities = &qprio;
210 memset(&dev_info, 0, sizeof dev_info);
211 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
212 dev_info.queueCreateInfoCount = 1;
213 dev_info.pQueueCreateInfos = &queue_info;
214 dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
215 dev_info.ppEnabledExtensionNames = devext_names;
217 if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vkdev) != 0) {
218 fprintf(stderr, "failed to create device %d\n", sel_dev);
219 return -1;
220 }
221 printf("created device %d\n", sel_dev);
223 vkGetDeviceQueue(vkdev, sel_qfamily, 0, &vkq);
225 /* create command buffer pool */
226 memset(&cmdpool_info, 0, sizeof cmdpool_info);
227 cmdpool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
228 cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
229 cmdpool_info.queueFamilyIndex = sel_qfamily;
231 if(vkCreateCommandPool(vkdev, &cmdpool_info, 0, &vkcmdpool) != 0) {
232 fprintf(stderr, "failed to get command quque!\n");
233 return -1;
234 }
236 if(!(vkcmdbuf = vku_alloc_cmdbuf(vkcmdpool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
237 fprintf(stderr, "failed to create primary command buffer\n");
238 return -1;
239 }
241 return 0;
242 }
244 void vku_cleanup(void)
245 {
246 if(vk) {
247 vkDeviceWaitIdle(vkdev);
248 vkDestroyCommandPool(vkdev, vkcmdpool, 0);
249 vkDestroyDevice(vkdev, 0);
250 vkDestroyInstance(vk, 0);
251 vk = 0;
252 }
254 free(phys_devices);
255 phys_devices = 0;
256 }
258 VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
259 {
260 VkCommandBuffer cmdbuf;
261 VkCommandBufferAllocateInfo inf;
263 memset(&inf, 0, sizeof inf);
264 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
265 inf.commandPool = pool;
266 inf.level = level;
267 inf.commandBufferCount = 1;
269 if(vkAllocateCommandBuffers(vkdev, &inf, &cmdbuf) != 0) {
270 return 0;
271 }
272 return cmdbuf;
273 }
275 void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf)
276 {
277 vkFreeCommandBuffers(vkdev, pool, 1, &buf);
278 }
280 void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
281 {
282 VkCommandBufferBeginInfo inf;
284 memset(&inf, 0, sizeof inf);
285 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
286 inf.flags = flags;
288 vkBeginCommandBuffer(buf, &inf);
289 }
292 void vku_end_cmdbuf(VkCommandBuffer buf)
293 {
294 vkEndCommandBuffer(buf);
295 }
297 void vku_reset_cmdbuf(VkCommandBuffer buf)
298 {
299 vkResetCommandBuffer(buf, 0);
300 }
302 void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
303 {
304 VkSubmitInfo info;
306 memset(&info, 0, sizeof info);
307 info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
308 info.commandBufferCount = 1;
309 info.pCommandBuffers = &buf;
311 vkQueueSubmit(q, 1, &info, done_fence);
312 }
314 VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
315 VkPresentModeKHR pmode, VkSwapchainKHR prev)
316 {
317 VkSwapchainKHR sc;
318 VkSwapchainCreateInfoKHR inf;
320 memset(&inf, 0, sizeof inf);
321 inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
322 inf.surface = surf;
323 inf.minImageCount = n;
324 inf.imageFormat = VK_FORMAT_B8G8R8A8_UNORM; /* TODO enumerate and choose */
325 inf.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
326 inf.imageExtent.width = xsz;
327 inf.imageExtent.height = ysz;
328 inf.imageArrayLayers = 1;
329 inf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
330 inf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* XXX make this an option? */
331 inf.preTransform = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
332 inf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
333 inf.presentMode = pmode;
334 inf.oldSwapchain = prev;
336 if(vkCreateSwapchainKHR(vkdev, &inf, 0, &sc) != 0) {
337 return 0;
338 }
339 return sc;
340 }
342 struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
343 {
344 struct vku_buffer *buf;
345 VkBufferCreateInfo binfo;
347 if(!(buf = malloc(sizeof *buf))) {
348 perror("failed to allocate vk_buffer structure");
349 return 0;
350 }
352 memset(&binfo, 0, sizeof binfo);
353 binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
354 binfo.size = sz;
355 binfo.usage = usage;
356 binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
358 if(vkCreateBuffer(vkdev, &binfo, 0, &buf->buf) != 0) {
359 fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
360 return 0;
361 }
362 // TODO back with memory
363 return buf;
364 }
366 void vku_destroy_buffer(struct vku_buffer *buf)
367 {
368 if(buf) {
369 vkDestroyBuffer(vkdev, buf->buf, 0);
370 free(buf);
371 }
372 }
374 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
375 VkBuffer src, int soffs, int size)
376 {
377 VkBufferCopy copy;
378 copy.size = size;
379 copy.srcOffset = soffs;
380 copy.dstOffset = doffs;
382 vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
383 }
385 #ifdef VK_USE_PLATFORM_XLIB_KHR
387 int vku_xlib_usable_visual(Display *dpy, VisualID vid)
388 {
389 return vkGetPhysicalDeviceXlibPresentationSupportKHR(phys_devices[sel_dev],
390 sel_qfamily, dpy, vid);
391 }
393 VkSurfaceKHR vku_xlib_create_surface(Display *dpy, Window win)
394 {
395 VkSurfaceKHR surf;
396 VkXlibSurfaceCreateInfoKHR inf;
398 memset(&inf, 0, sizeof inf);
399 inf.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
400 inf.dpy = dpy;
401 inf.window = win;
403 if(vkCreateXlibSurfaceKHR(vk, &inf, 0, &surf) != 0) {
404 return 0;
405 }
406 return surf;
407 }
409 #endif /* VK_USE_PLATFORM_XLIB_KHR */
411 static const char *get_device_name(VkPhysicalDeviceType type)
412 {
413 switch(type) {
414 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
415 return "integrated GPU";
416 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
417 return "discrete GPU";
418 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
419 return "virtual GPU";
420 case VK_PHYSICAL_DEVICE_TYPE_CPU:
421 return "CPU";
422 default:
423 break;
424 }
425 return "unknown";
426 }
428 static const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags)
429 {
430 static char str[128];
432 str[0] = 0;
433 if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
434 strcat(str, "device-local ");
435 }
436 if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
437 strcat(str, "host-visible ");
438 }
439 if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
440 strcat(str, "host-coherent ");
441 }
442 if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
443 strcat(str, "host-cached ");
444 }
445 if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
446 strcat(str, "lazily-allocated ");
447 }
449 if(!*str) {
450 strcat(str, "-");
451 }
452 return str;
453 }
455 static const char *get_queue_flag_string(VkQueueFlagBits flags)
456 {
457 static char str[128];
459 str[0] = 0;
460 if(flags & VK_QUEUE_GRAPHICS_BIT) {
461 strcat(str, "graphics ");
462 }
463 if(flags & VK_QUEUE_COMPUTE_BIT) {
464 strcat(str, "compute ");
465 }
466 if(flags & VK_QUEUE_TRANSFER_BIT) {
467 strcat(str, "transfer ");
468 }
469 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
470 strcat(str, "sparse-binding ");
471 }
472 if(!*str) {
473 strcat(str, "-");
474 }
475 return str;
476 }
478 static int ver_major(uint32_t ver)
479 {
480 return (ver >> 22) & 0x3ff;
481 }
483 static int ver_minor(uint32_t ver)
484 {
485 return (ver >> 12) & 0x3ff;
486 }
488 static int ver_patch(uint32_t ver)
489 {
490 return ver & 0xfff;
491 }
493 static const char *mem_size_str(long sz)
494 {
495 static char str[64];
496 static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
497 int uidx = 0;
498 sz *= 10;
500 while(sz >= 10240 && unitstr[uidx + 1]) {
501 sz /= 1024;
502 ++uidx;
503 }
504 sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
505 return str;
506 }