7 #include <vulkan/vulkan.h>
13 #include <vulkan/vulkan_win32.h>
15 /*#include <vulkan/vulkan_xlib.h>*/
16 #include <X11/Xlib-xcb.h>
17 #include <vulkan/vulkan_xcb.h>
37 /* if rpasses[rpidx].vkobj != vkrpass, the framebuf is invalid */
41 VkImageView imgv[MAX_FB_IMGV];
52 VkShaderModule sdr[VKSDR_MAX];
53 VkPrimitiveTopology prim;
54 VkPolygonMode polymode;
57 VkFrontFace frontface;
58 VkColorComponentFlags colorwr;
61 int stencil, stencilwr;
62 VkStencilOp sfail, szfail, szpass;
64 unsigned int sref, smask;
66 VkBlendFactor srcblend, dstblend, srcblend_a, dstblend_a;
72 VkPipelineLayout vkobj_layout; /* TODO probably best to split this */
76 static struct rpass *rpasses;
77 static struct framebuf *framebufs;
78 static struct pipeline *pipelines;
81 static int create_instance(void);
82 static int create_surface(void);
83 static int choose_phys_dev(void);
84 static int create_device(void);
85 static int create_swapchain(void);
86 static int create_default_cmdbuf(void);
88 static int choose_pixfmt(void);
89 static int eval_pdev_score(VkPhysicalDevice dev);
90 static int have_inst_layer(const char *name);
91 static int have_ext(VkExtensionProperties *ext, int next, const char *name);
96 #define MAX_INIT_QUEUE 32
101 VkCommandPool cmdpool;
102 } initq[MAX_INIT_QUEUE];
103 static int num_initq;
105 static VkInstance vk;
106 static VkPhysicalDevice vkpdev;
107 static VkQueueFamilyProperties *qfam;
108 static uint32_t num_qfam;
109 static VkDevice vkdev;
110 static VkSurfaceKHR vksurf;
111 static VkSurfaceCapabilitiesKHR vksurf_caps;
112 static int vksurf_numfmt, vksurf_selfmt;
113 static VkSurfaceFormatKHR *vksurf_fmt;
114 static VkSwapchainKHR vksc;
115 static int vksc_numimg;
116 static VkImage *vksc_img;
117 static VkExtent2D vksc_extent;
118 static VkImageView *vksc_view;
119 static VkCommandBuffer *vksc_cmdbuf; /* default command buffers (vksc_numimg) */
121 static VkLayerProperties *inst_layers;
122 static VkExtensionProperties *inst_ext, *dev_ext;
123 static uint32_t inst_ext_count, dev_ext_count, inst_layers_count;
125 static VkPhysicalDevice *pdev_list;
126 static uint32_t num_pdev;
128 static int have_raytrace, have_debug_report;
130 void vk_init_xwin(Display *d, Window w)
136 void vk_init_queue(unsigned int qflags, int count)
140 for(i=0; i<num_initq; i++) {
141 if(initq[i].flags == qflags) {
142 initq[i].count += count;
147 if(num_initq >= MAX_INIT_QUEUE) {
148 fprintf(stderr, "vk_init_queue: too many queues\n");
151 initq[num_initq].flags = qflags;
152 initq[num_initq].count = count;
156 int vk_init(unsigned int flags, unsigned int *usedflags)
159 vk_init_queue(VKQ_GFX | VKQ_PRESENT, 1);
163 if(create_instance() == -1) return -1;
164 if(create_surface() == -1) return -1;
165 if(choose_phys_dev() == -1) return -1;
166 if(create_device() == -1) return -1;
168 if(initflags != flags) {
170 *usedflags = initflags;
179 void vk_cleanup(void)
188 for(i=0; i<vksc_numimg; i++) {
189 vkDestroyImageView(vkdev, vksc_view[i], 0);
194 vkDestroySwapchainKHR(vkdev, vksc, 0);
198 vkDestroyDevice(vkdev, 0);
202 vkDestroySurfaceKHR(vk, vksurf, 0);
206 vkDestroyInstance(vk, 0);
219 int vk_reshape(int xsz, int ysz)
223 if(vksc && vksc_extent.width == xsz && vksc_extent.height == ysz) {
228 for(i=0; i<vksc_numimg; i++) {
229 vkDestroyImageView(vkdev, vksc_view[i], 0);
232 if(vksc) vkDestroySwapchainKHR(vkdev, vksc, 0);
234 vksc_extent.width = xsz;
235 vksc_extent.height = ysz;
237 if(create_swapchain() == -1) return -1;
240 if(create_default_cmdbuf() == -1) return -1;
243 /* TODO create depth/stencil buffers as needed (initflags) */
247 int vk_next_image(VkSemaphore sem)
250 if(vkAcquireNextImageKHR(vkdev, vksc, UINT64_MAX, sem, 0, &idx) != 0) {
256 int vk_submit(VkQueue q, VkCommandBuffer cmdbuf, VkSemaphore semwait, VkSemaphore semsig)
258 /* TODO: investigate if we need to expose the wait stage */
259 VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
260 VkSubmitInfo sinf = {0};
261 sinf.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
262 sinf.waitSemaphoreCount = semwait ? 1 : 0;
263 sinf.pWaitSemaphores = &semwait;
264 sinf.pWaitDstStageMask = &wait_stage;
265 sinf.commandBufferCount = 1;
266 sinf.pCommandBuffers = &cmdbuf;
267 sinf.signalSemaphoreCount = semsig ? 1 : 0;
268 sinf.pSignalSemaphores = &semsig;
270 if(vkQueueSubmit(q, 1, &sinf, 0) != 0) {
271 fprintf(stderr, "failed to submit command buffer\n");
277 int vk_present(VkQueue q, int imgid, VkSemaphore semwait)
279 VkPresentInfoKHR pinf = {0};
281 pinf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
282 pinf.waitSemaphoreCount = semwait ? 1 : 0;
283 pinf.pWaitSemaphores = &semwait;
284 pinf.swapchainCount = 1;
285 pinf.pSwapchains = &vksc;
286 pinf.pImageIndices = (unsigned int*)&imgid;
288 if(vkQueuePresentKHR(q, &pinf) != 0) {
289 fprintf(stderr, "present failed\n");
295 int vk_find_qfamily(unsigned int flags)
300 if(!qfam) return -1; /* not initialized I guess... */
302 for(i=0; i<num_qfam; i++) {
303 vkGetPhysicalDeviceSurfaceSupportKHR(vkpdev, i, vksurf, &can_pres);
305 if((flags & VKQ_PRESENT) && !can_pres) {
308 if((flags & VKQ_GFX) && !(qfam[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)) {
311 if((flags & VKQ_COMPUTE) && !(qfam[i].queueFlags & VK_QUEUE_COMPUTE_BIT)) {
315 return i; /* found a suitabe queue family */
321 VkQueue vk_getq_fam(int fam, int n)
325 if(fam < 0) return 0;
326 if(n < 0 || n >= qfam[fam].queueCount) {
327 fprintf(stderr, "vk_getq_fam: invalid index %d, family %d has %d queues\n",
328 n, fam, qfam[fam].queueCount);
332 vkGetDeviceQueue(vkdev, fam, n, &q);
336 VkQueue vk_getq(unsigned int flags, int n)
338 return vk_getq_fam(vk_find_qfamily(flags), n);
341 static VkCommandPool find_cmdpool(int qfam)
344 VkCommandPoolCreateInfo pinf;
346 for(i=0; i<num_initq; i++) {
347 if(initq[i].qfam == qfam) {
348 if(!initq[i].cmdpool) {
349 /* allocate command pool for this queue family */
350 memset(&pinf, 0, sizeof pinf);
351 pinf.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
352 pinf.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
353 pinf.queueFamilyIndex = qfam;
355 if(vkCreateCommandPool(vkdev, &pinf, 0, &initq[i].cmdpool) != 0) {
356 fprintf(stderr, "ck_create_cmdbuf: failed to create command buffer pool\n");
360 return initq[i].cmdpool;
364 fprintf(stderr, "vk_create_cmdbuf: failed to find command pool for queue family: %d\n", qfam);
368 VkCommandBuffer vk_create_cmdbuf_fam(int qfam, int level)
370 VkCommandBufferAllocateInfo inf = {0};
371 VkCommandBuffer cmdbuf;
372 VkCommandPool cmdpool;
374 if(!(cmdpool = find_cmdpool(qfam))) {
378 inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
379 inf.commandPool = cmdpool;
381 inf.commandBufferCount = 1;
383 if(vkAllocateCommandBuffers(vkdev, &inf, &cmdbuf) != 0) {
384 fprintf(stderr, "vk_create_cmdbuf: failed to allocate command buffer\n");
390 VkCommandBuffer vk_create_cmdbuf(unsigned int qflags, int level)
394 if((qfam = vk_find_qfamily(qflags)) == -1) {
395 fprintf(stderr, "vk_create_cmdbuf: failed to find matching queue family\n");
398 return vk_create_cmdbuf_fam(qfam, level);
401 VkCommandBuffer vk_get_cmdbuf(int imgid)
403 if(imgid < 0 || imgid >= vksc_numimg) {
404 fprintf(stderr, "vk_get_cmdbuf: invalid id %d, swap chain has %d images\n",
408 return vksc_cmdbuf[imgid];
411 int vk_create_rpass(void)
414 struct rpass rpass = {0}, *rp = &rpass;
417 rpasses = darr_alloc(0, sizeof *rpasses);
418 darr_push(rpasses, &rpass); /* add dummy rpass */
421 for(i=1; i<darr_size(rpasses); i++) {
422 if(!rpasses[i].used) {
427 /* init renderpass defaults */
429 rp->fmt = vksurf_fmt[vksurf_selfmt].format;
430 rp->zfmt = VK_FORMAT_D24_UNORM_S8_UINT;
438 darr_push(rpasses, rp);
439 return darr_size(rpasses) - 1;
444 void vk_free_rpass(int rp)
446 if(!rpasses || rp < 1 || rp >= darr_size(rpasses)) {
450 if(rpasses[rp].used && rpasses[rp].vkobj) {
451 vkDestroyRenderPass(vkdev, rpasses[rp].vkobj, 0);
453 rpasses[rp].used = 0;
456 void vk_rpass_colorbuf(int rp, int fmt, int n)
458 rpasses[rp].fmt = fmt;
459 rpasses[rp].num_colbuf = n;
460 rpasses[rp].vkobj_valid = 0;
463 void vk_rpass_msaa(int rp, int nsamp)
465 rpasses[rp].num_samples = nsamp;
466 rpasses[rp].vkobj_valid = 0;
469 void vk_rpass_clear(int rp, int clear)
471 rpasses[rp].clear = clear;
472 rpasses[rp].vkobj_valid = 0;
475 VkRenderPass vk_rpass(int rp)
479 VkAttachmentDescription att[17];
480 VkAttachmentReference catref[16], zatref;
481 VkSubpassDescription subpass;
482 VkRenderPassCreateInfo pinf;
486 if(!r->vkobj_valid) {
488 vkDestroyRenderPass(vkdev, r->vkobj, 0);
492 zidx = r->num_colbuf;
493 memset(att, 0, sizeof att);
494 for(i=0; i<r->num_colbuf; i++) {
495 att[i].format = r->fmt;
496 att[i].samples = r->num_samples;
497 att[i].loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
498 att[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
499 att[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
500 att[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
501 att[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
502 att[i].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
504 att[zidx].format = r->zfmt;
505 att[zidx].samples = 1;
506 att[zidx].loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
507 att[zidx].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
508 att[zidx].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
509 att[zidx].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
510 att[zidx].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
511 att[zidx].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
513 for(i=0; i<r->num_colbuf; i++) {
514 catref[i].attachment = i;
515 catref[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
517 zatref.attachment = zidx;
518 zatref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
520 memset(&subpass, 0, sizeof subpass);
521 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
522 subpass.colorAttachmentCount = r->num_colbuf;
523 subpass.pColorAttachments = catref;
524 subpass.pDepthStencilAttachment = &zatref;
526 memset(&pinf, 0, sizeof pinf);
527 pinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
528 pinf.attachmentCount = r->num_colbuf + 1;
529 pinf.pAttachments = att;
530 pinf.subpassCount = 1;
531 pinf.pSubpasses = &subpass;
533 if(vkCreateRenderPass(vkdev, &pinf, 0, &r->vkobj) != 0) {
534 fprintf(stderr, "failed to create render pass!\n");
544 int vk_create_fb(void)
547 struct framebuf framebuf = {0}, *fb = &framebuf;
550 framebufs = darr_alloc(0, sizeof *framebufs);
551 darr_push(framebufs, &framebuf); /* add dummy framebuffer */
554 for(i=1; i<darr_size(framebufs); i++) {
555 if(!framebufs[i].used) {
560 /* init framebuffer defaults */
561 memset(fb, 0, sizeof &fb);
564 if(fb == &framebuf) {
565 darr_push(framebufs, fb);
566 return darr_size(framebufs) - 1;
568 return fb - framebufs;
571 void vk_free_fb(int fb)
573 if(!framebufs || fb < 1 || fb >= darr_size(framebufs)) {
577 if(framebufs[fb].used && framebufs[fb].vkobj) {
578 vkDestroyFramebuffer(vkdev, framebufs[fb].vkobj, 0);
580 framebufs[fb].used = 0;
583 void vk_fb_size(int fb, int x, int y)
585 framebufs[fb].width = x;
586 framebufs[fb].height = y;
587 framebufs[fb].vkobj_valid = 0;
590 void vk_fb_rpass(int fb, int rpass)
592 if(rpass < 0 || rpass >= darr_size(rpasses) || !rpasses[rpass].used) {
593 fprintf(stderr, "vk_fb_rpass: %d is not a valid renderpass\n", rpass);
597 framebufs[fb].rpidx = rpass;
598 if(rpasses[rpass].vkobj_valid) {
599 framebufs[fb].vkrpass = rpasses[rpass].vkobj;
601 framebufs[fb].vkrpass = 0;
603 framebufs[fb].vkobj_valid = 0;
606 void vk_fb_images(int fb, int n, ...)
611 if(n > MAX_FB_IMGV) {
612 fprintf(stderr, "vk_fb_images: %d is too many images\n", n);
618 framebufs[fb].imgv[i] = va_arg(ap, VkImageView);
621 framebufs[fb].num_imgv = n;
622 framebufs[fb].vkobj_valid = 0;
625 VkFramebuffer vk_fb(int fb)
627 VkFramebufferCreateInfo fbinf;
633 if(!(rpass = vk_rpass(f->rpidx))) {
637 if(rpass != f->vkrpass || !f->vkobj_valid) {
640 vkDestroyFramebuffer(vkdev, f->vkobj, 0);
644 memset(&fbinf, 0, sizeof fbinf);
645 fbinf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
646 fbinf.renderPass = rpass;
647 fbinf.attachmentCount = f->num_imgv;
648 fbinf.pAttachments = f->imgv;
649 fbinf.width = f->width;
650 fbinf.height = f->height;
652 if(vkCreateFramebuffer(vkdev, &fbinf, 0, &f->vkobj) != 0) {
653 fprintf(stderr, "vk_fb: failed to create framebuffer\n");
662 int vk_create_pipeln(void)
665 struct pipeline pipeln = {0}, *pp = &pipeln;
668 pipelines = darr_alloc(0, sizeof *pipelines);
669 darr_push(pipelines, &pipeln); /* add dummy pipeline */
672 for(i=1; i<darr_size(pipelines); i++) {
673 if(!pipelines[i].used) {
678 /* init pipeline defaults */
679 memset(pp, 0, sizeof *pp);
681 pp->vport.width = pp->scissor.extent.width = 640;
682 pp->vport.height = pp->scissor.extent.height = 480;
683 pp->vport.minDepth = 0.0f;
684 pp->vport.maxDepth = 1.0f;
685 pp->prim = VKPRIM_TRIANGLES;
686 pp->polymode = VK_POLYGON_MODE_FILL;
687 pp->cull = VK_CULL_MODE_BACK_BIT;
688 pp->frontface = VK_FRONT_FACE_COUNTER_CLOCKWISE;
689 pp->line_width = 1.0f;
690 pp->colorwr = 0xf; /* RGBA */
692 pp->zop = VK_COMPARE_OP_LESS;
696 pp->sop = VK_COMPARE_OP_ALWAYS;
697 pp->smask = 0xffffffff;
699 pp->srcblend = pp->srcblend_a = VK_BLEND_FACTOR_ONE;
700 pp->dstblend = pp->dstblend_a = VK_BLEND_FACTOR_ZERO;
703 darr_push(pipelines, pp);
704 return darr_size(pipelines) - 1;
706 return pp - pipelines;
709 void vk_free_pipeln(int pp)
711 if(!pipelines || pp < 1 || pp >= darr_size(pipelines)) {
715 if(pipelines[pp].used && pipelines[pp].vkobj) {
716 vkDestroyPipeline(vkdev, pipelines[pp].vkobj, 0);
718 pipelines[pp].used = 0;
721 void vk_pipeln_rpass(int pp, VkRenderPass rp)
723 struct pipeline *p = pipelines + pp;
728 void vk_pipeln_viewport(int pp, int x, int y, int width, int height)
730 struct pipeline *p = pipelines + pp;
733 p->vport.width = width;
734 p->vport.height = height;
738 void vk_pipeln_scissor(int pp, int x, int y, int width, int height)
740 struct pipeline *p = pipelines + pp;
741 p->scissor.offset.x = x;
742 p->scissor.offset.y = y;
743 p->scissor.extent.width = width;
744 p->scissor.extent.height = height;
748 void vk_pipeln_shader(int pp, int type, VkShaderModule sdr)
750 struct pipeline *p = pipelines + pp;
755 /* TODO: vertex input */
756 void vk_pipeln_prim(int pp, int prim)
758 struct pipeline *p = pipelines + pp;
763 void vk_pipeln_polymode(int pp, int mode)
765 struct pipeline *p = pipelines + pp;
770 void vk_pipeln_cull(int pp, int cull)
772 struct pipeline *p = pipelines + pp;
777 void vk_pipeln_frontface(int pp, int ff)
779 struct pipeline *p = pipelines + pp;
784 void vk_pipeln_linewidth(int pp, int w)
786 struct pipeline *p = pipelines + pp;
791 void vk_pipeln_multisample(int pp, int nsamples)
796 void vk_pipeln_colormask(int pp, int r, int g, int b, int a)
798 struct pipeline *p = pipelines + pp;
800 if(r) p->colorwr |= VK_COLOR_COMPONENT_R_BIT;
801 if(g) p->colorwr |= VK_COLOR_COMPONENT_G_BIT;
802 if(b) p->colorwr |= VK_COLOR_COMPONENT_B_BIT;
803 if(a) p->colorwr |= VK_COLOR_COMPONENT_A_BIT;
807 void vk_pipeln_depthmask(int pp, int z)
809 struct pipeline *p = pipelines + pp;
814 void vk_pipeln_stencilmask(int pp, int s)
816 struct pipeline *p = pipelines + pp;
821 void vk_pipeln_zbuffer(int pp, int enable)
823 struct pipeline *p = pipelines + pp;
828 void vk_pipeln_zbuffer_op(int pp, int op)
830 struct pipeline *p = pipelines + pp;
835 void vk_pipeln_stencil(int pp, int enable)
837 struct pipeline *p = pipelines + pp;
842 void vk_pipeln_stencil_op(int pp, int sfail, int zfail, int zpass)
844 struct pipeline *p = pipelines + pp;
851 void vk_pipeln_stencil_func(int pp, int op, unsigned int ref, unsigned int mask)
853 struct pipeline *p = pipelines + pp;
860 void vk_pipeln_blend(int pp, int enable)
862 struct pipeline *p = pipelines + pp;
867 void vk_pipeln_blendfunc(int pp, int src, int dst)
869 struct pipeline *p = pipelines + pp;
875 VkPipeline vk_pipeln(int pp)
878 struct pipeline *p = pipelines + pp;
879 VkPipelineShaderStageCreateInfo ssinf[VKSDR_MAX];
880 VkPipelineVertexInputStateCreateInfo vinp;
881 VkPipelineInputAssemblyStateCreateInfo vasm;
882 VkPipelineViewportStateCreateInfo vp;
883 VkPipelineRasterizationStateCreateInfo rast;
884 VkPipelineMultisampleStateCreateInfo msaa;
885 VkPipelineDepthStencilStateCreateInfo zst;
886 VkPipelineColorBlendAttachmentState bat;
887 VkPipelineColorBlendStateCreateInfo blend;
888 VkPipelineLayoutCreateInfo lay;
889 VkGraphicsPipelineCreateInfo pinf;
897 vkDestroyPipeline(vkdev, p->vkobj, 0);
901 memset(ssinf, 0, sizeof ssinf);
903 for(i=0; i<VKSDR_MAX; i++) {
905 ssinf[idx].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
906 ssinf[idx].stage = VKSDR_STAGE(i);
907 ssinf[idx].module = p->sdr[idx];
908 ssinf[idx].pName = "main";
914 memset(&vinp, 0, sizeof vinp);
915 vinp.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
917 memset(&vasm, 0, sizeof vasm);
918 vasm.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
919 vasm.topology = p->prim;
921 memset(&vp, 0, sizeof vp);
922 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
923 vp.viewportCount = 1;
924 vp.pViewports = &p->vport;
926 vp.pScissors = &p->scissor;
928 memset(&rast, 0, sizeof rast);
929 rast.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
930 rast.polygonMode = p->polymode;
931 rast.lineWidth = p->line_width;
932 rast.cullMode = p->cull;
933 rast.frontFace = p->frontface;
936 memset(&msaa, 0, sizeof msaa);
937 msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
938 msaa.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
939 msaa.minSampleShading = 1.0f;
941 memset(&zst, 0, sizeof zst);
942 zst.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
943 zst.depthTestEnable = p->zbuf;
944 zst.depthWriteEnable = p->depthwr;
945 zst.depthCompareOp = p->zop;
946 zst.stencilTestEnable = p->stencil;
947 zst.front.writeMask = p->stencilwr;
948 zst.front.failOp = p->sfail;
949 zst.front.passOp = p->szpass;
950 zst.front.depthFailOp = p->szfail;
951 zst.front.compareOp = p->sop;
952 zst.front.compareMask = p->smask;
953 zst.front.reference = p->sref;
954 zst.back = zst.front;
956 memset(&bat, 0, sizeof bat);
957 bat.colorWriteMask = p->colorwr;
958 bat.blendEnable = p->blend;
959 bat.srcColorBlendFactor = p->srcblend;
960 bat.dstColorBlendFactor = p->dstblend;
961 bat.colorBlendOp = VK_BLEND_OP_ADD; /* TODO */
962 bat.srcAlphaBlendFactor = p->srcblend_a;
963 bat.dstAlphaBlendFactor = p->dstblend_a;
965 memset(&blend, 0, sizeof blend);
966 blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
967 blend.attachmentCount = 1;
968 blend.pAttachments = &bat;
971 memset(&lay, 0, sizeof lay);
972 lay.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
973 if(vkCreatePipelineLayout(vkdev, &lay, 0, &p->vkobj_layout) != 0) {
977 memset(&pinf, 0, sizeof pinf);
978 pinf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
979 pinf.stageCount = num_sdr;
980 pinf.pStages = ssinf;
981 pinf.pVertexInputState = &vinp;
982 pinf.pInputAssemblyState = &vasm;
983 pinf.pViewportState = &vp;
984 pinf.pRasterizationState = &rast;
985 pinf.pDepthStencilState = &zst;
986 pinf.pMultisampleState = &msaa;
987 pinf.pColorBlendState = &blend;
988 pinf.layout = p->vkobj_layout;
989 pinf.renderPass = p->rpass;
990 pinf.basePipelineIndex = -1;
992 if(vkCreateGraphicsPipelines(vkdev, 0, 1, &pinf, 0, &p->vkobj) != 0) {
1000 VkShaderModule vk_load_shader(const char *fname)
1005 VkShaderModuleCreateInfo sinf;
1008 if(!(fp = fopen(fname, "rb"))) {
1009 fprintf(stderr, "failed to open shader: %s: %s\n", fname, strerror(errno));
1012 fseek(fp, 0, SEEK_END);
1014 fseek(fp, 0, SEEK_SET);
1017 if(fread(buf, 1, sz, fp) < sz) {
1018 fprintf(stderr, "unexpected EOF while reading shader: %s\n", fname);
1024 memset(&sinf, 0, sizeof sinf);
1025 sinf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1029 if(vkCreateShaderModule(vkdev, &sinf, 0, &sdr) != 0) {
1030 fprintf(stderr, "failed to create shader from %s\n", fname);
1036 void vk_free_shader(VkShaderModule sdr)
1038 vkDestroyShaderModule(vkdev, sdr, 0);
1041 VkSemaphore vk_create_sem(void)
1044 VkSemaphoreCreateInfo sinf = {0};
1046 sinf.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1047 if(vkCreateSemaphore(vkdev, &sinf, 0, &sem) != 0) {
1053 void vk_free_sem(VkSemaphore sem)
1055 vkDestroySemaphore(vkdev, sem, 0);
1058 #define ARRSZ(arr) (sizeof arr / sizeof *arr)
1059 static const char *known_layer_list[] = {
1060 "VK_LAYER_GOOGLE_threading",
1061 "VK_LAYER_LUNARG_parameter_validation",
1062 "VK_LAYER_LUNARG_object_tracker",
1063 "VK_LAYER_LUNARG_image",
1064 "VK_LAYER_LUNARG_core_validation",
1065 "VK_LAYER_LUNARG_swapchain",
1066 "VK_LAYER_GOOGLE_unique_objects"
1072 } known_instext_list[] = {
1073 {"VK_KHR_surface", 1},
1075 {"VK_KHR_win32_surface", 1},
1077 /*{"VK_KHR_xlib_surface", 1},*/
1078 {"VK_KHR_xcb_surface", 1},
1080 {"VK_KHR_debug_report", 0}
1086 } known_devext_list[] = {
1087 {"VK_KHR_swapchain", 1},
1088 {"VK_KHR_acceleration_structure", 0},
1089 {"VK_KHR_ray_tracing_pipeline", 0}
1092 static int create_instance(void)
1094 int i, nlayers = 0, next = 0;
1095 VkInstanceCreateInfo instinf;
1096 VkApplicationInfo appinf;
1097 const char *layers[ARRSZ(known_layer_list)];
1098 const char *ext[ARRSZ(known_instext_list)];
1101 vkEnumerateInstanceVersion(&apiver);
1102 printf("Vulkan API version: %d.%d.%d\n", (apiver >> 22) & 0x7f,
1103 (apiver >> 12) & 0x3ff, apiver & 0xfff);
1105 memset(&appinf, 0, sizeof appinf);
1106 appinf.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
1107 appinf.pApplicationName = "vkray";
1108 appinf.pEngineName = "vkray";
1109 appinf.apiVersion = apiver;
1111 vkEnumerateInstanceLayerProperties(&inst_layers_count, 0);
1112 inst_layers = malloc_nf(inst_layers_count * sizeof *inst_layers);
1113 vkEnumerateInstanceLayerProperties(&inst_layers_count, inst_layers);
1115 vkEnumerateInstanceExtensionProperties(0, &inst_ext_count, 0);
1116 inst_ext = malloc_nf(inst_ext_count * sizeof *inst_ext);
1117 vkEnumerateInstanceExtensionProperties(0, &inst_ext_count, inst_ext);
1119 printf("Layers:\n");
1120 for(i=0; i<inst_layers_count; i++) {
1121 printf(" - %s: %s\n", inst_layers[i].layerName, inst_layers[i].description);
1123 printf("Instance extensions:\n");
1124 for(i=0; i<inst_ext_count; i++) {
1125 printf(" - %s\n", inst_ext[i].extensionName);
1128 have_debug_report = have_ext(inst_ext, inst_ext_count, "VK_KHR_debug_report");
1130 for(i=0; i<ARRSZ(known_layer_list); i++) {
1131 if(have_inst_layer(known_layer_list[i])) {
1132 layers[nlayers++] = known_layer_list[i];
1135 for(i=0; i<ARRSZ(known_instext_list); i++) {
1136 if(have_ext(inst_ext, inst_ext_count, known_instext_list[i].name)) {
1137 ext[next++] = known_instext_list[i].name;
1138 } else if(known_instext_list[i].required) {
1139 fprintf(stderr, "Vulkan implementation lacks required instance extension: %s\n",
1140 known_instext_list[i].name);
1145 memset(&instinf, 0, sizeof instinf);
1146 instinf.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
1147 instinf.pApplicationInfo = &appinf;
1148 instinf.enabledLayerCount = nlayers;
1149 instinf.ppEnabledLayerNames = layers;
1150 instinf.enabledExtensionCount = next;
1151 instinf.ppEnabledExtensionNames = ext;
1152 if(vkCreateInstance(&instinf, 0, &vk) != 0) {
1153 fprintf(stderr, "failed to create vulkan instance\n");
1160 static int create_surface(void)
1163 VkXlibSurfaceCreateInfoKHR xinf = {0};
1164 xinf.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
1168 if(vkCreateXlibSurfaceKHR(vk, &xinf, 0, &vksurf) != 0) {
1169 fprintf(stderr, "failed to create Xlib window surface\n");
1173 VkXcbSurfaceCreateInfoKHR xinf = {0};
1174 xinf.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
1175 xinf.connection = XGetXCBConnection(dpy);
1176 xinf.window = (xcb_window_t)win;
1178 if(vkCreateXcbSurfaceKHR(vk, &xinf, 0, &vksurf) != 0) {
1179 fprintf(stderr, "failed to create XCB window surface\n");
1185 int choose_phys_dev(void)
1187 uint32_t i, num_pdev, score, best_score, best_dev;
1188 VkPhysicalDevice *pdev;
1189 VkPhysicalDeviceProperties pdevprop;
1192 vkEnumeratePhysicalDevices(vk, &num_pdev, 0);
1194 fprintf(stderr, "no vulkan devices found\n");
1197 pdev = malloc_nf(num_pdev * sizeof *pdev);
1198 vkEnumeratePhysicalDevices(vk, &num_pdev, pdev);
1200 printf("Found %d physical devices\n", num_pdev);
1204 for(i=0; i<num_pdev; i++) {
1205 if((score = eval_pdev_score(pdev[i])) && score > best_score) {
1210 vkGetPhysicalDeviceProperties(pdev[i], &pdevprop);
1211 printf(" %d: %s (score: %d)\n", i, pdevprop.deviceName, score);
1213 if(best_dev == -1) {
1214 fprintf(stderr, "no suitable vulkan device found\n");
1218 vkpdev = pdev[best_dev];
1220 if(qfam) free(qfam);
1222 vkGetPhysicalDeviceQueueFamilyProperties(vkpdev, &num_qfam, 0);
1223 qfam = malloc_nf(num_qfam * sizeof *qfam);
1224 vkGetPhysicalDeviceQueueFamilyProperties(vkpdev, &num_qfam, qfam);
1231 static int create_device(void)
1234 VkDeviceQueueCreateInfo qinf[MAX_INIT_QUEUE] = {0};
1235 VkPhysicalDeviceFeatures feat = {0};
1236 VkDeviceCreateInfo devinf = {0};
1237 const char *ext[ARRSZ(known_devext_list) + 16];
1238 int i, j, num_ext, qfam, totalq;
1240 vkEnumerateDeviceExtensionProperties(vkpdev, 0, &dev_ext_count, 0);
1241 dev_ext = malloc_nf(dev_ext_count * sizeof *dev_ext);
1242 vkEnumerateDeviceExtensionProperties(vkpdev, 0, &dev_ext_count, dev_ext);
1245 for(i=0; i<ARRSZ(known_devext_list); i++) {
1246 if(have_ext(dev_ext, dev_ext_count, known_devext_list[i].name)) {
1247 ext[num_ext++] = known_devext_list[i].name;
1248 } else if(known_devext_list[i].required) {
1249 fprintf(stderr, "Vulkan device lacks required extension: %s\n",
1250 known_devext_list[i].name);
1255 if(initflags & VKINIT_RAY) {
1256 if(have_ext(dev_ext, dev_ext_count, "VK_KHR_acceleration_structure") &&
1257 have_ext(dev_ext, dev_ext_count, "VK_KHR_ray_tracing_pipeline")) {
1258 ext[num_ext++] = "VK_KHR_acceleration_structure";
1259 ext[num_ext++] = "VK_KHR_ray_tracing_pipeline";
1261 initflags &= ~VKINIT_RAY;
1266 for(i=0; i<num_initq; i++) {
1267 totalq += initq[i].count;
1270 fprintf(stderr, "create_device: arbitrary limit of total queues exceeded (%d)\n", totalq);
1273 prio = alloca(totalq * sizeof *prio);
1275 for(i=0; i<num_initq; i++) {
1276 if((qfam = vk_find_qfamily(initq[i].flags)) == -1) {
1277 fprintf(stderr, "create_device: failed to find queue family (flags: 0x%2x)\n",
1281 initq[i].qfam = qfam;
1282 initq[i].cmdpool = 0;
1284 qinf[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
1285 qinf[i].queueFamilyIndex = qfam;
1286 qinf[i].queueCount = initq[i].count;
1287 qinf[i].pQueuePriorities = prio;
1288 for(j=0; j<initq[i].count; i++) {
1289 *prio++ = 1.0f; /* all queue priorities 1 */
1293 devinf.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
1294 devinf.pQueueCreateInfos = qinf;
1295 devinf.queueCreateInfoCount = num_initq;
1296 devinf.pEnabledFeatures = &feat;
1297 devinf.enabledExtensionCount = num_ext;
1298 devinf.ppEnabledExtensionNames = ext;
1300 if(vkCreateDevice(vkpdev, &devinf, 0, &vkdev) != 0) {
1301 fprintf(stderr, "failed to create vulkan device\n");
1307 static int create_swapchain(void)
1311 VkSwapchainCreateInfoKHR scinf = {0};
1312 VkImageViewCreateInfo ivinf;
1314 if(vksc_extent.width <= 0 || vksc_extent.height <= 0) {
1318 scinf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
1319 scinf.surface = vksurf;
1320 scinf.minImageCount = 2;
1321 scinf.imageFormat = vksurf_fmt[vksurf_selfmt].format;
1322 scinf.imageColorSpace = vksurf_fmt[vksurf_selfmt].colorSpace;
1323 scinf.imageExtent = vksc_extent;
1324 scinf.imageArrayLayers = 1;
1325 scinf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1326 scinf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
1327 scinf.preTransform = vksurf_caps.currentTransform;
1328 scinf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1329 scinf.presentMode = VK_PRESENT_MODE_FIFO_KHR;
1330 scinf.clipped = VK_TRUE;
1332 if(vkCreateSwapchainKHR(vkdev, &scinf, 0, &vksc) != 0) {
1333 fprintf(stderr, "failed to create swapchain\n");
1337 if(!vksc_img || vksc_numimg != num) {
1339 vkGetSwapchainImagesKHR(vkdev, vksc, &num, 0);
1340 vksc_img = malloc_nf(num * sizeof *vksc_img);
1341 vkGetSwapchainImagesKHR(vkdev, vksc, &num, vksc_img);
1343 if(!vksc_view || vksc_numimg != num) {
1345 vksc_view = malloc_nf(num * sizeof *vksc_view);
1349 for(i=0; i<num; i++) {
1350 memset(&ivinf, 0, sizeof ivinf);
1351 ivinf.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1352 ivinf.image = vksc_img[i];
1353 ivinf.format = vksurf_fmt[vksurf_selfmt].format;
1354 ivinf.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
1355 ivinf.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
1356 ivinf.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
1357 ivinf.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
1358 ivinf.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1359 ivinf.subresourceRange.levelCount = 1;
1360 ivinf.subresourceRange.layerCount = 1;
1361 ivinf.viewType = VK_IMAGE_VIEW_TYPE_2D;
1363 if(vkCreateImageView(vkdev, &ivinf, 0, vksc_view + i) != 0) {
1364 fprintf(stderr, "failed to create image view (%d)\n", i);
1371 static int create_default_cmdbuf(void)
1374 VkCommandPool cmdpool;
1375 VkCommandBufferAllocateInfo cbinf = {0};
1377 assert(!vksc_cmdbuf);
1379 if((qfam = vk_find_qfamily(VKQ_GFX | VKQ_PRESENT)) == -1) {
1380 fprintf(stderr, "failed to find a gfx|present capable queue family\n");
1383 if(!(cmdpool = find_cmdpool(qfam))) {
1384 fprintf(stderr, "failed to find usable command pool for default command buffers\n");
1387 if(!(vksc_cmdbuf = malloc(vksc_numimg * sizeof *vksc_cmdbuf))) {
1388 fprintf(stderr, "failed to allocate %d command buffers for the swap-chain\n", vksc_numimg);
1392 cbinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1393 cbinf.commandPool = cmdpool;
1394 cbinf.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1395 cbinf.commandBufferCount = vksc_numimg;
1397 if(vkAllocateCommandBuffers(vkdev, &cbinf, vksc_cmdbuf) != 0) {
1398 fprintf(stderr, "failed to create %d command buffers for the swap-chain\n", vksc_numimg);
1404 static int eval_pdev_score(VkPhysicalDevice dev)
1407 uint32_t i, num_fmt, num_qfam, num_ext;
1408 VkQueueFamilyProperties *qfam;
1409 VkExtensionProperties *ext;
1410 VkPhysicalDeviceProperties prop;
1411 VkPhysicalDeviceFeatures feat;
1412 VkSurfaceFormatKHR *sfmt;
1415 vkGetPhysicalDeviceProperties(dev, &prop);
1416 vkGetPhysicalDeviceFeatures(dev, &feat);
1418 /* check if we have the swapchain extension */
1419 vkEnumerateDeviceExtensionProperties(dev, 0, &num_ext, 0);
1420 ext = malloc_nf(num_ext * sizeof *ext);
1421 vkEnumerateDeviceExtensionProperties(dev, 0, &num_ext, ext);
1423 if(!have_ext(ext, num_ext, "VK_KHR_swapchain")) {
1428 /* populate format and present modes arrays, and make sure we have some of each */
1429 vkGetPhysicalDeviceSurfaceFormatsKHR(dev, vksurf, &num_fmt, 0);
1434 sfmt = malloc_nf(num_fmt * sizeof *sfmt);
1435 vkGetPhysicalDeviceSurfaceFormatsKHR(dev, vksurf, &num_fmt, sfmt);
1437 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(dev, vksurf, &vksurf_caps);
1439 /* find a queue family which can do graphics and can present */
1440 vkGetPhysicalDeviceQueueFamilyProperties(dev, &num_qfam, 0);
1441 qfam = malloc_nf(num_qfam * sizeof *qfam);
1442 vkGetPhysicalDeviceQueueFamilyProperties(dev, &num_qfam, qfam);
1444 for(i=0; i<num_qfam; i++) {
1445 vkGetPhysicalDeviceSurfaceSupportKHR(dev, i, vksurf, &can_pres);
1446 if(qfam[i].queueCount && (qfam[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && can_pres) {
1451 switch(prop.deviceType) {
1452 case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
1455 case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
1458 case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
1465 if(initflags & VKINIT_RAY) {
1466 if(have_ext(ext, num_ext, "VK_KHR_acceleration_structure") &&
1467 have_ext(ext, num_ext, "VK_KHR_ray_tracing_pipeline")) {
1478 static int choose_pixfmt(void)
1480 static const VkFormat pref[] = {
1481 VK_FORMAT_B8G8R8_UNORM,
1482 VK_FORMAT_R8G8B8_UNORM,
1483 VK_FORMAT_B8G8R8A8_UNORM,
1484 VK_FORMAT_R8G8B8A8_UNORM
1489 vkGetPhysicalDeviceSurfaceFormatsKHR(vkpdev, vksurf, &num_fmt, 0);
1490 if(!num_fmt) return -1;
1491 vksurf_fmt = malloc_nf(num_fmt * sizeof *vksurf_fmt);
1492 vkGetPhysicalDeviceSurfaceFormatsKHR(vkpdev, vksurf, &num_fmt, vksurf_fmt);
1495 for(i=0; i<num_fmt; i++) {
1496 if(vksurf_fmt[i].colorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
1499 for(j=0; j<sizeof pref / sizeof *pref; j++) {
1500 if(vksurf_fmt[i].format == pref[j]) {
1502 vksurf_numfmt = num_fmt;
1513 static int have_inst_layer(const char *name)
1516 for(i=0; i<inst_layers_count; i++) {
1517 if(strcmp(inst_layers[i].layerName, name) == 0) {
1524 static int have_ext(VkExtensionProperties *ext, int next, const char *name)
1527 for(i=0; i<next; i++) {
1528 if(strcmp(ext[i].extensionName, name) == 0) {