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;
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 */
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;
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);
}
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;
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;
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;
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;
}
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;
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)
{
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)
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);
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)
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;
* 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);
}
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;
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);
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)
{
vkQueueWaitIdle(ctx->queue);
}
-// FIXME: external
bool
vk_create_semaphores(struct vk_ctx *ctx,
bool is_external,
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;
uint8_t driverUUID[VK_UUID_SIZE];
};
-struct vk_swap_image_obj
-{
- VkImage image;
- VkImageView image_view;
-};
-
struct vk_swapchain
{
VkSwapchainKHR swapchain;
VkExtent2D extent2d;
uint32_t num_images;
- struct vk_swap_image_obj *images;
+ struct vk_image_obj *images;
};
struct vk_image_props
struct vk_mem_obj mobj;
};
-struct vk_image_att {
+struct vk_image_attachment {
struct vk_image_obj obj;
struct vk_image_props props;
};
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 */
struct vk_buf *bo);
-/* semaphores */
+/* semaphores and fences */
bool
vk_create_semaphores(struct vk_ctx *ctx,
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
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);
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);
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);
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,