command buffer allocation
[vktest3] / src / vk.c
index 44c838f..5480fc1 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -1,9 +1,11 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include <stdarg.h>
 #include <vulkan/vulkan.h>
 #include "vk.h"
 #include "util.h"
+#include "darray.h"
 
 #ifdef __WIN32__
 #include <vulkan/vulkan_win32.h>
 #include <vulkan/vulkan_xcb.h>
 #endif
 
+struct rpass {
+       int used;
+       int fmt;
+       int zfmt;
+       int num_colbuf;
+       int num_samples;
+       int clear;
+
+       int vkobj_valid;
+       VkRenderPass vkobj;
+};
+
+#define MAX_FB_IMGV    8
+struct framebuf {
+       int used;
+       int width, height;
+
+       /* if rpasses[rpidx].vkobj != vkrpass, the framebuf is invalid */
+       int rpidx;
+       VkRenderPass vkrpass;
+
+       VkImageView imgv[MAX_FB_IMGV];
+       int num_imgv;
+
+       int vkobj_valid;
+       VkFramebuffer vkobj;
+};
+
+
+static struct rpass *rpasses;
+static struct framebuf *framebufs;
+
+
 static int create_instance(void);
 static int create_surface(void);
 static int choose_phys_dev(void);
@@ -27,12 +62,20 @@ static int have_ext(VkExtensionProperties *ext, int next, const char *name);
 static Display *dpy;
 static Window win;
 static int initflags;
+#define MAX_INIT_QUEUE 32
+static struct {
+       unsigned int flags;
+       int count;
+       int qfam;
+       VkCommandPool cmdpool;
+} initq[MAX_INIT_QUEUE];
+static int num_initq;
 
 static VkInstance vk;
 static VkPhysicalDevice vkpdev;
-static int vkqfam_idx, vkqfam_maxq;
+static VkQueueFamilyProperties *qfam;
+static uint32_t num_qfam;
 static VkDevice vkdev;
-static VkQueue vkq;
 static VkSurfaceKHR vksurf;
 static VkSurfaceCapabilitiesKHR vksurf_caps;
 static int vksurf_numfmt, vksurf_selfmt;
@@ -58,8 +101,32 @@ void vk_init_xwin(Display *d, Window w)
        win = w;
 }
 
+void vk_init_queue(unsigned int qflags, int count)
+{
+       int i;
+
+       for(i=0; i<num_initq; i++) {
+               if(initq[i].flags == qflags) {
+                       initq[i].count += count;
+                       return;
+               }
+       }
+
+       if(num_initq >= MAX_INIT_QUEUE) {
+               fprintf(stderr, "vk_init_queue: too many queues\n");
+               return;
+       }
+       initq[num_initq].flags = qflags;
+       initq[num_initq].count = count;
+       num_initq++;
+}
+
 int vk_init(unsigned int flags, unsigned int *usedflags)
 {
+       if(!num_initq) {
+               vk_init_queue(VKQ_GFX | VKQ_PRESENT, 1);
+       }
+
        initflags = flags;
        if(create_instance() == -1)     return -1;
        if(create_surface() == -1) return -1;
@@ -141,19 +208,111 @@ int vk_reshape(int xsz, int ysz)
        return 0;
 }
 
