command buffer allocation
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 19 Aug 2022 09:38:47 +0000 (12:38 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 19 Aug 2022 09:38:47 +0000 (12:38 +0300)
src/vk.c
src/vk.h

index 6887460..5480fc1 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -66,6 +66,8 @@ static int initflags;
 static struct {
        unsigned int flags;
        int count;
+       int qfam;
+       VkCommandPool cmdpool;
 } initq[MAX_INIT_QUEUE];
 static int num_initq;
 
@@ -101,6 +103,15 @@ void vk_init_xwin(Display *d, Window 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;
@@ -238,9 +249,69 @@ VkQueue vk_getq_fam(int fam, int n)
        return q;
 }
 
-VkQueue vk_getq(unsigned int flags)
+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)
 {
-       return vk_getq_fam(vk_find_qfamily(flags), 0);
+       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)
@@ -717,6 +788,9 @@ static int create_device(void)
                                        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;
index 3f765ac..218e810 100644 (file)
--- a/src/vk.h
+++ b/src/vk.h
@@ -28,7 +28,10 @@ int vk_reshape(int xsz, int ysz);
 
 int vk_find_qfamily(unsigned int flags);
 VkQueue vk_getq_fam(int fam, int n);
-VkQueue vk_getq(unsigned int flags);
+VkQueue vk_getq(unsigned int flags, int n);
+
+VkCommandBuffer vk_create_cmdbuf_fam(int qfam, int level);
+VkCommandBuffer vk_create_cmdbuf(unsigned int qflags, int level);
 
 int vk_create_rpass(void);
 void vk_free_rpass(int rp);