Search code examples
cinstancevulkansurface

Vulkan: vkGetPhysicalDeviceSurfaceCapabilitiesKHR returning VK_ERROR_SURFACE_LOST_KHR in C


I have this function:

static cyBool swapchainSuitable(VkPhysicalDevice device, VkSurfaceKHR surface){

    //Capabilities
    VkSurfaceCapabilitiesKHR capabilities;
    VkResult result =  vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &capabilities);
    if (result != VK_SUCCESS){
        CY_LOG_FATAL("Unable to get the physical device surface capabilities");
        return FALSE;
    }

    //more stuff...
}

The result is always VK_ERROR_SURFACE_LOST_KHR, the Vulkan Spec defines it as

A surface is no longer available.

What does this mean? The Vulkan validation debugger does not pick up anything and the surface was created successfully. I have not destroyed the surface nor the instance and the device is a valid pointer.

This is how the Surface is created (I am not using GLFW but the Win32 API)

VkWin32SurfaceCreateInfoKHR createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
createInfo.pNext = NULL;
createInfo.flags = 0;
createInfo.hinstance = GetModuleHandleA(NULL);
createInfo.hwnd = (HWND) &pWindowHandle;

VkResult result = vkCreateWin32SurfaceKHR(pInstance->handle, &createInfo, NULL, &pSurface->handle);
if(result != VK_SUCCESS){
    CY_LOG_FATAL("Unable to create Windows Surface");
    return FALSE;
}

Again: the instance handle is valid, the HWND is also valid and the surface is created successfully since the debug validation does not show anything.

the Instance extensions I wanted:

  • VK_KHR_SURFACE_EXTENSION_NAME
  • "VK_KHR_win32_surface"
  • VK_EXT_DEBUG_UTILS_EXTENSION_NAME
  • VK_EXT_DEBUG_REPORT_EXTENSION_NAME

The device extensions I wanted:

  • VK_KHR_SWAPCHAIN_EXTENSION_NAME

I feel I am missing something really stupid but I cannot put my finger to it.


Solution

  • Thanks to @Kazz in the comments who found that I was passing my Window handle the wrong way.