From fe054b7b9d000bd8f6c94cd569e1cb1836419e47 Mon Sep 17 00:00:00 2001 From: Jordan Halase Date: Thu, 7 Nov 2019 15:22:53 -0600 Subject: Separate surface configuration from swapchain --- main.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 0686583..2a7f242 100755 --- a/main.c +++ b/main.c @@ -185,7 +185,6 @@ struct VulkanDev { struct SwapchainVulkan { struct SwapchainVulkan *pNext; // Linked list of retired swapchains VkSwapchainKHR rawSwapchain; - VkSurfaceFormatKHR surfaceFormat; uint32_t nImages; VkExtent2D extent; VkImage *images; @@ -219,6 +218,8 @@ struct RenderVulkan { VkInstance instance; VkDebugReportCallbackEXT debugCallback; VkSurfaceKHR surface; + VkSurfaceCapabilitiesKHR surfaceCapabilities; + VkSurfaceFormatKHR surfaceFormat; VkPresentModeKHR presentMode; VkPhysicalDeviceProperties deviceProperties; // TODO: Make this a pointer. It's really big. VkPhysicalDevice physicalDevice; @@ -1233,13 +1234,13 @@ static bool isPresentModeSupported(const VkPresentModeKHR *const presentModes, return false; } -static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) +/** Configure the surface for the currently opened device. */ +int rvkConfigureSurface(struct RenderVulkan *vk) { VkSurfaceCapabilitiesKHR surfaceCapabilities; VkSurfaceFormatKHR *surfaceFormats; VkPresentModeKHR *presentModes; VkResult result; - /* TODO: Separate this preliminary stuff into a configureSurface() */ if ((result = vk->api->vkGetPhysicalDeviceSurfaceCapabilitiesKHR( vk->physicalDevice, vk->surface, &surfaceCapabilities))) { rvkSetErrMsg(vk, "Could not get surface capabilities: %d", result); @@ -1271,11 +1272,11 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }; if (surfaceFormats[i].format == VK_FORMAT_UNDEFINED) { - vk->swapchain->surfaceFormat = want; + vk->surfaceFormat = want; break; } if (surfaceFormats[i].format == want.format && surfaceFormats[i].colorSpace == want.colorSpace) { - vk->swapchain->surfaceFormat = want; + vk->surfaceFormat = want; break; } } @@ -1311,13 +1312,18 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) free(presentModes); vk->presentMode = presentMode; printf("Using present mode:\t\t`%s`\t(%d)\n", strPresentMode(presentMode), presentMode); + vk->surfaceCapabilities = surfaceCapabilities; + return 0; +} +static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) +{ // TODO: Clamp vk->swapchain->extent.width = width; vk->swapchain->extent.height = height; - vk->swapchain->nImages = surfaceCapabilities.minImageCount; - //vk->swapchain->nImages = surfaceCapabilities.maxImageCount; + vk->swapchain->nImages = vk->surfaceCapabilities.minImageCount; + //vk->swapchain->nImages = vk->surfaceCapabilities.maxImageCount; printf("Using %d swapchain images\n", vk->swapchain->nImages); @@ -1325,16 +1331,17 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.surface = vk->surface; createInfo.minImageCount = vk->swapchain->nImages; - createInfo.imageFormat = vk->swapchain->surfaceFormat.format; + createInfo.imageFormat = vk->surfaceFormat.format; createInfo.imageExtent = vk->swapchain->extent; createInfo.imageArrayLayers = 1; createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; - createInfo.preTransform = surfaceCapabilities.currentTransform; + createInfo.preTransform = vk->surfaceCapabilities.currentTransform; createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; createInfo.presentMode = vk->presentMode; createInfo.clipped = VK_TRUE; createInfo.oldSwapchain = VK_NULL_HANDLE; // TODO + VkResult result; if ((result = vk->dev->vkCreateSwapchainKHR(vk->device, &createInfo, ALLOC_VK, &vk->swapchain->rawSwapchain))) { rvkSetErrMsg(vk, "Could not create swapchain: %d", result); return -1; @@ -1344,7 +1351,7 @@ static int rvkCreateRawSwapchain(struct RenderVulkan *vk, int width, int height) static void rvkDestroyRawSwapchain(struct RenderVulkan *vk) { - if (vk->swapchain->rawSwapchain) { + if (vk->swapchain && vk->swapchain->rawSwapchain) { vk->dev->vkDestroySwapchainKHR(vk->device, vk->swapchain->rawSwapchain, ALLOC_VK); memset(&vk->swapchain, 0, sizeof(vk->swapchain)); } @@ -1392,6 +1399,7 @@ static void destroySwapchainImageViews(VkDevice device, const struct VulkanDev *const dev, struct SwapchainVulkan *const swapchain) { + if (!swapchain) return; if (swapchain->imageViews) { for (uint32_t i = 0; i < swapchain->nImages; ++i) { if (swapchain->imageViews[i]) { @@ -1531,7 +1539,7 @@ int rvkCreateSwapchain(struct RenderVulkan *vk, const int width, const int heigh void rvkDestroySwapchain(struct RenderVulkan *vk) { - if (vk->swapchain->fences) { + if (vk->swapchain && vk->swapchain->fences) { for (uint32_t i = 0; i < vk->swapchain->nImages; ++i) { if (vk->swapchain->fences[i]) { vk->dev->vkDestroyFence(vk->device, @@ -1779,6 +1787,7 @@ int main(int argc, char **argv) CHECK_RVK(vk, rvkCreateSurface(vk, frame)); CHECK_RVK(vk, rvkSelectPhysicalDevice(vk)); CHECK_RVK(vk, rvkOpenDevice(vk)); + CHECK_RVK(vk, rvkConfigureSurface(vk)); CHECK_RVK(vk, rvkCreateSwapchain(vk, frame.width, frame.height)); CHECK_RVK(vk, rvkCreateSyncObjects(vk)); -- cgit v1.2.1