-struct rpass {
-       int used;
-       int fmt;
-       int num_colbuf;
-       int num_samples;
-       /* TODO: stuff about depth-stencil */
-       int clear;
+int vk_find_qfamily(unsigned int flags)
+{
+       int i, famidx = -1;
+       VkBool32 can_pres;
 
-       int vkobj_valid;
-       VkRenderPass vkobj;
-};
+       if(!qfam) return -1;    /* not initialized I guess... */
 
-static struct rpass *rpasses;
+       for(i=0; i<num_qfam; i++) {
+               vkGetPhysicalDeviceSurfaceSupportKHR(vkpdev, i, vksurf, &can_pres);
+
+               if((flags & VKQ_PRESENT) && !can_pres) {
+                       continue;
+               }
+               if((flags & VKQ_GFX) && !(qfam[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)) {
+                       continue;
+               }
+               if((flags & VKQ_COMPUTE) && !(qfam[i].queueFlags & VK_QUEUE_COMPUTE_BIT)) {
+                       continue;
+               }
+
+               return i;       /* found a suitabe queue family */
+       }
+
+       return -1;
+}
+
+VkQueue vk_getq_fam(int fam, int n)
+{
+       VkQueue q;
+
+       if(fam < 0) return 0;
+       if(n < 0 || n >= qfam[fam].queueCount) {
+               fprintf(stderr, "vk_getq_fam: invalid index %d, family %d has %d queues\n",
+                          n, fam, qfam[fam].queueCount);
+               return 0;
+       }
+
+       vkGetDeviceQueue(vkdev, fam, n, &q);
+       return q;
+}
+
+VkQueue vk_getq(unsigned int flags, int n)
+{
+       return vk_getq_fam(vk_find_qfamily(flags), n);
+}
+
+static VkCommandPool find_cmdpool(int qfam)
+{
+       int i;
+       VkCommandPoolCreateInfo pinf;
+
+       for(i=0; i<num_initq; i++) {
+               if(initq[i].qfam == qfam) {
+                       if(!initq[i].cmdpool) {
+                               /* allocate command pool for this queue family */
+                               memset(&pinf, 0, sizeof pinf);
+                               pinf.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
+                               pinf.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
+                               pinf.queueFamilyIndex = qfam;
+
+                               if(vkCreateCommandPool(vkdev, &pinf, 0, &initq[i].cmdpool) != 0) {
+                                       fprintf(stderr, "ck_create_cmdbuf: failed to create command buffer pool\n");
+                                       return 0;
+                               }
+                       }
+                       return initq[i].cmdpool;
+               }
+       }
+
+       fprintf(stderr, "vk_create_cmdbuf: failed to find command pool for queue family: %d\n", qfam);
+       return 0;
+}
+
+VkCommandBuffer vk_create_cmdbuf_fam(int qfam, int level)
+{
+       VkCommandBufferAllocateInfo inf = {0};
+       VkCommandBuffer cmdbuf;
+       VkCommandPool cmdpool;
+
+       if(!(cmdpool = find_cmdpool(qfam))) {
+               return 0;
+       }
+
+       inf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+       inf.commandPool = cmdpool;
+       inf.level = level;
+       inf.commandBufferCount = 1;
+
+       if(vkAllocateCommandBuffers(vkdev, &inf, &cmdbuf) != 0) {
+               fprintf(stderr, "vk_create_cmdbuf: failed to allocate command buffer\n");
+               return 0;
+       }
+       return cmdbuf;
+}
+
+VkCommandBuffer vk_create_cmdbuf(unsigned int qflags, int level)
+{
+       int qfam;
+
+       if((qfam = vk_find_qfamily(qflags)) == -1) {
+               fprintf(stderr, "vk_create_cmdbuf: failed to find matching queue family\n");
+               return 0;
+       }
+       return vk_create_cmdbuf_fam(qfam, level);
+}
 
 int vk_create_rpass(void)
 {
@@ -174,6 +333,7 @@ int vk_create_rpass(void)
        /* init renderpass defaults */
        rp->used = 1;
        rp->fmt = vksurf_fmt[vksurf_selfmt].format;
+       rp->zfmt = VK_FORMAT_D24_UNORM_S8_UINT;
        rp->num_colbuf = 1;
        rp->num_samples = 1;
        rp->clear = 1;
@@ -209,52 +369,202 @@ void vk_rpass_colorbuf(int rp, int fmt, int n)
 void vk_rpass_msaa(int rp, int nsamp)
 {
        rpasses[rp].num_samples = nsamp;
+       rpasses[rp].vkobj_valid = 0;
 }
 
-void vk_rpass_clear(int clear)
+void vk_rpass_clear(int rp, int clear)
 {
        rpasses[rp].clear = clear;
+       rpasses[rp].vkobj_valid = 0;
 }
 
 VkRenderPass vk_rpass(int rp)
 {
+       int i, zidx;
        struct rpass *r;
-       VkAttachmentDescription cat;
-       VkAttachmentReference catref[16];
+       VkAttachmentDescription att[17];
+       VkAttachmentReference catref[16], zatref;
        VkSubpassDescription subpass;
        VkRenderPassCreateInfo pinf;
-       VkSubpassDependency dep;
 
        r = rpasses + rp;
 
        if(!r->vkobj_valid) {
-               memset(&cat, 0, sizeof cat);
-               cat.format = r->fmt;
-               cat.samples = r->num_samples;
-               cat.loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
-               cat.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
-               cat.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
-               cat.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
-               cat.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
-               cat.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
+               if(r->vkobj) {
+                       vkDestroyRenderPass(vkdev, r->vkobj, 0);
+                       r->vkobj = 0;
+               }
+
+               zidx = r->num_colbuf;
+               memset(att, 0, sizeof att);
+               for(i=0; i<r->num_colbuf; i++) {
+                       att[i].format = r->fmt;
+                       att[i].samples = r->num_samples;
+                       att[i].loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+                       att[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
+                       att[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+                       att[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+                       att[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+                       att[i].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
+               }
+               att[zidx].format = r->zfmt;
+               att[zidx].samples = 1;
+               att[zidx].loadOp = r->clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+               att[zidx].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
+               att[zidx].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+               att[zidx].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
+               att[zidx].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+               att[zidx].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
 
                for(i=0; i<r->num_colbuf; i++) {
-                       memset(&catref[i], 0, sizeof catref);
+                       catref[i].attachment = i;
                        catref[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
                }
+               zatref.attachment = zidx;
+               zatref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
 
                memset(&subpass, 0, sizeof subpass);
                subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
                subpass.colorAttachmentCount = r->num_colbuf;
                subpass.pColorAttachments = catref;
+               subpass.pDepthStencilAttachment = &zatref;
+
+               memset(&pinf, 0, sizeof pinf);
+               pinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
+               pinf.attachmentCount = r->num_colbuf + 1;
+               pinf.pAttachments = att;
+               pinf.subpassCount = 1;
+               pinf.pSubpasses = &subpass;
+
+               if(vkCreateRenderPass(vkdev, &pinf, 0, &r->vkobj) != 0) {
+                       fprintf(stderr, "failed to create render pass!\n");
+                       return 0;
+               }
+               r->vkobj_valid = 1;
+       }
+
+       return r->vkobj;
+}
 
-               memset(&dep, 0, sizeof dep);
-               dep.srcSubpass = 
+
+int vk_create_fb(void)
+{
+       int i;
+       struct framebuf framebuf = {0}, *fb = &framebuf;
+
+       if(!framebufs) {
+               framebufs = darr_alloc(0, sizeof *framebufs);
+               darr_push(framebufs, &framebuf);        /* add dummy rpass */
+       }
+
+       for(i=1; i<darr_size(framebufs); i++) {
+               if(!framebufs[i].used) {
+                       fb = framebufs + i;
+               }
        }
 
-       return rpasses[rp].vkobj;
+       /* init framebuffer defaults */
+       memset(fb, 0, sizeof &fb);
+       fb->used = 1;
+
+       if(fb == &framebuf) {
+               darr_push(framebufs, fb);
+               return darr_size(framebufs) - 1;
+       }
+       return fb - framebufs;
 }
 
+void vk_free_fb(int fb)
+{
+       if(!framebufs || fb < 1 || fb >= darr_size(framebufs)) {
+               return;
+       }
+
+       if(framebufs[fb].used && framebufs[fb].vkobj) {
+               vkDestroyFramebuffer(vkdev, framebufs[fb].vkobj, 0);
+       }
+       framebufs[fb].used = 0;
+}
+
+void vk_fb_size(int fb, int x, int  y)
+{
+       framebufs[fb].width = x;
+       framebufs[fb].height = y;
+       framebufs[fb].vkobj_valid = 0;
+}
+
+void vk_fb_rpass(int fb, int rpass)
+{
+       if(rpass < 0 || rpass >= darr_size(rpasses) || !rpasses[rpass].used) {
+               fprintf(stderr, "vk_fb_rpass: %d is not a valid renderpass\n", rpass);
+               return;
+       }
+
+       framebufs[fb].rpidx = rpass;
+       if(rpasses[rpass].vkobj_valid) {
+               framebufs[fb].vkrpass = rpasses[rpass].vkobj;
+       } else {
+               framebufs[fb].vkrpass = 0;
+       }
+       framebufs[fb].vkobj_valid = 0;
+}
+
+void vk_fb_images(int fb, int n, ...)
+{
+       int i;
+       va_list ap;
+
+       if(n > MAX_FB_IMGV) {
+               fprintf(stderr, "vk_fb_images: %d is too many images\n", n);
+               n = MAX_FB_IMGV;
+       }
+
+       va_start(ap, n);
+       for(i=0; i<n; i++) {
+               framebufs[fb].imgv[i] = va_arg(ap, VkImageView);
+       }
+       va_end(ap);
+       framebufs[fb].num_imgv = n;
+       framebufs[fb].vkobj_valid = 0;
+}
+
+VkFramebuffer vk_fb(int fb)
+{
+       VkFramebufferCreateInfo fbinf;
+       VkRenderPass rpass;
+       struct framebuf *f;
+
+       f = framebufs + fb;
+
+       if(!(rpass = vk_rpass(f->rpidx))) {
+               return 0;
+       }
+
+       if(rpass != f->vkrpass || !f->vkobj_valid) {
+               f->vkrpass = rpass;
+               if(f->vkobj) {
+                       vkDestroyFramebuffer(vkdev, f->vkobj, 0);
+                       f->vkobj = 0;
+               }
+
+               memset(&fbinf, 0, sizeof fbinf);
+               fbinf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
+               fbinf.renderPass = rpass;
+               fbinf.attachmentCount = f->num_imgv;
+               fbinf.pAttachments = f->imgv;
+               fbinf.width = f->width;
+               fbinf.height = f->height;
+
+               if(vkCreateFramebuffer(vkdev, &fbinf, 0, &f->vkobj) != 0) {
+                       fprintf(stderr, "vk_fb: failed to create framebuffer\n");
+                       return 0;
+               }
+               f->vkobj_valid = 1;
+       }
+       return f->vkobj;
+}
+
+
 #define ARRSZ(arr)     (sizeof arr / sizeof *arr)
 static const char *known_layer_list[] = {
        "VK_LAYER_GOOGLE_threading",
@@ -384,10 +694,9 @@ static int create_surface(void)
 
 int choose_phys_dev(void)
 {
-       uint32_t i, num_pdev, num_qfam, score, best_score, best_dev;
+       uint32_t i, num_pdev, score, best_score, best_dev;
        VkPhysicalDevice *pdev;
        VkPhysicalDeviceProperties pdevprop;
-       VkQueueFamilyProperties *qfam;
        VkBool32 can_pres;
 
        vkEnumeratePhysicalDevices(vk, &num_pdev, 0);
@@ -418,21 +727,12 @@ int choose_phys_dev(void)
        }
        vkpdev = pdev[best_dev];
 
+       if(qfam) free(qfam);
+
        vkGetPhysicalDeviceQueueFamilyProperties(vkpdev, &num_qfam, 0);
        qfam = malloc_nf(num_qfam * sizeof *qfam);
        vkGetPhysicalDeviceQueueFamilyProperties(vkpdev, &num_qfam, qfam);
 
-       vkqfam_idx = -1;
-       for(i=0; i<num_qfam; i++) {
-               vkGetPhysicalDeviceSurfaceSupportKHR(vkpdev, i, vksurf, &can_pres);
-               if(qfam[i].queueCount && (qfam[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && can_pres) {
-                       vkqfam_maxq = qfam[i].queueCount;
-                       vkqfam_idx = i;
-                       break;
-               }
-       }
-
-       free(qfam);
        free(pdev);
        choose_pixfmt();
        return 0;
@@ -440,12 +740,12 @@ int choose_phys_dev(void)
 
 static int create_device(void)
 {
-       float prio = 1.0f;
-       VkDeviceQueueCreateInfo qinf = {0};
+       float *prio;
+       VkDeviceQueueCreateInfo qinf[MAX_INIT_QUEUE] = {0};
        VkPhysicalDeviceFeatures feat = {0};
        VkDeviceCreateInfo devinf = {0};
        const char *ext[ARRSZ(known_devext_list) + 16];
-       int i, num_ext;
+       int i, j, num_ext, qfam, totalq;
 
        vkEnumerateDeviceExtensionProperties(vkpdev, 0, &dev_ext_count, 0);
        dev_ext = malloc_nf(dev_ext_count * sizeof *dev_ext);
@@ -472,14 +772,37 @@ static int create_device(void)
                }
        }
 
-       qinf.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
-       qinf.queueFamilyIndex = vkqfam_idx;
-       qinf.queueCount = 1;
-       qinf.pQueuePriorities = &prio;
+       totalq = 0;
+       for(i=0; i<num_initq; i++) {
+               totalq += initq[i].count;
+       }
+       if(totalq > 1024) {
+               fprintf(stderr, "create_device: arbitrary limit of total queues exceeded (%d)\n", totalq);
+               return -1;
+       }
+       prio = alloca(totalq * sizeof *prio);
+
+       for(i=0; i<num_initq; i++) {
+               if((qfam = vk_find_qfamily(initq[i].flags)) == -1) {
+                       fprintf(stderr, "create_device: failed to find queue family (flags: 0x%2x)\n",
+                                       initq[i].flags);
+                       return -1;
+               }
+               initq[i].qfam = qfam;
+               initq[i].cmdpool = 0;
+
+               qinf[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
+               qinf[i].queueFamilyIndex = qfam;
+               qinf[i].queueCount = initq[i].count;
+               qinf[i].pQueuePriorities = prio;
+               for(j=0; j<initq[i].count; i++) {
+                       *prio++ = 1.0f; /* all queue priorities 1 */
+               }
+       }
 
        devinf.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
-       devinf.pQueueCreateInfos = &qinf;
-       devinf.queueCreateInfoCount = 1;
+       devinf.pQueueCreateInfos = qinf;
+       devinf.queueCreateInfoCount = num_initq;
        devinf.pEnabledFeatures = &feat;
        devinf.enabledExtensionCount = num_ext;
        devinf.ppEnabledExtensionNames = ext;
@@ -488,8 +811,6 @@ static int create_device(void)
                fprintf(stderr, "failed to create vulkan device\n");
                return -1;
        }
-
-       vkGetDeviceQueue(vkdev, vkqfam_idx, 0, &vkq);
        return 0;
 }