1 #include <gmath/gmath.h>
2 #include <vulkan/vulkan.h>
12 /* global variables */
13 VkPhysicalDevice vk_physical;
15 VkCommandPool vk_pool;
17 VkSurfaceKHR vk_surface;
18 VkSwapchainKHR vk_swapchain;
20 VkPipeline *vkgparent_pipeline;
21 VkFramebuffer *vkfbufs;
24 VkCommandBuffer vkcmdbuf; /* primary command buffer */
27 VkSemaphore vk_img_avail_sema;
28 VkSemaphore vk_rend_done_sema;
29 VkImage *vkswapchain_images;
30 VkImageView *vkswapchain_views;
31 int vknum_swapchain_images;
32 int vk_curr_swapchain_image;
34 /* static functions */
35 static const char *get_device_name_str(VkPhysicalDeviceType type);
36 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags);
37 static const char *get_queue_flags_str(VkQueueFlags flags);
38 static const char *get_mem_size_str(long sz);
39 static int ver_major(uint32_t ver);
40 static int ver_minor(uint32_t ver);
41 static int ver_patch(uint32_t ver);
43 /* static variables */
44 static VkPhysicalDevice *phys_devices;
47 static int sel_qfamily;
49 static VkExtensionProperties *vkext, *vkdevext;
50 static uint32_t vkext_count, vkdevext_count;
52 bool vku_have_extension(const char *name)
56 vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0);
58 vkext = new VkExtensionProperties[vkext_count];
59 vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext);
61 printf("instance extensions:\n");
62 for(int i=0; i<(int)vkext_count; i++) {
63 printf(" %s (ver: %u)\n", vkext[i].extensionName, (unsigned int)vkext[i].specVersion);
68 for(int i=0; i<(int)vkext_count; i++) {
69 if(strcmp(vkext[i].extensionName, name) == 0) {
76 bool vku_have_device_extension(const char *name)
78 if(sel_dev < 0) return false;
82 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0);
84 vkdevext = new VkExtensionProperties[vkdevext_count];
85 vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext);
87 printf("selected device extensions:\n");
88 for(int i=0; i<(int)vkdevext_count; i++) {
89 printf(" %s (ver: %u)\n", vkdevext[i].extensionName, (unsigned int)vkdevext[i].specVersion);
94 for(int i=0; i<(int)vkdevext_count; i++) {
95 if(strcmp(vkdevext[i].extensionName, name) == 0) {
102 bool vku_create_device()
104 VkInstanceCreateInfo inst_info;
105 VkDeviceCreateInfo dev_info;
106 VkDeviceQueueCreateInfo queue_info;
107 VkCommandPoolCreateInfo cmdpool_info;
108 uint32_t num_devices;
111 static const char *ext_names[] = {
112 "VK_KHR_xcb_surface",
116 static const char *devext_names[] = {
123 for(unsigned int i=0; i<sizeof ext_names / sizeof *ext_names; i++) {
124 if(!vku_have_extension(ext_names[i])) {
125 fprintf(stderr, "required extension (%s) not found\n", ext_names[i]);
129 memset(&inst_info, 0, sizeof inst_info);
130 inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
131 inst_info.ppEnabledExtensionNames = ext_names;
132 inst_info.enabledExtensionCount = sizeof ext_names / sizeof *ext_names;
134 if(vkCreateInstance(&inst_info, 0, &vkinst) != 0) {
135 fprintf(stderr, "failed to create vulkan instance\n");
138 printf("created vulkan instance\n");
139 if(vkEnumeratePhysicalDevices(vkinst, &num_devices, 0) != 0) {
140 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
143 phys_devices = new VkPhysicalDevice[num_devices];
144 if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) {
145 fprintf(stderr, "failed to enumerate vulkan physical devices\n");
148 printf("found %u physical device(s)\n", (unsigned int)num_devices);
150 for(int i=0; i<(int)num_devices; i++) {
151 VkPhysicalDeviceProperties dev_prop;
152 VkPhysicalDeviceMemoryProperties mem_prop;
153 VkQueueFamilyProperties *qprop;
154 uint32_t qprop_count;
156 vkGetPhysicalDeviceProperties(phys_devices[i], &dev_prop);
158 printf("Device %d: %s\n", i, dev_prop.deviceName);
159 printf(" type: %s\n", get_device_name_str(dev_prop.deviceType));
160 printf(" API version: %d.%d.%d\n", ver_major(dev_prop.apiVersion), ver_minor(dev_prop.apiVersion),
161 ver_patch(dev_prop.apiVersion));
162 printf(" driver version: %d.%d.%d\n", ver_major(dev_prop.driverVersion), ver_minor(dev_prop.driverVersion),
163 ver_patch(dev_prop.driverVersion));
164 printf(" vendor id: %x device id: %x\n", dev_prop.vendorID, dev_prop.deviceID);
167 vkGetPhysicalDeviceMemoryProperties(phys_devices[i], &mem_prop);
168 printf(" %d memory heaps:\n", mem_prop.memoryHeapCount);
169 for(unsigned int j=0; j<mem_prop.memoryHeapCount; j++) {
170 VkMemoryHeap heap = mem_prop.memoryHeaps[j];
171 printf(" Heap %d - size: %s, flags: %s\n", j, get_mem_size_str(heap.size),
172 heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT ? "device-local" : "-");
174 printf(" %d memory types:\n", mem_prop.memoryTypeCount);
175 for(unsigned int j=0; j<mem_prop.memoryTypeCount; j++) {
176 VkMemoryType type = mem_prop.memoryTypes[j];
177 printf(" Type %d - heap: %d, flags: %s\n", j, type.heapIndex,
178 get_memtype_flags_str(type.propertyFlags));
181 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, 0);
182 if(qprop_count <= 0) {
185 qprop = new VkQueueFamilyProperties[qprop_count];
186 vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop);
188 for(unsigned int j=0; j<qprop_count; j++) {
189 printf(" Queue family %d:\n", j);
190 printf(" flags: %s\n", get_queue_flags_str(qprop[j].queueFlags));
191 printf(" num queues: %u\n", qprop[j].queueCount);
193 if(qprop[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
201 if(sel_dev < 0 || sel_qfamily < 0) {
202 fprintf(stderr, "failed to find any device with a graphics-capable command queue\n");
203 vkDestroyDevice(vk_device, 0);
207 for(unsigned int i=0; i<sizeof devext_names / sizeof *devext_names; i++) {
208 if(!vku_have_device_extension(devext_names[i])) {
209 fprintf(stderr, "required extension (%s) not found on the selected device (%d)\n",
210 ext_names[i], sel_dev);
215 /* create device & command queue */
216 memset(&queue_info, 0, sizeof queue_info);
217 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
218 queue_info.queueFamilyIndex = sel_qfamily;
219 queue_info.queueCount = 1;
220 queue_info.pQueuePriorities = &qprio;
222 memset(&dev_info, 0, sizeof dev_info);
223 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
224 dev_info.queueCreateInfoCount = 1;
225 dev_info.pQueueCreateInfos = &queue_info;
226 dev_info.enabledExtensionCount = sizeof devext_names / sizeof *devext_names;
227 dev_info.ppEnabledExtensionNames = devext_names;
229 if(vkCreateDevice(phys_devices[sel_dev], &dev_info, 0, &vk_device) != 0) {
230 fprintf(stderr, "failed to create device %d\n", sel_dev);
233 printf("created device %d\n", sel_dev);
235 vk_physical = phys_devices[sel_dev];
236 vkqfamily = sel_qfamily;
238 vkGetDeviceQueue(vk_device, sel_qfamily, 0, &vk_queue);
240 /* create command buffer pool */
241 memset(&cmdpool_info, 0, sizeof cmdpool_info);
242 cmdpool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
243 cmdpool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
244 cmdpool_info.queueFamilyIndex = sel_qfamily;
246 if(vkCreateCommandPool(vk_device, &cmdpool_info, 0, &vk_pool) != 0) {
247 fprintf(stderr, "failed to get command queue!\n");
251 /* if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) {
252 fprintf(stderr, "failed to create primary command buffer\n");
262 vkDeviceWaitIdle(vk_device);
263 vkDestroyCommandPool(vk_device, vk_pool, 0);
265 vkDestroySemaphore(vk_device, vk_img_avail_sema, 0);
266 vkDestroySemaphore(vk_device, vk_rend_done_sema, 0);
268 vkDestroyDevice(vk_device, 0);
269 vkDestroyInstance(vkinst, 0);
273 delete [] phys_devices;
277 bool vku_create_semaphores()
279 VkSemaphoreCreateInfo sinf;
280 memset(&sinf, 0, sizeof sinf);
282 sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
283 if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_img_avail_sema) != VK_SUCCESS) {
284 fprintf(stderr, "Failed to create semaphore\n");
288 if(vkCreateSemaphore(vk_device, &sinf, 0, &vk_rend_done_sema) != VK_SUCCESS) {
289 fprintf(stderr, "Failed to create semaphore\n");
296 VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level)
298 VkCommandBuffer cmdbuf;
299 VkCommandBufferAllocateInfo inf;
301 memset(&inf, 0, sizeof inf);
302 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
303 inf.commandPool = pool;
305 inf.commandBufferCount = 1;
307 if(vkAllocateCommandBuffers(vk_device, &inf, &cmdbuf) != 0) {
313 void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf)
315 vkFreeCommandBuffers(vk_device, pool, 1, &buf);
318 void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags)
320 VkCommandBufferBeginInfo inf;
322 memset(&inf, 0, sizeof inf);
323 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
326 vkBeginCommandBuffer(buf, &inf);
330 void vku_end_cmdbuf(VkCommandBuffer buf)
332 vkEndCommandBuffer(buf);
335 void vku_reset_cmdbuf(VkCommandBuffer buf)
337 vkResetCommandBuffer(buf, 0);
340 void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence)
344 memset(&info, 0, sizeof info);
345 info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
346 info.commandBufferCount = 1;
347 info.pCommandBuffers = &buf;
349 vkQueueSubmit(q, 1, &info, done_fence);
352 VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n,
353 VkPresentModeKHR pmode, VkSwapchainKHR prev)
355 VkSurfaceCapabilitiesKHR scap;
356 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical, surf, &scap);
358 VkPhysicalDeviceMemoryProperties mprops;
359 vkGetPhysicalDeviceMemoryProperties(vk_physical, &mprops);
362 vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, surf, &pmcnt, 0);
363 VkPresentModeKHR scpm = VK_PRESENT_MODE_IMMEDIATE_KHR;
368 // VkSurfaceTransformFlagBitsKHR pretransf;
369 // if(scap.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
370 // pretransf = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
372 // pretransf = scap.currentTransform;
376 VkSwapchainCreateInfoKHR inf;
378 VkSurfaceFormatKHR sformat;
379 sformat.format = VK_FORMAT_B8G8R8A8_UNORM;
381 memset(&inf, 0, sizeof inf);
382 inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
384 inf.minImageCount = n;
385 inf.imageFormat = sformat.format; /* TODO enumerate and choose */
386 inf.imageColorSpace = sformat.colorSpace;
387 inf.imageExtent = scext;
388 inf.imageArrayLayers = 1;
389 inf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
390 inf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* XXX make this an option? */
391 inf.preTransform = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR;
392 inf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
393 inf.presentMode = pmode;
394 inf.oldSwapchain = VK_NULL_HANDLE;
395 inf.clipped = VK_TRUE;
397 if(vkCreateSwapchainKHR(vk_device, &inf, 0, &sc) != 0) {
403 VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count)
408 if(vkGetSwapchainImagesKHR(vk_device, sc, &nimg, 0) != 0) {
411 images = new VkImage[nimg];
412 vkGetSwapchainImagesKHR(vk_device, sc, &nimg, images);
414 if(count) *count = (int)nimg;
418 VkImageView *vku_create_image_views(VkImage *images, int count)
422 iviews = new VkImageView[count];
423 for(int i=0; i<count; i++) {
424 VkImageViewCreateInfo inf;
425 memset(&inf, 0, sizeof inf);
427 inf.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
428 inf.image = images[i];
429 inf.viewType = VK_IMAGE_VIEW_TYPE_2D;
430 inf.format = VK_FORMAT_B8G8R8A8_UNORM; //TODO
431 inf.components.r = inf.components.g = inf.components.b =
432 inf.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
433 inf.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
434 inf.subresourceRange.levelCount = 1;
435 inf.subresourceRange.layerCount = 1;
437 if(vkCreateImageView(vk_device, &inf, 0, iviews) != 0) {
438 fprintf(stderr, "Failed to create image views.\n");
447 int vku_get_next_image(VkSwapchainKHR sc)
451 if(vkAcquireNextImageKHR(vk_device, sc, UINT64_MAX, 0, 0, &next) != 0) {
457 void vku_present(VkSwapchainKHR sc, int img_idx)
459 VkPresentInfoKHR inf;
461 uint32_t index = img_idx;
463 memset(&inf, 0, sizeof inf);
464 inf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
465 inf.swapchainCount = 1;
466 inf.pSwapchains = ≻
467 inf.pImageIndices = &index;
470 vkQueuePresentKHR(vk_queue, &inf);
473 bool vku_create_renderpass()
475 VkAttachmentDescription attachments[2];
476 memset(&attachments, 0, 2 * sizeof *attachments);
479 attachments[0].format = VK_FORMAT_B8G8R8A8_UNORM;
480 attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
481 attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
482 attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
483 attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
484 attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
485 attachments[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
486 attachments[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
489 attachments[1].format = VK_FORMAT_D32_SFLOAT_S8_UINT; //TODO
490 attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
491 attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
492 attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
493 attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
494 attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
495 attachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
496 attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
498 VkAttachmentReference color_ref;
499 color_ref.attachment = 0;
500 color_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
502 VkAttachmentReference depth_ref;
503 depth_ref.attachment = 1;
504 depth_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
506 VkSubpassDescription subpass_desc;
507 memset(&subpass_desc, 0, sizeof subpass_desc);
509 subpass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
510 subpass_desc.colorAttachmentCount = 1;
511 subpass_desc.pColorAttachments = &color_ref;
512 subpass_desc.pDepthStencilAttachment = &depth_ref;
514 VkRenderPassCreateInfo inf;
515 memset(&inf, 0, sizeof inf);
517 inf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
518 inf.attachmentCount = 2;
519 inf.pAttachments = attachments;
520 inf.subpassCount = 1;
521 inf.pSubpasses = &subpass_desc;
523 if(vkCreateRenderPass(vk_device, &inf, 0, &vkrpass) != VK_SUCCESS) {
530 bool vku_create_framebuffers(VkImageView *image_views, int count, int w, int h)
533 vkfbufs = new VkFramebuffer[count];
535 VkFramebufferCreateInfo inf;
536 memset(&inf, 0, sizeof inf);
538 inf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
539 inf.renderPass = vkrpass;
540 inf.attachmentCount = count;
541 inf.pAttachments = image_views;
546 for(int i=0; i<count; i++) {
547 if(vkCreateFramebuffer(vk_device, &inf, 0, &vkfbufs[i]) != VK_SUCCESS) {
548 fprintf(stderr, "Failed to create framebuffer for image view: %d\n", i);
556 struct vku_buffer *vku_create_buffer(int sz, unsigned int usage)
558 struct vku_buffer *buf;
559 VkBufferCreateInfo binfo;
561 buf = new vku_buffer;
563 memset(&binfo, 0, sizeof binfo);
564 binfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
567 binfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
569 if(vkCreateBuffer(vk_device, &binfo, 0, &buf->buf) != 0) {
570 fprintf(stderr, "failed to create %d byte buffer (usage: %x)\n", sz, usage);
573 // TODO back with memory
577 void vku_destroy_buffer(struct vku_buffer *buf)
580 vkDestroyBuffer(vk_device, buf->buf, 0);
585 void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs,
586 VkBuffer src, int soffs, int size)
590 copy.srcOffset = soffs;
591 copy.dstOffset = doffs;
593 vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©);
596 const char *vku_get_vulkan_error_str(VkResult error)
601 errmsg = std::string("VK_SUCCESS");
604 errmsg = std::string("VK_NOT_READY");
607 errmsg = std::string("VK_TIMEOUT");
610 errmsg = std::string("VK_EVENT_SET");
613 errmsg = std::string("VK_EVENT_RESET");
616 errmsg = std::string("VK_EVENT");
618 case VK_ERROR_OUT_OF_HOST_MEMORY:
619 errmsg = std::string("VK_ERROR_OUT_OF_HOST_MEMORY");
621 case VK_ERROR_OUT_OF_DEVICE_MEMORY:
622 errmsg = std::string("VK_ERROR_OUT_OF_DEVICE_MEMORY");
624 case VK_ERROR_INITIALIZATION_FAILED:
625 errmsg = std::string("VK_ERROR_INITIALIZATION_FAILED");
627 case VK_ERROR_DEVICE_LOST:
628 errmsg = std::string("VK_ERROR_DEVICE_LOST");
630 case VK_ERROR_MEMORY_MAP_FAILED:
631 errmsg = std::string("VK_ERROR_MEMORY_MAP_FAILED");
633 case VK_ERROR_LAYER_NOT_PRESENT:
634 errmsg = std::string("VK_ERROR_LAYER_NOT_PRESENT");
636 case VK_ERROR_EXTENSION_NOT_PRESENT:
637 errmsg = std::string("VK_ERROR_EXTENSION_NOT_PRESENT");
639 case VK_ERROR_FEATURE_NOT_PRESENT:
640 errmsg = std::string("VK_ERROR_FEATURE_NOT_PRESENT");
641 case VK_ERROR_INCOMPATIBLE_DRIVER:
642 errmsg = std::string("VK_ERROR_INCOMPATIBLE_DRIVER");
644 case VK_ERROR_TOO_MANY_OBJECTS:
645 errmsg = std::string("VK_ERROR_TOO_MANY_OBJECTS");
647 case VK_ERROR_FORMAT_NOT_SUPPORTED:
648 errmsg = std::string("VK_ERROR_FORMAT_NOT_SUPPORTED");
650 case VK_ERROR_FRAGMENTED_POOL:
651 errmsg = std::string("VK_ERROR_FRAGMENTED_POOL");
654 errmsg = std::string("UNKNOWN");
658 return errmsg.c_str();
661 bool vku_create_graphics_pipeline(VkPipelineLayout *layout)
663 VkGraphicsPipelineCreateInfo inf;
664 memset(&inf, 0, sizeof inf);
666 inf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
667 inf.layout = *layout;
668 inf.renderPass = vkrpass;
672 /* how primitives are assembled */
673 VkPipelineInputAssemblyStateCreateInfo ias;
674 memset(&ias, 0, sizeof ias);
675 ias.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
676 ias.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
679 VkPipelineRasterizationStateCreateInfo rsi;
680 memset(&rsi, 0, sizeof rsi);
681 rsi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
682 rsi.polygonMode = VK_POLYGON_MODE_FILL;
683 rsi.cullMode = VK_CULL_MODE_BACK_BIT;
684 rsi.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
685 rsi.lineWidth = 1.0f;
688 VkPipelineColorBlendAttachmentState bas[1];
689 memset(&bas[0], 0, sizeof bas[0]);
691 VkPipelineColorBlendStateCreateInfo cbs;
692 memset(&cbs, 0, sizeof cbs);
693 cbs.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
694 cbs.attachmentCount = 1;
695 cbs.pAttachments = bas;
697 /* number of viewport and scissors in this pipeline */
698 VkPipelineViewportStateCreateInfo vs;
699 memset(&vs, 0, sizeof vs);
700 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
701 vs.viewportCount = 1;
704 /* dynamic states: that can be changed later */
705 // std::vector<VkDynamicState> ds_enabled;
706 // ds_enabled.push_back(VK_DYNAMIC_STATE_VIEWPORT);
707 //ds_enabled.push_back(VK_DYNAMIC_STATE_SCISSOR);
708 // VkPipelineDynamicStateCreateInfo ds;
709 // ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
710 // ds.pDynamicStates = ds_enabled.data();
711 // ds.dynamicStateCount = static_cast<uint32_t>(ds_enabled.size());
714 VkPipelineDepthStencilStateCreateInfo dsi;
715 memset(&dsi, 0, sizeof dsi);
716 dsi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
717 dsi.depthTestEnable = VK_TRUE;
718 dsi.depthWriteEnable = VK_TRUE;
719 dsi.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
720 dsi.back.failOp = VK_STENCIL_OP_KEEP;
721 dsi.back.passOp = VK_STENCIL_OP_KEEP;
722 dsi.back.compareOp = VK_COMPARE_OP_ALWAYS;
723 dsi.front = dsi.back;
725 /* multisampling - must be set even if not used */
726 VkPipelineMultisampleStateCreateInfo msi;
727 memset(&msi, 0, sizeof msi);
728 msi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
729 msi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
731 /* vertex input descriptions */
732 VkVertexInputBindingDescription vib;
733 memset(&vib, 0, sizeof vib);
734 vib.stride = sizeof(Vec3);
735 vib.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
737 /* input attr bindings */
738 VkVertexInputAttributeDescription via[2];
739 memset(&via, 0, sizeof via);
740 via[0].format = VK_FORMAT_R32G32B32A32_SFLOAT;
745 static const char *get_device_name_str(VkPhysicalDeviceType type)
748 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
749 return "integrated GPU";
750 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
751 return "discrete GPU";
752 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
753 return "virtual GPU";
754 case VK_PHYSICAL_DEVICE_TYPE_CPU:
762 static const char *get_memtype_flags_str(VkMemoryPropertyFlags flags)
764 static char str[128];
767 if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
768 strcat(str, "device-local ");
770 if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
771 strcat(str, "host-visible ");
773 if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
774 strcat(str, "host-coherent ");
776 if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
777 strcat(str, "host-cached ");
779 if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
780 strcat(str, "lazily-allocated ");
789 static const char *get_queue_flags_str(VkQueueFlags flags)
791 static char str[128];
794 if(flags & VK_QUEUE_GRAPHICS_BIT) {
795 strcat(str, "graphics ");
797 if(flags & VK_QUEUE_COMPUTE_BIT) {
798 strcat(str, "compute ");
800 if(flags & VK_QUEUE_TRANSFER_BIT) {
801 strcat(str, "transfer ");
803 if(flags & VK_QUEUE_SPARSE_BINDING_BIT) {
804 strcat(str, "sparse-binding ");
812 static int ver_major(uint32_t ver)
814 return (ver >> 22) & 0x3ff;
817 static int ver_minor(uint32_t ver)
819 return (ver >> 12) & 0x3ff;
822 static int ver_patch(uint32_t ver)
827 static const char *get_mem_size_str(long sz)
830 static const char *unitstr[] = { "bytes", "KB", "MB", "GB", "TB", "PB", 0 };
834 while(sz >= 10240 && unitstr[uidx + 1]) {
838 sprintf(str, "%ld.%ld %s", sz / 10, sz % 10, unitstr[uidx]);