I am always stuck at the swapchain creation, and I don't know why. I enabled the validation layers, and the best anwser I got is:
- vkCreateSwapchainKHR: internal drawable creation failed
I have an Nvidia GTX960 card. i ran some vulkan samples on it so, It must support vulkan.
Here is my swapchain creator function:
void Renderer::createSwapChain(VkSwapchainKHR *swapchain,VkPhysicalDevice *dev,VkDevice *vulk_dev,VkSurfaceKHR *surface, uint32_t family_index,VkExtent2D *extent) {
uint32_t format_count;
VkFormat format;
VkBool32 support;
vkGetPhysicalDeviceSurfaceSupportKHR(*dev, family_index, *surface, &support);
if (!support) {
fprintf(*Renderer::error_log, "%d :Surface is not supported.", __LINE__);
fclose(*Renderer::error_log);
exit(-1);
}
vkGetPhysicalDeviceSurfaceFormatsKHR(*dev, *surface, &format_count, nullptr);
vector<VkSurfaceFormatKHR> surface_format(format_count);
vkGetPhysicalDeviceSurfaceFormatsKHR(*dev, *surface, &format_count, surface_format.data());
if( 1 == format_count && surface_format[0].format == VK_FORMAT_UNDEFINED) {
format = VK_FORMAT_B8G8R8A8_UNORM;
infos.format.color_format = VK_FORMAT_B8G8R8A8_UNORM;
}else {
format = surface_format[0].format;
}
VkFormat depth_format = VK_FORMAT_D16_UNORM;
VkFormatProperties format_props;
vkGetPhysicalDeviceFormatProperties(*dev, depth_format, &format_props);
if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
infos.tiling = VK_IMAGE_TILING_LINEAR;
}else if (format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
infos.tiling = VK_IMAGE_TILING_OPTIMAL;
}else {
fprintf(*Renderer::error_log, "%d: VK_FORMAT_D16_UNORM is not supported",__LINE__);
fclose(*Renderer::error_log);
exit(-1);
}
VkPresentModeKHR present_mode_selected = VK_PRESENT_MODE_FIFO_KHR;
uint32_t present_modes_c;
vkGetPhysicalDeviceSurfacePresentModesKHR(*dev, *surface, &present_modes_c, nullptr);
vector<VkPresentModeKHR> present_modes(present_modes_c);
vkGetPhysicalDeviceSurfacePresentModesKHR(*dev, *surface,&present_modes_c,present_modes.data());
for (int i = 0; i < present_modes_c; i++) {
if (present_modes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
cout << "Mailbox supported." << endl;
present_mode_selected = VK_PRESENT_MODE_MAILBOX_KHR;
}
}
VkSurfaceCapabilitiesKHR capabilities;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(*dev, *surface, &capabilities);
if (capabilities.maxImageExtent.width < (*extent).width) {
(*extent).width = capabilities.maxImageExtent.width;
}
if (capabilities.maxImageExtent.height < (*extent).height) {
(*extent).height = capabilities.maxImageExtent.height;
}
VkCompositeAlphaFlagBitsKHR composite_alpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
if (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR) {
composite_alpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
}else if (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) {
composite_alpha =VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
}
VkSurfaceTransformFlagBitsKHR transform;
if (capabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
}else {
transform = capabilities.currentTransform;
}
VkSwapchainCreateInfoKHR swapchain_ci = {};
swapchain_ci.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchain_ci.pNext = NULL;
swapchain_ci.surface = *surface;
swapchain_ci.minImageCount = capabilities.minImageCount;
swapchain_ci.imageFormat = format;
swapchain_ci.imageExtent = capabilities.currentExtent;
swapchain_ci.preTransform = transform;
swapchain_ci.compositeAlpha = composite_alpha;
swapchain_ci.imageArrayLayers = 1;
swapchain_ci.presentMode = present_mode_selected;
swapchain_ci.oldSwapchain = VK_NULL_HANDLE;
swapchain_ci.clipped = true;
swapchain_ci.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.queueFamilyIndexCount = 0;
if (VK_SUCCESS != vkCreateSwapchainKHR(*vulk_dev, &swapchain_ci, nullptr, swapchain)) {
fprintf(*Renderer::error_log, "%d: Couldn't create Swapchain", __LINE__);
fclose(*Renderer::error_log);
exit(-1);
}else {
cout << "Swapchain created successfully" << endl;
}
}