vulkan_test2

view src/vku.c @ 9:5fbfd7f8fc24

minor
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Jun 2018 02:40:22 +0300
parents cec4b0e7fce8
children e17abe477616
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 VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count)
343 {
344 uint32_t nimg;
345 VkImage *images;
347 if(vkGetSwapchainImagesKHR(vkdev, sc, &nimg, 0) != 0) {
348 return 0;
349 }
350 if(!(images = malloc(nimg * sizeof *images))) {
351 return 0;
352 }
353 vkGetSwapchainImagesKHR(vkdev, sc, &nimg, images);
355 if(count) *count = (int)nimg;
356 return images;
357 }
359 int vku_get_next_image(VkSwapchainKHR sc)
360 {
361 uint32_t next;
363 if(vkAcquireNextImageKHR(vkdev, sc, UINT64_MAX, 0, 0, &next) != 0) {
364 return -1;
365 }
366 return (int)next;
367 }
369 void vku_present(VkSwapchainKHR sc, int img_idx)
370 {
371 VkPresentInfoKHR inf;
372 VkResult res;
373 uint32_t index = img_idx;
375 memset(&inf, 0, sizeof inf);
376 inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
377 inf.swapchainCount = 1;
378 inf.pSwapchains = &sc;
379 inf.pImageIndices = &index;
380 inf.pResults = &res;
382 vkQueuePresentKHR(vkq, &inf);
383 }
385 struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
386 {
387 struct vku_buffer *buf;
388 VkBufferCreateInfo binfo;
390 if(!(buf = malloc(sizeof *buf))) {
391 perror("failed to allocate vk_buffer structure");
392 return 0;
393 }
395 memset(&binfo, 0, sizeof binfo);
396 binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
397 binfo.size = sz;
398 binfo.usage = usage;
399 binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
401 if(vkCreateBuffer(vkdev, &binfo, 0, &buf->buf) != 0) {
402 fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
403 return 0;
404 }
405 // TODO back with memory
406 return buf;
407 }
409 void vku_destroy_buffer(struct vku_buffer *buf)
410 {
411 if(buf) {
412 vkDestroyBuffer(vkdev, buf->buf, 0);
413 free(buf);
414 }
415 }
417 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
418 VkBuffer src, int soffs, int size)
419 {
420 VkBufferCopy copy;
421 copy.size = size;
422 copy.srcOffset = soffs;
423 copy.dstOffset = doffs;
425 vkCmdCopyBuffer(cmdbuf, src, dest, 1, &copy);
426 }
428 #ifdef VK_USE_PLATFORM_XLIB_KHR
430 int vku_xlib_usable_visual(Display *dpy, VisualID vid)
431 {
432 return vkGetPhysicalDeviceXlibPresentationSupportKHR(phys_devices[sel_dev],
433 sel_qfamily, dpy, vid);
434 }
436 VkSurfaceKHR vku_xlib_create_surface(Display *dpy, Window win)
437 {
438 VkSurfaceKHR surf;
439 VkXlibSurfaceCreateInfoKHR inf;
441 memset(&inf, 0, sizeof inf);
442 inf.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
443 inf.dpy = dpy;
444 inf.window = win;
446 if(vkCreateXlibSurfaceKHR(vk, &inf, 0, &surf) != 0) {
447 return 0;
448 }
449 return surf;
450 }
452 #endif /* VK_USE_PLATFORM_XLIB_KHR */
454 static const char *get_device_name(VkPhysicalDeviceType type)
455 {
456 switch(type) {
457 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
458 return "integrated GPU";
459 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
460 return "discrete GPU";
461 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
462 return "virtual GPU";
463 case VK_PHYSICAL_DEVICE_TYPE_CPU:
464 return "CPU";
465 default:
466 break;
467 }
468 return "unknown";
469 }
471 static const char *get_mem_prop_flag_string(VkMemoryPropertyFlags flags)
472 {
473 static char str[128];
475 str[0] = 0;
476 if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
477 strcat(str, "device-local ");
478 }
479 if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
480 strcat(str, "host-visible ");
481 }
482 if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
483 strcat(str, "host-coherent ");
484 }
485 if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
486 strcat(str, "host-cached ");
487 }
488 if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
489 strcat(str, "lazily-allocated ");
490 }
492 if(!*str) {
493 strcat(str, "-");
494 }
495 return str;
496 }
498 static const char *get_queue_flag_string(VkQueueFlagBits flags)
499 {
500 static char str[128];
502 str[0] = 0;
503 if(flags & VK_QUEUE_GRAPHICS_BIT) {
504 strcat(str, "graphics ");
505 }
506 if(flags & VK_QUEUE_COMPUTE_BIT) {
507 strcat(str, "compute ");
508 }
509 if(flags & VK_QUEUE_TRANSFER_BIT) {
510 strcat(str, "transfer ");
511 }
512 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
513 strcat(str, "sparse-binding ");
514 }
515 if(!*str) {
516 strcat(str, "-");
517 }
518 return str;
519 }
521 static int ver_major(uint32_t ver)
522 {
523 return (ver >> 22) & 0x3ff;
524 }
526 static int ver_minor(uint32_t ver)
527 {
528 return (ver >> 12) & 0x3ff;
529 }
531 static int ver_patch(uint32_t ver)
532 {
533 return ver & 0xfff;
534 }
536 static const char *mem_size_str(long sz)
537 {
538 static char str[64];
539 static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
540 int uidx = 0;
541 sz *= 10;
543 while(sz >= 10240 && unitstr[uidx + 1]) {
544 sz /= 1024;
545 ++uidx;
546 }
547 sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);
548 return str;
549 }