From 9d56322eb3c2c9967ab988faeb26e86be9661e34 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sat, 28 Aug 2021 23:28:02 +0300 Subject: [PATCH] fixes in image structs and functions - changed swapchain to use image objects - changed memory allocation to not assume images are external - renamed fill_ext_image_props to fill_image_props - removed the image view creation from the swapchain creation, renderer is going to create them when the fb is created --- src/main.c | 24 ++++++++- src/vk.c | 161 +++++++++++++++++++++++++++++++++++++++--------------------- src/vk.h | 61 ++++++++++++----------- 3 files changed, 160 insertions(+), 86 deletions(-) diff --git a/src/main.c b/src/main.c index b0a5ec4..6309b41 100644 --- a/src/main.c +++ b/src/main.c @@ -41,8 +41,10 @@ static VkSurfaceKHR vk_surf; static int vsz, fsz; static struct vk_renderer vk_rnd; static struct vk_swapchain vk_chain; - static struct vk_semaphores vk_sema; +static struct vk_image_attachment vk_color_att; +static struct vk_image_attachment vk_depth_att; +static struct vk_image_props vk_depth_att_props; /* empty for as long as we hardcode the vertices in the vertex shader */ static struct vk_vertex_info vk_vert_info; @@ -127,9 +129,27 @@ init() vsdr = sdr_load("data/main.vert.spv", &vsz); fsdr = sdr_load("data/main.frag.spv", &fsz); + /* create depth attachment (for the moment we are going to use this + * for all images */ + if (!vk_fill_image_props(&vk_core, + win_w, win_h, 1, + 1, 1, 1, + VK_FORMAT_D24_UNORM_S8_UINT, + VK_IMAGE_TILING_OPTIMAL, + VK_IMAGE_LAYOUT_UNDEFINED, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + false, &vk_depth_att_props)) { + fprintf(stderr, "Unsupported depth image properties\n"); + return false; + } + if (!vk_create_image(&vk_core, &vk_depth_att_props, &vk_depth_att.obj)) { + fprintf(stderr, "Failed to create depth attachment.\n"); + return false; + } + /* create renderer */ if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz, - false, false, 0, 0, + false, false, 0, &vk_depth_att, &vk_vert_info, &vk_rnd)) { fprintf(stderr, "Failed to create renderer.\n"); return false; diff --git a/src/vk.c b/src/vk.c index de807a4..b4e9eb7 100644 --- 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; diff --git a/src/vk.h b/src/vk.h index ff5c025..259240b 100644 --- a/src/vk.h +++ b/src/vk.h @@ -21,12 +21,6 @@ struct vk_ctx uint8_t driverUUID[VK_UUID_SIZE]; }; -struct vk_swap_image_obj -{ - VkImage image; - VkImageView image_view; -}; - struct vk_swapchain { VkSwapchainKHR swapchain; @@ -38,7 +32,7 @@ struct vk_swapchain VkExtent2D extent2d; uint32_t num_images; - struct vk_swap_image_obj *images; + struct vk_image_obj *images; }; struct vk_image_props @@ -75,7 +69,7 @@ struct vk_image_obj { struct vk_mem_obj mobj; }; -struct vk_image_att { +struct vk_image_attachment { struct vk_image_obj obj; struct vk_image_props props; }; @@ -135,25 +129,25 @@ vk_destroy_image(struct vk_ctx *ctx, struct vk_image_obj *img_obj); bool -vk_fill_ext_image_props(struct vk_ctx *ctx, - uint32_t w, uint32_t h, - uint32_t depth, - 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 depth, + 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); bool vk_create_attachment_from_swapchain_image(struct vk_ctx *ctx, VkImage *swapchain_img, VkImageView *swapchain_view, struct vk_image_props *swapchain_props, - struct vk_image_att *color_att); + struct vk_image_attachment *color_att); /* buffers */ @@ -180,7 +174,7 @@ vk_create_ext_buffer(struct vk_ctx *ctx, struct vk_buf *bo); -/* semaphores */ +/* semaphores and fences */ bool vk_create_semaphores(struct vk_ctx *ctx, @@ -190,6 +184,17 @@ void vk_destroy_semaphores(struct vk_ctx *ctx, struct vk_semaphores *semaphores); +bool +vk_create_fences(struct vk_ctx *ctx, + int num_cmd_buf, + VkFenceCreateFlagBits flags, + VkFence *fences); + +void +vk_destroy_fences(struct vk_ctx *ctc, + int num_fences, + VkFence *fences); + /* renderer */ bool @@ -200,8 +205,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); @@ -218,7 +223,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); @@ -230,7 +235,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); @@ -251,12 +256,12 @@ 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); void -vk_transition_image_layout(struct vk_image_att *img_att, +vk_transition_image_layout(struct vk_image_attachment *img_att, VkCommandBuffer cmd_buf, VkImageLayout old_layout, VkImageLayout new_layout, -- 1.7.10.4