fixes in image structs and functions
[vkrt] / src / vk.c
index de807a4..b4e9eb7 100644 (file)
--- a/src/vk.c
+++ b/src/vk.c
@@ -466,8 +466,8 @@ create_image_view(struct vk_ctx *ctx,
 
 static void
 create_framebuffer(struct vk_ctx *ctx,
-                   struct vk_image_att *color_att,
-                   struct vk_image_att *depth_att,
+                   struct vk_image_attachment *color_att,
+                   struct vk_image_attachment *depth_att,
                    struct vk_renderer *renderer)
 {
     VkImageSubresourceRange sr;
@@ -499,10 +499,12 @@ create_framebuffer(struct vk_ctx *ctx,
     sr.layerCount = color_att->props.num_layers;
 
     /* color view */
-    if (!create_image_view(ctx, color_att->obj.img, view_type, color_att->props.format, sr, false, &color_att->obj.img_view)) {
-        fprintf(stderr, "Failed to create color image view for framebuffer.\n");
-        vk_destroy_image(ctx, &color_att->obj);
-        goto fail;
+    if (!color_att->obj.img_view) {
+        if (!create_image_view(ctx, color_att->obj.img, view_type, color_att->props.format, sr, false, &color_att->obj.img_view)) {
+            fprintf(stderr, "Failed to create color image view for framebuffer.\n");
+            vk_destroy_image(ctx, &color_att->obj);
+            goto fail;
+        }
     }
 
     /* depth view */
@@ -513,17 +515,19 @@ create_framebuffer(struct vk_ctx *ctx,
     sr.baseArrayLayer = 0;
     sr.layerCount = depth_att->props.num_layers ? depth_att->props.num_layers : 1;
 
-    if (!create_image_view(ctx, depth_att->obj.img,
-                           depth_att->props.num_layers > 1 ?
-                           VK_IMAGE_VIEW_TYPE_2D_ARRAY :
-                           VK_IMAGE_VIEW_TYPE_2D,
-                           depth_att->props.format,
-                           sr,
-                           false,
-                           &depth_att->obj.img_view)) {
-        fprintf(stderr, "Failed to create depth image view for framebuffer.\n");
-        vk_destroy_image(ctx, &depth_att->obj);
-        goto fail;
+    if (!depth_att->obj.img_view) {
+        if (!create_image_view(ctx, depth_att->obj.img,
+                               depth_att->props.num_layers > 1 ?
+                               VK_IMAGE_VIEW_TYPE_2D_ARRAY :
+                               VK_IMAGE_VIEW_TYPE_2D,
+                               depth_att->props.format,
+                               sr,
+                               false,
+                               &depth_att->obj.img_view)) {
+            fprintf(stderr, "Failed to create depth image view for framebuffer.\n");
+            vk_destroy_image(ctx, &depth_att->obj);
+            goto fail;
+        }
     }
 
     atts[0] = color_att->obj.img_view;
@@ -856,7 +860,7 @@ alloc_memory(struct vk_ctx *ctx,
 
     memset(&mem_alloc_info, 0, sizeof mem_alloc_info);
     mem_alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
-    mem_alloc_info.pNext = &exp_mem_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);
@@ -883,7 +887,7 @@ alloc_memory(struct vk_ctx *ctx,
 }
 
 static bool
-alloc_image_memory(struct vk_ctx *ctx, 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;
@@ -904,13 +908,13 @@ alloc_image_memory(struct vk_ctx *ctx, struct vk_image_obj *img_obj)
 
     vkGetImageMemoryRequirements2(ctx->dev, &req_info2, &mem_reqs2);
     img_obj->mobj.mem = alloc_memory(ctx,
-                                     true, /* is_external = FIXME */
+                                     is_external,
                                      &mem_reqs2.memoryRequirements,
                                      ded_reqs.requiresDedicatedAllocation ?
                                         img_obj->img : VK_NULL_HANDLE,
                                      VK_NULL_HANDLE,
                                      mem_reqs2.memoryRequirements.memoryTypeBits &
-                                        VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
+                                     VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
 
     img_obj->mobj.mem_sz = mem_reqs2.memoryRequirements.size;
     img_obj->mobj.dedicated = ded_reqs.requiresDedicatedAllocation;
@@ -1250,7 +1254,7 @@ vk_create_image(struct vk_ctx *ctx,
     if (vkCreateImage(ctx->dev, &img_info, 0, &img->img) != VK_SUCCESS)
         goto fail;
 
-    if(!alloc_image_memory(ctx, img))
+    if(!alloc_image_memory(ctx, props->need_export, img))
         goto fail;
 
     return true;
@@ -1313,7 +1317,7 @@ vk_create_ext_image(struct vk_ctx *ctx,
     if (vkCreateImage(ctx->dev, &img_info, 0, &img->img) != VK_SUCCESS)
         goto fail;
 
-    if(!alloc_image_memory(ctx, img))
+    if(!alloc_image_memory(ctx, true, img))
         goto fail;
 
     return true;
@@ -1327,19 +1331,19 @@ fail:
 }
 
 bool
-vk_fill_ext_image_props(struct vk_ctx *ctx,
-                        uint32_t w,
-                        uint32_t h,
-                        uint32_t d,
-                        uint32_t num_samples,
-                        uint32_t num_levels,
-                        uint32_t num_layers,
-                        VkFormat format,
-                        VkImageTiling tiling,
-                        VkImageLayout in_layout,
-                        VkImageLayout end_layout,
-                        bool need_export,
-                        struct vk_image_props *props)
+vk_fill_image_props(struct vk_ctx *ctx,
+                    uint32_t w,
+                    uint32_t h,
+                    uint32_t d,
+                    uint32_t num_samples,
+                    uint32_t num_levels,
+                    uint32_t num_layers,
+                    VkFormat format,
+                    VkImageTiling tiling,
+                    VkImageLayout in_layout,
+                    VkImageLayout end_layout,
+                    bool need_export,
+                    struct vk_image_props *props)
 {
     props->w = w;
     props->h = h;
@@ -1370,8 +1374,8 @@ vk_create_renderer(struct vk_ctx *ctx,
                    unsigned int fs_size,
                    bool enable_depth,
                    bool enable_stencil,
-                   struct vk_image_att *color_att,
-                   struct vk_image_att *depth_att,
+                   struct vk_image_attachment *color_att,
+                   struct vk_image_attachment *depth_att,
                    struct vk_vertex_info *vert_info,
                    struct vk_renderer *renderer)
 {
@@ -1573,7 +1577,7 @@ vk_draw(struct vk_ctx *ctx,
         float *vk_fb_color,
         uint32_t vk_fb_color_count,
         struct vk_semaphores *semaphores,
-        struct vk_image_att *attachments,
+        struct vk_image_attachment *attachments,
         uint32_t n_attachments,
         float x, float y,
         float w, float h)
@@ -1679,7 +1683,7 @@ vk_draw(struct vk_ctx *ctx,
             calloc(n_attachments, sizeof(VkImageMemoryBarrier));
         VkImageMemoryBarrier *barrier = barriers;
         for (uint32_t n = 0; n < n_attachments; n++, barrier++) {
-            struct vk_image_att *att = &attachments[n];
+            struct vk_image_attachment *att = &attachments[n];
             VkImageAspectFlagBits depth_stencil_flags =
                 get_aspect_from_depth_format(att->props.format);
             bool is_depth = (depth_stencil_flags != 0);
@@ -1732,7 +1736,7 @@ vk_clear_color(struct vk_ctx *ctx,
                uint32_t vk_fb_color_count,
                struct vk_semaphores *semaphores,
                bool has_wait, bool has_signal,
-               struct vk_image_att *attachments,
+               struct vk_image_attachment *attachments,
                uint32_t n_attachments,
                float x, float y,
                float w, float h)
@@ -1844,7 +1848,7 @@ vk_clear_color(struct vk_ctx *ctx,
         VkImageMemoryBarrier *barrier = barriers;
 
         for (uint32_t n = 0; n < n_attachments; n++, barrier++) {
-            struct vk_image_att *att = &attachments[n];
+            struct vk_image_attachment *att = &attachments[n];
 
             /* Insert barrier to mark ownership transfer. */
             barrier->sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
@@ -1981,7 +1985,7 @@ vk_create_swapchain(struct vk_ctx *ctx,
      * the old swapchain and clean up the images */
     if (old_swapchain) {
         for (i = 0; i < old_swapchain->num_images; i++) {
-            vkDestroyImageView(ctx->dev, old_swapchain->images[i].image_view, 0);
+            vkDestroyImageView(ctx->dev, old_swapchain->images[i].img_view, 0);
         }
         vk_destroy_swapchain(ctx, old_swapchain);
     }
@@ -1997,7 +2001,7 @@ vk_create_swapchain(struct vk_ctx *ctx,
     vkGetSwapchainImagesKHR(ctx->dev, swapchain->swapchain, &swapchain->num_images, s_images);
 
     swapchain->image_fmt = s_info.imageFormat;
-    swapchain->images = malloc(swapchain->num_images * sizeof(struct vk_swap_image_obj));
+    swapchain->images = malloc(swapchain->num_images * sizeof(struct vk_image_obj));
 
     memset(&sr, 0, sizeof sr);
     sr.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
@@ -2005,24 +2009,26 @@ vk_create_swapchain(struct vk_ctx *ctx,
     sr.layerCount = 1;
 
     for (i = 0; i < swapchain->num_images; i++) {
-        swapchain->images[i].image = s_images[i];
+        swapchain->images[i].img = s_images[i];
         if (!(create_image_view(ctx,
-                                swapchain->images[i].image,
+                                swapchain->images[i].img,
                                 VK_IMAGE_VIEW_TYPE_2D,
                                 swapchain->image_fmt,
                                 sr,
                                 true,
-                                &swapchain->images[i].image_view))) {
+                                &swapchain->images[i].img_view))) {
             fprintf(stderr, "Fail to create an image view from the swapchain image: i=%d\n", i);
             break;
         }
         if (i < swapchain->num_images - 1) {
             int j;
             for (j = 0; j < i; j++) {
-                vkDestroyImageView(ctx->dev, swapchain->images[i].image_view, 0);
+                vkDestroyImageView(ctx->dev, swapchain->images[i].img_view, 0);
             }
             return false;
         }
+
+        memset(&swapchain->images[i].mobj, 0, sizeof swapchain->images[i].mobj);
     }
 
     free(s_images);
@@ -2042,7 +2048,7 @@ vk_destroy_swapchain(struct vk_ctx *ctx,
 
 void
 vk_copy_image_to_buffer(struct vk_ctx *ctx,
-                        struct vk_image_att *src_img,
+                        struct vk_image_attachment *src_img,
                         struct vk_buf *dst_bo,
                         float w, float h)
 {
@@ -2123,7 +2129,6 @@ vk_copy_image_to_buffer(struct vk_ctx *ctx,
     vkQueueWaitIdle(ctx->queue);
 }
 
-// FIXME: external
 bool
 vk_create_semaphores(struct vk_ctx *ctx,
                      bool is_external,
@@ -2167,13 +2172,57 @@ vk_destroy_semaphores(struct vk_ctx *ctx,
         vkDestroySemaphore(ctx->dev, semaphores->frame_done, 0);
 }
 
+bool
+vk_create_fences(struct vk_ctx *ctx,
+                 int num_cmd_buf,
+                 VkFenceCreateFlagBits flags,
+                 VkFence *fences)
+{
+    VkFenceCreateInfo f_info;
+    int i, j = -1;
+
+    memset(&f_info, 0, sizeof f_info);
+    f_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
+    f_info.flags = flags ? flags : VK_FENCE_CREATE_SIGNALED_BIT;
+
+
+    fences = malloc(num_cmd_buf * sizeof(VkFence));
+    for (i = 0; i < num_cmd_buf; i++) {
+        if (vkCreateFence(ctx->dev, &f_info, 0, &fences[i]) != VK_SUCCESS) {
+            fprintf(stderr, "Failed to create fence number: %d\n", (i + 1));
+            j = i;
+            break;
+        }
+    }
+
+    if (j == i) {
+        for (i = 0; i < j; i++) {
+            vkDestroyFence(ctx->dev, fences[i], 0);
+        }
+        return false;
+    }
+
+    return true;
+}
+
+void
+vk_destroy_fences(struct vk_ctx *ctx,
+                  int num_fences,
+                  VkFence *fences)
+{
+    int i;
+    for (i = 0; i < num_fences; i++) {
+        vkDestroyFence(ctx->dev, fences[i], 0);
+    }
+}
+
 void
-vk_transition_image_layout(struct vk_image_att *img_att,
-               VkCommandBuffer cmd_buf,
-               VkImageLayout old_layout,
-               VkImageLayout new_layout,
-               uint32_t src_queue_fam_idx,
-               uint32_t dst_queue_fam_idx)
+vk_transition_image_layout(struct vk_image_attachment *img_att,
+                           VkCommandBuffer cmd_buf,
+                           VkImageLayout old_layout,
+                           VkImageLayout new_layout,
+                           uint32_t src_queue_fam_idx,
+                           uint32_t dst_queue_fam_idx)
 {
     VkImageMemoryBarrier barrier;
     struct vk_image_props props = img_att->props;