From: John Tsiombikas Date: Wed, 11 Sep 2024 03:19:41 +0000 (+0300) Subject: solved all validation issues X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=commitdiff_plain;h=5c83a441b3faa8d3982ec0289a6e411ccd56d791;p=vktest3 solved all validation issues --- diff --git a/src/app.c b/src/app.c index 7fff41e..c242b32 100644 --- a/src/app.c +++ b/src/app.c @@ -3,24 +3,29 @@ #include "app.h" #include "vk.h" -struct swapchain { - int fb; - VkImageView img; +#define NUM_FRM 2 + +struct frame { + VkCommandBuffer cmdbuf; VkSemaphore sem_getimg; VkFence imgfence; }; int win_width, win_height; +static int frameno; + static int rpass, pipeln, num_swap_img; -static struct swapchain *swapchain; +static int *fb; +static struct frame frame[NUM_FRM]; static VkSemaphore sem_draw; static VkQueue queue; int app_init(void) { - int i; + int i, qfam; unsigned int flags; + VkCommandPool cmdpool; if(vk_init(VKINIT_DEPTH, &flags) == -1) { return -1; @@ -35,21 +40,34 @@ int app_init(void) if((num_swap_img = vk_num_swap_images()) <= 0) { return -1; } - if(!(swapchain = malloc(num_swap_img * sizeof *swapchain))) { + + if(!(fb = malloc(num_swap_img * sizeof *fb))) { + fprintf(stderr, "failed to allocate default framebuffers\n"); return -1; } rpass = vk_create_rpass(); for(i=0; iimgfence, -1); + imgid = vk_next_swap_image(frm->sem_getimg); + cmdbuf = frm->cmdbuf; { VkCommandBufferBeginInfo cmdbegin = {0}; @@ -105,7 +132,7 @@ void app_display(void) rpbegin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; rpbegin.renderPass = vk_rpass(rpass); - rpbegin.framebuffer = vk_fb(swapchain[imgid].fb); + rpbegin.framebuffer = vk_fb(fb[imgid]); vk_rect(&rpbegin.renderArea, 0, 0, win_width, win_height); rpbegin.pClearValues = clear; rpbegin.clearValueCount = 1; @@ -119,10 +146,12 @@ void app_display(void) } /* submit the command buffer, wait for one semaphore, signal another */ - vk_submit(queue, cmdbuf, swapchain[semid].sem_getimg, sem_draw); + vk_submit(queue, cmdbuf, frm->sem_getimg, sem_draw, frm->imgfence); /* swap buffers after drawing is finished */ vk_present(queue, imgid, sem_draw); + + frameno++; } void app_reshape(int x, int y) @@ -134,8 +163,8 @@ void app_reshape(int x, int y) } for(i=0; i= 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; @@ -1089,12 +1088,15 @@ void vk_free_sem(VkSemaphore sem) vkDestroySemaphore(vkdev, sem, 0); } -VkFence vk_create_fence(void) +VkFence vk_create_fence(int state) { VkFence fence; VkFenceCreateInfo finf = {0}; finf.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; + if(state) { + finf.flags = VK_FENCE_CREATE_SIGNALED_BIT; + } if(vkCreateFence(vkdev, &finf, 0, &fence) != 0) { return 0; } @@ -1106,6 +1108,12 @@ void vk_free_fence(VkFence fence) vkDestroyFence(vkdev, fence, 0); } +void vk_wait_fence(VkFence fence, long timeout) +{ + vkWaitForFences(vkdev, 1, &fence, VK_TRUE, timeout < 0 ? UINT64_MAX : timeout); + vkResetFences(vkdev, 1, &fence); +} + void vk_rect(VkRect2D *r, int x, int y, int w, int h) { r->offset.x = x; @@ -1430,7 +1438,7 @@ static int create_swapchain(void) scinf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; scinf.surface = vksurf; - scinf.minImageCount = 2; + scinf.minImageCount = vksurf_caps.minImageCount; scinf.imageFormat = vksurf_fmt[vksurf_selfmt].format; scinf.imageColorSpace = vksurf_fmt[vksurf_selfmt].colorSpace; scinf.imageExtent = vksc_extent; @@ -1484,39 +1492,6 @@ static int create_swapchain(void) return 0; } -static int create_default_cmdbuf(void) -{ - int qfam; - VkCommandPool cmdpool; - VkCommandBufferAllocateInfo cbinf = {0}; - - assert(!vksc_cmdbuf); - - if((qfam = vk_find_qfamily(VKQ_GFX | VKQ_PRESENT)) == -1) { - fprintf(stderr, "failed to find a gfx|present capable queue family\n"); - return -1; - } - if(!(cmdpool = find_cmdpool(qfam))) { - fprintf(stderr, "failed to find usable command pool for default command buffers\n"); - return -1; - } - if(!(vksc_cmdbuf = malloc(vksc_numimg * sizeof *vksc_cmdbuf))) { - fprintf(stderr, "failed to allocate %d command buffers for the swap-chain\n", vksc_numimg); - return -1; - } - - cbinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - cbinf.commandPool = cmdpool; - cbinf.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - cbinf.commandBufferCount = vksc_numimg; - - if(vkAllocateCommandBuffers(vkdev, &cbinf, vksc_cmdbuf) != 0) { - fprintf(stderr, "failed to create %d command buffers for the swap-chain\n", vksc_numimg); - return -1; - } - return 0; -} - static int eval_pdev_score(VkPhysicalDevice dev) { int score = 0; diff --git a/src/vk.h b/src/vk.h index 4beedf8..e987e96 100644 --- a/src/vk.h +++ b/src/vk.h @@ -71,7 +71,10 @@ VkImageView vk_swap_image(int idx); /* returns the image index, or -1 on failure. Pass optional semaphore to signal */ int vk_next_swap_image(VkSemaphore sem); -int vk_submit(VkQueue q, VkCommandBuffer cmdbuf, VkSemaphore semwait, VkSemaphore semsig); +void vk_finish(void); + +int vk_submit(VkQueue q, VkCommandBuffer cmdbuf, VkSemaphore semwait, + VkSemaphore semsig, VkFence fence); int vk_present(VkQueue q, int imgid, VkSemaphore semwait); int vk_find_qfamily(unsigned int flags); @@ -80,8 +83,6 @@ 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); -/* return one of the swap-chain command buffers */ -VkCommandBuffer vk_get_cmdbuf(int imgid); int vk_create_rpass(void); void vk_free_rpass(int rp); @@ -128,8 +129,9 @@ void vk_free_shader(VkShaderModule sdr); VkSemaphore vk_create_sem(void); void vk_free_sem(VkSemaphore sem); -VkFence vk_create_fence(void); +VkFence vk_create_fence(int state); void vk_free_fence(VkFence fence); +void vk_wait_fence(VkFence fence, long timeout); /* -1 = inf */ /* random helpers */ void vk_rect(VkRect2D *r, int x, int y, int w, int h);