tmp to switch computer: there are logical errors in main, needs fixing
[vkrt] / src / vk.c
index 5aabed1..a62fc18 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -892,8 +892,9 @@ alloc_memory(struct vk_ctx *ctx,
     mem_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
     mem_alloc_info.pNext = is_external ? &exp_mem_info : VK_NULL_HANDLE;
     mem_alloc_info.allocationSize = mem_reqs->size;
-    mem_alloc_info.memoryTypeIndex =
-        get_memory_type_idx(ctx->pdev, mem_reqs, prop_flags);
+    mem_alloc_info.memoryTypeIndex = get_memory_type_idx(ctx->pdev,
+                                                         mem_reqs,
+                                                         prop_flags);
 
     if (mem_alloc_info.memoryTypeIndex == UINT32_MAX) {
         fprintf(stderr, "No suitable memory type index found.\n");
@@ -917,7 +918,9 @@ alloc_memory(struct vk_ctx *ctx,
 }
 
 static bool
-alloc_image_memory(struct vk_ctx *ctx, bool is_external, struct vk_image_obj *img_obj)
+alloc_image_memory(struct vk_ctx *ctx,
+                   bool is_external,
+                   struct vk_image_obj *img_obj)
 {
     VkMemoryDedicatedRequirements ded_reqs;
     VkImageMemoryRequirementsInfo2 req_info2;
@@ -1129,7 +1132,6 @@ sc_select_supported_present_modes(struct vk_ctx *ctx,
 {
     VkPresentModeKHR *present_modes;
     uint32_t num_present_modes;
-    int i;
 
     /* find supported present modes */
     if (vkGetPhysicalDeviceSurfacePresentModesKHR(ctx->pdev, surf, &num_present_modes, 0) != VK_SUCCESS || !num_present_modes) {
@@ -1149,20 +1151,21 @@ sc_select_supported_present_modes(struct vk_ctx *ctx,
     }
 
     s_info->presentMode = VK_PRESENT_MODE_FIFO_KHR;
+#if 0
     if (!has_vsync) {
         for (i = 0; i < num_present_modes; i++) {
             if (present_modes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
                 s_info->presentMode = present_modes[i];
-                goto success;
+                break;
             }
             if (present_modes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR) {
                 s_info->presentMode = present_modes[i];
-                goto success;
+                break;
             }
         }
     }
+#endif
 
-success:
     free(present_modes);
     return true;
 }
@@ -1194,7 +1197,7 @@ vk_init_ctx(struct vk_ctx *ctx,
     return true;
 
 fail:
-    vk_cleanup_ctx(ctx, enable_layers);
+    vk_cleanup_ctx(ctx);
     return false;
 }
 
@@ -1229,31 +1232,26 @@ vk_init_ctx_for_rendering(struct vk_ctx *ctx,
     return true;
 
 fail:
-    vk_cleanup_ctx(ctx, enable_layers);
+    vk_cleanup_ctx(ctx);
     return false;
 }
 
 void
-vk_destroy_cmd_bufs(struct vk_ctx *ctx,
-                    uint32_t num_buffers,
-                    VkCommandBuffer *buffers)
+vk_destroy_cmd_buffers(struct vk_ctx *ctx,
+                       uint32_t num_buffers,
+                       VkCommandBuffer *buffers)
 {
     int i;
     for (i = 0; i < num_buffers; i++) {
         vkResetCommandBuffer(buffers[i],
                              VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
     }
-    vkFreeCommandBuffers(ctx->dev, ctx->cmd_pool, num_buffers, &buffers[i]);
+    vkFreeCommandBuffers(ctx->dev, ctx->cmd_pool, num_buffers, buffers);
 }
 
 void
-vk_cleanup_ctx(struct vk_ctx *ctx,
-               bool enable_layers)
+vk_cleanup_ctx(struct vk_ctx *ctx)
 {
-    if (enable_layers) {
-        return;
-    }
-
     if (ctx->cmd_pool != VK_NULL_HANDLE) {
         vkResetCommandPool(ctx->dev, ctx->cmd_pool,
                            VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT);
@@ -1299,7 +1297,7 @@ vk_create_image(struct vk_ctx *ctx,
     img_info.tiling = props->tiling;
     img_info.usage = props->usage ?
                      props->usage : VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
-    img_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+    img_info.initialLayout = props->in_layout;
     img_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
 
     if (vkCreateImage(ctx->dev, &img_info, 0, &img->img) != VK_SUCCESS)
@@ -1625,7 +1623,7 @@ fail:
 
 void
 vk_destroy_buffer(struct vk_ctx *ctx,
-          struct vk_buf *bo)
+                  struct vk_buf *bo)
 {
     if (bo->buf != VK_NULL_HANDLE)
         vkDestroyBuffer(ctx->dev, bo->buf, 0);
@@ -1638,6 +1636,25 @@ vk_destroy_buffer(struct vk_ctx *ctx,
     bo->mobj.mem = VK_NULL_HANDLE;
 }
 
+bool
+vk_create_fence(struct vk_ctx *ctx,
+                VkFence *fence)
+{
+    VkFenceCreateInfo finfo;
+
+    memset(&finfo, 0, sizeof finfo);
+    finfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
+    finfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
+
+    if (vkCreateFence(ctx->dev, &finfo, 0, fence) != VK_SUCCESS) {
+        fprintf(stderr, "Failed to create fence.\n");
+        fence = 0;
+        return false;
+    }
+
+    return true;
+}
+
 VkCommandBuffer
 vk_create_cmd_buffer(struct vk_ctx *ctx)
 {
@@ -1675,6 +1692,7 @@ vk_record_cmd_buffer(struct vk_ctx *ctx,
     VkDeviceSize offsets[] = {0};
     int num_vertices;
     struct vk_dims img_size;
+    bool create_cmd_buf = false;
 
     assert(vk_fb_color_count == 4);
 
@@ -1685,6 +1703,7 @@ vk_record_cmd_buffer(struct vk_ctx *ctx,
             fprintf(stderr, "Failed to create command buffer.\n");
             return false;
         }
+        create_cmd_buf = true;
     }
 
     /* VkCommandBufferBeginInfo */
@@ -1799,6 +1818,9 @@ vk_record_cmd_buffer(struct vk_ctx *ctx,
 #endif
 
     vkEndCommandBuffer(cmd_buf);
+    if (create_cmd_buf) {
+        vk_destroy_cmd_buffers(ctx, 1, &cmd_buf);
+    }
     return true;
 }
 
@@ -1835,9 +1857,13 @@ vk_draw(struct vk_ctx *ctx,
 
     if (vkQueueSubmit(ctx->queue, 1, &submit_info, VK_NULL_HANDLE) != VK_SUCCESS) {
         fprintf(stderr, "Failed to submit queue.\n");
+        abort();
     }
 
-    vkQueueWaitIdle(ctx->queue);
+    if (vkQueueWaitIdle(ctx->queue) != VK_SUCCESS) {
+        fprintf(stderr, "Failed to wait idle.\n");
+        abort();
+    }
 }
 
 void
@@ -2007,6 +2033,33 @@ vk_clear_color(struct vk_ctx *ctx,
         vkQueueWaitIdle(ctx->queue);
 }
 
+void
+vk_set_viewport(struct vk_ctx *ctx,
+                VkCommandBuffer cmd_buf,
+                float x, float y,
+                float w, float h,
+                float near, float far)
+{
+    VkCommandBufferBeginInfo binfo;
+    VkViewport viewport;
+
+    memset(&viewport, 0, sizeof viewport);
+    viewport.x = x;
+    viewport.y = y;
+    viewport.width = w;
+    viewport.height = h;
+    viewport.minDepth = near;
+    viewport.maxDepth = far;
+
+    memset(&binfo, 0, sizeof binfo);
+    binfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
+    binfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
+
+    vkBeginCommandBuffer(cmd_buf, &binfo);
+    vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
+    vkEndCommandBuffer(cmd_buf);
+}
+
 bool
 vk_create_swapchain(struct vk_ctx *ctx,
                     int width, int height,
@@ -2155,11 +2208,10 @@ vk_destroy_swapchain(struct vk_ctx *ctx,
                      struct vk_swapchain *swapchain)
 {
     vkDestroySwapchainKHR(ctx->dev, swapchain->swapchain, 0);
-    swapchain = 0;
 }
 
 bool
-vk_present_queue(struct vk_swapchain *swapchain,
+vk_queue_present(struct vk_swapchain *swapchain,
                  VkQueue queue,
                  uint32_t image_idx,
                  VkSemaphore wait_sema)
@@ -2169,12 +2221,11 @@ vk_present_queue(struct vk_swapchain *swapchain,
     memset(&pinfo, 0, sizeof pinfo);
     pinfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
     pinfo.swapchainCount = 1;
+    pinfo.pSwapchains = &swapchain->swapchain;
     pinfo.pImageIndices = &image_idx;
 
-    if (wait_sema != VK_NULL_HANDLE) {
-        pinfo.pWaitSemaphores = &wait_sema;
-        pinfo.waitSemaphoreCount = 1;
-    }
+    pinfo.pWaitSemaphores = &wait_sema;
+    pinfo.waitSemaphoreCount = 1;
 
     if (vkQueuePresentKHR(queue, &pinfo) != VK_SUCCESS) {
         fprintf(stderr, "Failed to present queue.\n");