You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建Vulkan交换链时程序崩溃且无调试信息求助

Vulkan交换链创建崩溃排查

我正在按照Vulkan教程学习,已经成功创建Instance和验证层,但调用createSwapChain函数时程序崩溃。以下是我的函数代码、开发环境及设备信息:

交换链创建代码

void createSwapChain() {

    VkSurfaceFormatKHR surfaceFormat;
    surfaceFormat.format=VK_FORMAT_B8G8R8A8_SRGB;
    surfaceFormat.colorSpace=VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
    VkPresentModeKHR presentMode=VK_PRESENT_MODE_FIFO_KHR;
    int width,height;
    glfwGetFramebufferSize(window, &width, &height);
    VkExtent2D extent ={width,height};
    uint32_t imageCount=2;//minimum cap +1
    VkSurfaceCapabilitiesKHR capabilities;
    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
    VkSwapchainCreateInfoKHR createInfo={0};
    createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    createInfo.surface = surface;
    createInfo.minImageCount = imageCount;
    createInfo.imageFormat = surfaceFormat.format;
    createInfo.imageColorSpace = surfaceFormat.colorSpace;
    createInfo.imageExtent = extent;
    createInfo.imageArrayLayers = 1;
    createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;

    createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
//  createInfo.queueFamilyIndexCount = 1;
//  createInfo.pQueueFamilyIndices = NULL;

    createInfo.preTransform = capabilities.currentTransform/*VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR*/;//swapChainSupport.capabilities.currentTransform
    createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;

    createInfo.presentMode = presentMode;
    createInfo.clipped = VK_TRUE;// means obscured images have unimportant color!!
    createInfo.oldSwapchain = VK_NULL_HANDLE;

     if (vkCreateSwapchainKHR(device, &createInfo, NULL, &swapChain) != VK_SUCCESS)
     printf("failed to create swap chain!");

}

开发环境与设备信息

开发环境:Windows 10 64位,Eclipse + MinGW-64,链接库:-lglfw3、-lvulkan-1

Available Layers:
         VK_LAYER_AMD_switchable_graphics
         VK_LAYER_VALVE_steam_overlay
         VK_LAYER_VALVE_steam_fossilize
         VK_LAYER_KHRONOS_validation
Available extensions:
         VK_KHR_device_group_creation
         VK_KHR_external_fence_capabilities
         VK_KHR_external_memory_capabilities
         VK_KHR_external_semaphore_capabilities
         VK_KHR_get_physical_device_properties2
         VK_KHR_get_surface_capabilities2
         VK_KHR_surface
         VK_KHR_win32_surface
         VK_EXT_debug_report
         VK_EXT_debug_utils
         VK_EXT_swapchain_colorspace
Instance extension:
         VK_KHR_surface
         VK_KHR_win32_surface
         VK_EXT_debug_utils
Availible devices:
AMD Radeon(TM) Graphics          Type: 1         GeometryShader: Yes
         FamilyIndex: 0 AvailibleQueueCount: 1   Surface Support: Yes    Graphics | Compute | Transfer | Binding
         FamilyIndex: 1 AvailibleQueueCount: 2   Surface Support: Yes     | Compute | Transfer | Binding
         FamilyIndex: 2 AvailibleQueueCount: 1   Surface Support: Yes     |  | Transfer | Binding
   Availible device extensions:  (The required is VK_KHR_swapchain)
         VK_KHR_16bit_storage    VK_KHR_8bit_storage     VK_KHR_bind_memory2
         VK_KHR_buffer_device_address    VK_KHR_copy_commands2   VK_KHR_create_renderpass2
         VK_KHR_dedicated_allocation     VK_KHR_depth_stencil_resolve    VK_KHR_descriptor_update_template
         VK_KHR_device_group     VK_KHR_draw_indirect_count      VK_KHR_driver_properties
         VK_KHR_dynamic_rendering        VK_KHR_external_fence   VK_KHR_external_fence_win32
         VK_KHR_external_memory  VK_KHR_external_memory_win32    VK_KHR_external_semaphore
         VK_KHR_external_semaphore_win32         VK_KHR_format_feature_flags2    VK_KHR_get_memory_requirements2
         VK_KHR_global_priority  VK_KHR_imageless_framebuffer    VK_KHR_image_format_list
         VK_KHR_maintenance1     VK_KHR_maintenance2     VK_KHR_maintenance3
         VK_KHR_maintenance4     VK_KHR_multiview        VK_KHR_pipeline_executable_properties
         VK_KHR_pipeline_library         VK_KHR_push_descriptor  VK_KHR_relaxed_block_layout
         VK_KHR_sampler_mirror_clamp_to_edge     VK_KHR_sampler_ycbcr_conversion         VK_KHR_separate_depth_stencil_layouts
         VK_KHR_shader_atomic_int64      VK_KHR_shader_clock     VK_KHR_shader_draw_parameters
         VK_KHR_shader_float16_int8      VK_KHR_shader_float_controls    VK_KHR_shader_integer_dot_product
         VK_KHR_shader_non_semantic_info         VK_KHR_shader_subgroup_extended_types   VK_KHR_shader_subgroup_uniform_control_flow
         VK_KHR_shader_terminate_invocation      VK_KHR_spirv_1_4        VK_KHR_storage_buffer_storage_class

  Swap chain details:
         Min/max images in swap chain; 1, 16
         Min/max width and height 800 : 600 | 800 : 600
         Pixel format codes(at least we need one no matter what!):       44
         50      58      97      44      50      58      97      58      97      58
         97      97      2       3       4       5       8       37      38      43
         45      51      52      57      64      91      92      122
         color space codes(at least we need one no matter what!):        0
         0       0       0       1000104006      1000104006      1000104006      1000104006      1000104008      1000104008      1000104007
         1000104007      1000104002      0       0       0       0       0       0       0       0
         0       0       0       0       0       0       0       0
  Available presentation modes:
         0 (Immediate)
         2       3 ( FIFO, FIFO_relaxed)

崩溃原因排查方向

  • 验证层未正确生效:当前仅用printf输出简单错误,无法获取详细崩溃原因。确保创建Instance时启用VK_LAYER_KHRONOS_validation,并通过VK_EXT_debug_utils设置调试回调,捕获具体的Vulkan错误信息,这是排查崩溃的关键。
  • 表面格式与颜色空间不匹配设备支持:硬编码的VK_FORMAT_B8G8R8A8_SRGB+VK_COLOR_SPACE_SRGB_NONLINEAR_KHR组合需要确认在设备返回的VkSurfaceFormatKHR列表中存在。遍历vkGetPhysicalDeviceSurfaceFormatsKHR的结果,选择设备支持的格式,不要直接硬编码。
  • 交换链图像数量设置不当:虽然当前设置的2在设备1-16的范围内,但如果capabilities.minImageCount等于capabilities.maxImageCount,必须使用该固定值,不能随意加1。
  • 逻辑设备未启用VK_KHR_swapchain扩展:创建VkDevice时必须将VK_KHR_swapchain加入启用扩展列表,否则调用vkCreateSwapchainKHR会因缺少扩展而崩溃。
  • 全局变量未正确初始化:检查physicalDevice、surface、device、swapChain这些外部变量是否为有效Vulkan句柄,若device无效,调用创建函数会直接崩溃。
  • Surface Extent合法性:确认glfwGetFramebufferSize获取的宽高在capabilities.minImageExtent和capabilities.maxImageExtent范围内。若capabilities.currentExtent不是VK_WHOLE_SIZE,应直接使用该值而非窗口尺寸。

内容的提问来源于stack exchange,提问作者mohammadsdtmnd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 10:40:17