Vulkan - instance creation fails when loading extensions

598 Views Asked by At

I run into some problems creating a basic Vulkan application: When trying to create an instance it fails, as soon as I tried to load any extensions via ppEnableExtensionNames. If ppEnableExtensionNames is NULL, the instance creation succeeds (quite pointles though, since I cant create a surface that way).

I double checked with different tutorials and couldnt find any mistakes. I installed the SDK and checked for the vulkan-1.dll, as well as running the cube demo successfully. Furthermore I enumerated through available extensions and all extensions I tried to load were listed, so my device should basically support them.

Heres my Code, breaks at vkCreateInstance as long as ppEnableExtensionNames != NULL. EDIT: Even though I solved this problem meanwhile, I changed the code to what caused the actual problems, in case anyone makes the same stupid mistake.

    #define VK_USE_PLATFORM_WIN32_KHR


    //STD
    #include <Windows.h>
    #include <iostream>
    #include <string>
    #include <vector>

    //NON-STD Libraries
    #include "vulkan\vulkan.h"



    //==================
    //Window Reference
    //==================
    WNDCLASSEX window;
    MSG msg;
    HWND hwnd;         
    HDC hdc;            



    //===================
    //SETUP VULKAN
    //===================

    //Global VK Variables
    VkInstance instance;
    VkPhysicalDevice physDevice;        
    VkApplicationInfo appInfo = {};
    VkInstanceCreateInfo instanceInfo = {};


    void loadExtensions{

std::vector<const char *> enabledExtensions;
        enabledExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
        enabledExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);

        instanceInfo.enabledExtensionCount = enabledExtensions.size();
        instanceInfo.ppEnabledExtensionNames = &enabledExtensions[0];

}


    //==================
    //WinMain, Entry Point
    //==================
    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd) {


        appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
        appInfo.pNext = NULL;
        appInfo.pApplicationName = "Monody";
        appInfo.applicationVersion = VK_MAKE_VERSION(0, 0, 0);
        appInfo.pEngineName = "Monody Engine";
        appInfo.engineVersion = VK_MAKE_VERSION(0, 0, 0);
        appInfo.apiVersion = VK_API_VERSION_1_0;

        instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
        instanceInfo.pNext = NULL;
        instanceInfo.flags = 0;
        instanceInfo.pApplicationInfo = &appInfo;
        instanceInfo.enabledLayerCount = 0;
        instanceInfo.ppEnabledLayerNames = NULL;
        instanceInfo.enabledExtensionCount = 0;
        instanceInfo.ppEnabledExtensionNames = NULL;


    loadExtensions();


        //Create Instance
        VkResult error = vkCreateInstance(&instanceInfo, nullptr, &instance);




        return 0;

    }
1

There are 1 best solutions below

0
On

Since the vector is only within the loadExtensions() function within scope, and it is only given as a pointer to the instanceInfo struct, the pointer becomes invalid, when - back in the WinMain function - the vector is out of scope. So just make the vector global or store it somewhere else, until the Instance is created.