vkGetPhysicalDeviceSurfaceCapabilitiesKHR什么都不返回

时间:2017-09-20 08:53:56

标签: c++ graphics vulkan

我创建了physical devicesurface。它工作正常,但是当我打电话时:

vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface,
                                          &surface_capabilities)

虽然函数返回0,但surface_capabilities结构仍未初始化,每个成员都是null

同样的问题发生在

vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface,
                                     &formats_count, nullptr)

功能和

vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface,
                                          &present_modes_count,
                                          &present_modes[0])
调用函数后,formats_countpresent_modes_count也是null

这是我的代码:

   #define GLFW_INCLUDE_VULKAN
    #define VK_USE_PLATFORM_WIN32_KHR
    #include <iostream>
    #include <stdexcept>
    #include <vector>
    #include <cstring>
    #include "Window.h"
    #include <vulkan\vulkan.h>

    using namespace std;

    int main()
    {

        Window* myWindow = new Window();

        uint32_t instance_extensions_count = 0;
        vkEnumerateInstanceExtensionProperties(nullptr, &instance_extensions_count, nullptr);
        vector<VkExtensionProperties> availible_instance_extensions(instance_extensions_count);
        vkEnumerateInstanceExtensionProperties(nullptr, &instance_extensions_count, availible_instance_extensions.data());

         std::vector<const char*> extensions= {
            "VK_KHR_win32_surface",
        }; 
         /*------------------------------------------------------------------*/
        VkApplicationInfo application_info = {
            VK_STRUCTURE_TYPE_APPLICATION_INFO,
            nullptr,
            "Test",
             VK_MAKE_VERSION(1, 0, 0),
            "Test",
            VK_MAKE_VERSION(1, 0, 0),
            VK_MAKE_VERSION(1, 0, 0),

        };

        VkInstanceCreateInfo instance_create_info =
        {
            VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
            nullptr,
                0,
                &application_info,
                0,
                nullptr,
                static_cast<uint32_t>(extensions.size()),
                extensions.data(),
        };

        VkInstance pInstance;
        vkCreateInstance(&instance_create_info, nullptr, &pInstance);

        /*------------------------------------------------------------------*/

        VkSurfaceKHR surface;
        glfwCreateWindowSurface(pInstance, myWindow->window, nullptr, &surface);
        VkWin32SurfaceCreateInfoKHR surface_create_info;
        surface_create_info.flags = 0;
        surface_create_info.hinstance = myWindow->instance;
        surface_create_info.hwnd = myWindow->handle;
        surface_create_info.pNext = nullptr;
        surface_create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;

        vkCreateWin32SurfaceKHR(pInstance, &surface_create_info, nullptr, &surface);
        /*------------------------------------------------------------------*/

        uint32_t num_devices = 0;
        vkEnumeratePhysicalDevices(pInstance, &num_devices, nullptr);
        vector<VkPhysicalDevice> physicalDevices(num_devices);
        vkEnumeratePhysicalDevices(pInstance, &num_devices, physicalDevices.data());

        uint32_t device_extensions_count = 0;
        vector<const char*> device_extensions = {
            VK_KHR_SWAPCHAIN_EXTENSION_NAME
        };
        VkPhysicalDevice physicalDevice = nullptr;

        for (auto device : physicalDevices)
        {
            vkEnumerateDeviceExtensionProperties(device, nullptr, &device_extensions_count, nullptr);
            vector<VkExtensionProperties> available_extensions(device_extensions_count);
            vkEnumerateDeviceExtensionProperties(device, nullptr, &device_extensions_count, available_extensions.data());
            //CheckExtensionAvailability(device_extensions[i], available_extensions)    
            physicalDevice = device;
        } 


        VkPhysicalDeviceProperties device_properties;
        vkGetPhysicalDeviceProperties(physicalDevice, &device_properties);
        VkPhysicalDeviceFeatures device_features;
        vkGetPhysicalDeviceFeatures(physicalDevice, &device_features);

        /*------------------------------------------------------------------*/

        uint32_t queue_families_count = 0;
        vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queue_families_count, nullptr);
        vector<VkQueueFamilyProperties> queue_family_properties(queue_families_count);
        vector<VkBool32>                queue_present_support(queue_families_count);
        vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queue_families_count, queue_family_properties.data());
        uint32_t graphics_queue_family_index = UINT32_MAX;
        uint32_t present_queue_family_index = UINT32_MAX;
        for (uint32_t i = 0; i < queue_families_count; ++i)
        {
        vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, queue_present_support.data());
            if ((queue_family_properties[i].queueCount > 0) && (queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT))
                graphics_queue_family_index = i;
        }

        for (uint32_t i = 0; i < queue_families_count; ++i) {
            if (queue_present_support[i]) {
                present_queue_family_index = i;
                break;
            }
        }

        vector<float> queue_priorities = { 1.0f };
        vector<VkDeviceQueueCreateInfo> queue_create_infos;
        queue_create_infos.push_back({ 
            VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
            nullptr,
            0,
            graphics_queue_family_index,
            static_cast<uint32_t>(queue_priorities.size()),
            queue_priorities.data() 
        });

        if (graphics_queue_family_index != present_queue_family_index)
        {
            queue_create_infos.push_back({
                VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
                nullptr,
                0,
                present_queue_family_index,
                static_cast<uint32_t>(queue_priorities.size()),
                queue_priorities.data()
            });
        };
        /*------------------------------------------------------------------*/
        VkDeviceCreateInfo device_create_info = {
            VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
            nullptr,
            0,
            static_cast<uint32_t>(queue_create_infos.size()),
            &queue_create_infos[0],
            0,
            nullptr,
            static_cast<uint32_t>(device_extensions.size()),
            &device_extensions[0],
            nullptr
        };

        VkDevice device;
        vkCreateDevice(physicalDevice, &device_create_info, nullptr, &device);
        /*------------------------------------------------------------------*/
        VkSemaphore                   imageAvailableSemaphore;
        VkSemaphore                   renderingFinishedSemaphore;

        VkSemaphoreCreateInfo semaphore_create_info = {
            VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
            nullptr,
            0
        };

        vkCreateSemaphore(device, &semaphore_create_info, nullptr, &imageAvailableSemaphore);
        vkCreateSemaphore(device, &semaphore_create_info, nullptr, &renderingFinishedSemaphore);

        /*---------------------------wrong code--------------------------------*/
        VkSurfaceCapabilitiesKHR surface_capabilities;
        vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surface_capabilities);
        surface_capabilities.maxImageCount;//the value is 3435973836


        uint32_t formats_count;
        vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formats_count, nullptr);
        vector<VkSurfaceFormatKHR> surface_formats(formats_count);
//formats_count is 3435973836
        vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formats_count, surface_formats.data());

        uint32_t present_modes_count;
        vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &present_modes_count, nullptr);
//present_modes_count is 3435973836
        std::vector<VkPresentModeKHR> present_modes(present_modes_count);
        vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &present_modes_count, &present_modes[0]);







        myWindow->GameLoop();

        return 0;
    }

1 个答案:

答案 0 :(得分:1)

您的分机列表中缺少"VK_KHR_surface"个分机号。 将其添加到您的extensions列表中。或者更好地使用glfwGetRequiredInstanceExtensions()来获取所需的确切扩展名。

您还需要执行错误检查。例如。检查VkResult返回的glfwCreateWindowSurface()是否有错误。始终始终检查从Vulkan命令返回的VkResult。对于GLFW,使用glfwSetErrorCallback()设置错误回调,并检查返回的错误代码。顺便说一下,我没有看到glfwInit();是Window.h吗?

此外,您还有重复的曲面创建。您拨打了glfwCreateWindowSurface(),然后又拨打了vkCreateWin32SurfaceKHR()