From 526b6a7f13b31bab2b022eb9f74ec653ce4d4cd0 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sat, 5 Jun 2021 12:32:39 +0300 Subject: [PATCH] moved ui.[hc] to vk.[hc] no reason to have separate files for the swapchain --- src/ui.c | 57 --------------------------------------------------------- src/ui.h | 25 ------------------------- src/vk.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/vk.h | 20 ++++++++++++++++++++ 4 files changed, 73 insertions(+), 82 deletions(-) delete mode 100644 src/ui.c delete mode 100644 src/ui.h diff --git a/src/ui.c b/src/ui.c deleted file mode 100644 index fd00364..0000000 --- a/src/ui.c +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include -#include -#include "ui.h" - -bool -vk_create_swapchain(struct vk_ctx *ctx, - int width, int height, - int num_qfam, - struct vk_swapchain *swapchain) -{ - VkSwapchainCreateInfoKHR s_info; - VkExtent2D extent; - - extent.width = width; - extent.height = height; - - memset(&s_info, 0, sizeof s_info); - - s_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - s_info.flags = 0; - s_info.surface = swapchain->surface; - s_info.minImageCount = 2; - s_info.imageFormat = VK_FORMAT_R32G32B32A32_SFLOAT; - s_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; - s_info.imageExtent = extent; - s_info.imageArrayLayers = 1; - s_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | - VK_IMAGE_USAGE_SAMPLED_BIT | - VK_IMAGE_USAGE_TRANSFER_SRC_BIT | - VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - s_info.imageSharingMode = VK_SHARING_MODE_CONCURRENT; - s_info.queueFamilyIndexCount = num_qfam; /* how many queue families */ - - s_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; - s_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; - s_info.presentMode = VK_PRESENT_MODE_FIFO_KHR; - s_info.clipped = VK_TRUE; - s_info.oldSwapchain = VK_NULL_HANDLE; - - if (vkCreateSwapchainKHR(ctx->dev, &s_info, 0, - &swapchain->swapchain) != VK_SUCCESS) { - fprintf(stderr, "Failed to create a swapchain.\n"); - return false; - } - - return true; -} - -void -vk_destroy_swapchain(struct vk_ctx *ctx, - struct vk_swapchain *swapchain) -{ - vkDestroySwapchainKHR(ctx->dev, swapchain->swapchain, 0); - vkDestroySurfaceKHR(ctx->inst, swapchain->surface, 0); -} diff --git a/src/ui.h b/src/ui.h deleted file mode 100644 index d26f4f0..0000000 --- a/src/ui.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef UI_H -#define UI_H - -#include "vk.h" - -struct vk_swapchain -{ - VkSwapchainKHR swapchain; - VkSurfaceKHR surface; - VkSurfaceFormatKHR surf_fmt; - uint32_t num_images; - VkExtent2D extent2d; -}; - -/* swapchain */ -bool -vk_create_swapchain(struct vk_ctx *ctx, - int width, int height, - int num_qfam, - struct vk_swapchain *swapchain); -void -vk_destroy_swapchain(struct vk_ctx *ctx, - struct vk_swapchain *swapchain); - -#endif /* UI_H */ diff --git a/src/vk.c b/src/vk.c index 4e3aa1c..81e0f51 100644 --- a/src/vk.c +++ b/src/vk.c @@ -1750,6 +1750,59 @@ vk_clear_color(struct vk_ctx *ctx, vkQueueWaitIdle(ctx->queue); } +bool +vk_create_swapchain(struct vk_ctx *ctx, + int width, int height, + int num_qfam, + struct vk_swapchain *swapchain) +{ + VkSwapchainCreateInfoKHR s_info; + VkExtent2D extent; + + extent.width = width; + extent.height = height; + + memset(&s_info, 0, sizeof s_info); + + s_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; + s_info.flags = 0; + s_info.surface = swapchain->surface; + s_info.minImageCount = 2; + s_info.imageFormat = VK_FORMAT_R32G32B32A32_SFLOAT; + s_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; + s_info.imageExtent = extent; + s_info.imageArrayLayers = 1; + s_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_SAMPLED_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT; + + s_info.imageSharingMode = VK_SHARING_MODE_CONCURRENT; + s_info.queueFamilyIndexCount = num_qfam; /* how many queue families */ + + s_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; + s_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + s_info.presentMode = VK_PRESENT_MODE_FIFO_KHR; + s_info.clipped = VK_TRUE; + s_info.oldSwapchain = VK_NULL_HANDLE; + + if (vkCreateSwapchainKHR(ctx->dev, &s_info, 0, + &swapchain->swapchain) != VK_SUCCESS) { + fprintf(stderr, "Failed to create a swapchain.\n"); + return false; + } + + return true; +} + +void +vk_destroy_swapchain(struct vk_ctx *ctx, + struct vk_swapchain *swapchain) +{ + vkDestroySwapchainKHR(ctx->dev, swapchain->swapchain, 0); + vkDestroySurfaceKHR(ctx->inst, swapchain->surface, 0); +} + void vk_copy_image_to_buffer(struct vk_ctx *ctx, struct vk_image_att *src_img, diff --git a/src/vk.h b/src/vk.h index 7380a40..140f525 100644 --- a/src/vk.h +++ b/src/vk.h @@ -21,6 +21,15 @@ struct vk_ctx uint8_t driverUUID[VK_UUID_SIZE]; }; +struct vk_swapchain +{ + VkSwapchainKHR swapchain; + VkSurfaceKHR surface; + VkSurfaceFormatKHR surf_fmt; + uint32_t num_images; + VkExtent2D extent2d; +}; + struct vk_image_props { uint32_t w; @@ -205,6 +214,17 @@ vk_clear_color(struct vk_ctx *ctx, uint32_t n_attachments, float x, float y, float w, float h); +/* swapchain */ + +bool +vk_create_swapchain(struct vk_ctx *ctx, + int width, int height, + int num_qfam, + struct vk_swapchain *swapchain); +void +vk_destroy_swapchain(struct vk_ctx *ctx, + struct vk_swapchain *swapchain); + /* transitions */ void -- 1.7.10.4