vkCreateSwapchainKHR 错误

vkCreateSwapchainKHR Error

即使验证层和调试回调扩展已启用并正常工作(它们响应错误的结构等),我仍然从 vkCreateSwapchainKHR() 得到 "VK_ERROR_VALIDATION_FAILED_EXT" 结果,并且没有验证图层错误以查明错误... 交换链创建(使用 GTX 970):

VkBool32 isSupported = false;
vkGetPhysicalDeviceSurfaceSupportKHR( physicalDevices[0], 0, surface, &isSupported);

if (!isSupported) {
    std::cout << "*ERROR* This device doesn't support surfaces" << std::endl;
}

VkSurfaceCapabilitiesKHR surfCaps;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevices[0], surface, &surfCaps);

std::vector<VkSurfaceFormatKHR> deviceFormats;
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevices[0], surface, &formatCount, nullptr);
deviceFormats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevices[0], surface, &formatCount, deviceFormats.data());


swapChainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapChainInfo.pNext = nullptr;
swapChainInfo.flags = 0;
swapChainInfo.surface = surface;

swapChainInfo.minImageCount = surfCaps.minImageCount; 
swapChainInfo.imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
swapChainInfo.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
swapChainInfo.imageExtent = surfCaps.currentExtent;
swapChainInfo.imageArrayLayers = 1;
swapChainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapChainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;

swapChainInfo.queueFamilyIndexCount = 0;
swapChainInfo.pQueueFamilyIndices = VK_NULL_HANDLE;
swapChainInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; 
swapChainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapChainInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
swapChainInfo.clipped = VK_TRUE; // TODO : TEST clipping against another window
swapChainInfo.oldSwapchain = VK_NULL_HANDLE;

result = vkCreateSwapchainKHR( device, &swapChainInfo, nullptr, &swapChain );
if (result) {
    std::cout << "*ERROR* Swapchain Creation Failed :" << result << std::endl;
}

使用 GLFW 创建表面(return 没有任何错误):

if (result = glfwCreateWindowSurface(instance, window, nullptr, &surface)) 
    {
        std::cout << "*ERROR* Surface Creation Failed : " << result << std::endl;
    }

vkCreateSwapchainKHR 验证失败的原因有很多(查看 core_validation.cpp 中的 PreCallValidateCreateSwapchainKHR)。

这里可能没有足够的代码来说明失败的原因,例如,失败可能是因为表面无效。但是,为了查明问题,它应该在调试日志中给出一条失败消息,它会告诉你确切的原因。在尝试创建交换链之前,您应该通过调用 CreateDebugReportCallbackEXT 来启用它。这还需要您启用 VK_EXT_debug_report 扩展。有关详细信息,请参阅 here

关于您的代码的几点说明(可能是问题的原因):

  • 您没有检查所有 vkGet* 个命令中的 VkResult
  • 您没有根据 deviceFormats
  • 中支持的格式检查 swapChainInfo.imageFormat
  • 您没有考虑 surfCaps.currentExtent 可以 0xFFFFFFFF
  • 的情况
  • swapChainInfo.pQueueFamilyIndices 是指针不是句柄;使用 nullptr
  • 您没有检查 swapChainInfo.preTransformsurfCaps.supportedTransforms
  • 您没有检查 swapChainInfo.compositeAlphasurfCaps.supportedCompositeAlpha