fixed some validation bugs
[vktest3] / src / vk.c
index ebc31ae..3ea0060 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -244,7 +244,17 @@ int vk_reshape(int xsz, int ysz)
        return 0;
 }
 
-int vk_next_image(VkSemaphore sem)
+int vk_num_swap_images(void)
+{
+       return vksc_numimg;
+}
+
+VkImageView vk_swap_image(int idx)
+{
+       return vksc_view[idx];
+}
+
+int vk_next_swap_image(VkSemaphore sem)
 {
        uint32_t idx;
        if(vkAcquireNextImageKHR(vkdev, vksc, UINT64_MAX, sem, 0, &idx) != 0) {
@@ -253,6 +263,45 @@ int vk_next_image(VkSemaphore sem)
        return (int)idx;
 }
 
+int vk_submit(VkQueue q, VkCommandBuffer cmdbuf, VkSemaphore semwait, VkSemaphore semsig)
+{
+       /* TODO: investigate if we need to expose the wait stage */
+       VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
+       VkSubmitInfo sinf = {0};
+       sinf.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
+       sinf.waitSemaphoreCount = semwait ? 1 : 0;
+       sinf.pWaitSemaphores = &semwait;
+       sinf.pWaitDstStageMask = &wait_stage;
+       sinf.commandBufferCount = 1;
+       sinf.pCommandBuffers = &cmdbuf;
+       sinf.signalSemaphoreCount = semsig ? 1 : 0;
+       sinf.pSignalSemaphores = &semsig;
+
+       if(vkQueueSubmit(q, 1, &sinf, 0) != 0) {
+               fprintf(stderr, "failed to submit command buffer\n");
+               return -1;
+       }
+       return 0;
+}
+
+int vk_present(VkQueue q, int imgid, VkSemaphore semwait)
+{
+       VkPresentInfoKHR pinf = {0};
+
+       pinf.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
+       pinf.waitSemaphoreCount = semwait ? 1 : 0;
+       pinf.pWaitSemaphores = &semwait;
+       pinf.swapchainCount = 1;
+       pinf.pSwapchains = &vksc;
+       pinf.pImageIndices = (unsigned int*)&imgid;
+
+       if(vkQueuePresentKHR(q, &pinf) != 0) {
+               fprintf(stderr, "present failed\n");
+               return -1;
+       }
+       return 0;
+}
+
 int vk_find_qfamily(unsigned int flags)
 {
        int i, famidx = -1;
@@ -359,6 +408,16 @@ VkCommandBuffer vk_create_cmdbuf(unsigned int qflags, int level)
        return vk_create_cmdbuf_fam(qfam, level);
 }
 
+VkCommandBuffer vk_get_cmdbuf(int imgid)
+{
+       if(imgid < 0 || imgid >= vksc_numimg) {
+               fprintf(stderr, "vk_get_cmdbuf: invalid id %d, swap chain has %d images\n",
+                               imgid, vksc_numimg);
+               return 0;
+       }
+       return vksc_cmdbuf[imgid];
+}
+
 int vk_create_rpass(void)
 {
        int i;
@@ -452,6 +511,7 @@ VkRenderPass vk_rpass(int rp)
                        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;
@@ -460,23 +520,26 @@ VkRenderPass vk_rpass(int rp)
                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++) {
                        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;
+               subpass.pDepthStencilAttachment = 0;//&zatref;
 
                memset(&pinf, 0, sizeof pinf);
                pinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
-               pinf.attachmentCount = r->num_colbuf + 1;
+               pinf.attachmentCount = r->num_colbuf;// + 1;
                pinf.pAttachments = att;
                pinf.subpassCount = 1;
                pinf.pSubpasses = &subpass;
@@ -499,7 +562,7 @@ int vk_create_fb(void)
 
        if(!framebufs) {
                framebufs = darr_alloc(0, sizeof *framebufs);
-               darr_push(framebufs, &framebuf);        /* add dummy rpass */
+               darr_push(framebufs, &framebuf);        /* add dummy framebuffer */
        }
 
        for(i=1; i<darr_size(framebufs); i++) {
@@ -533,9 +596,11 @@ void vk_free_fb(int fb)
 
 void vk_fb_size(int fb, int x, int  y)
 {
-       framebufs[fb].width = x;
-       framebufs[fb].height = y;
-       framebufs[fb].vkobj_valid = 0;
+       if(x != framebufs[fb].width || y != framebufs[fb].height) {
+               framebufs[fb].width = x;
+               framebufs[fb].height = y;
+               framebufs[fb].vkobj_valid = 0;
+       }
 }
 
 void vk_fb_rpass(int fb, int rpass)
@@ -599,6 +664,7 @@ VkFramebuffer vk_fb(int fb)
                fbinf.pAttachments = f->imgv;
                fbinf.width = f->width;
                fbinf.height = f->height;
+               fbinf.layers = 1;
 
                if(vkCreateFramebuffer(vkdev, &fbinf, 0, &f->vkobj) != 0) {
                        fprintf(stderr, "vk_fb: failed to create framebuffer\n");
@@ -1006,8 +1072,19 @@ void vk_free_sem(VkSemaphore sem)
        vkDestroySemaphore(vkdev, sem, 0);
 }
 
+
+void vk_rect(VkRect2D *r, int x, int y, int w, int h)
+{
+       r->offset.x = x;
+       r->offset.y = y;
+       r->extent.width = w;
+       r->extent.height = h;
+}
+
+
 #define ARRSZ(arr)     (sizeof arr / sizeof *arr)
 static const char *known_layer_list[] = {
+       "VK_LAYER_KHRONOS_validation",
        "VK_LAYER_GOOGLE_threading",
        "VK_LAYER_LUNARG_parameter_validation",
        "VK_LAYER_LUNARG_object_tracker",