Matthew Buchanan
Matthew Buchanan

Reputation: 35

difficulty creating a vulkan instance

im starting to rebuild a game engine i began writing for openGL, this time the graphical framework is Vulkan based but i can't create my first Vkinstance because of an access violation. im following the tutorial on vulkan-tutorial.com, the instance section. i declare a

Vkinstance instance;

then later i call

vkCreateInstance(&createInfo, nullptr, &instance)

and my program breaks citing an access violation.

i have tried making instance a pointer and setting it

instance = new Vkinstance();

but this doesn't solve the problem. i still have the access violation. this is upseting because iv seen in tutorials Vkinstance created and not initialised and then sent strait to vkCreateInstance on the next line. so why am i getting this error?

additionaly: instance is a private variable belonging to the class vkCreateInstance is called from.

EDIT:

here is a more complete code segment that you can test

#define VK_USE_PLATFORM_WIN32_KHR
#include <vulkan/vulkan.h>

int main(int argc, char* argv[]) {
    VkInstance instance;

    VkApplicationInfo appInfo = {};
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = "Hello Triangle";
    appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.pEngineName = "No Engine";
    appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.apiVersion = VK_API_VERSION_1_0;

    VkInstanceCreateInfo createInfo = {};
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;
    unsigned int extensionCount = 0;
    const char* extensionNames[] = {
        VK_KHR_SURFACE_EXTENSION_NAME,
        VK_KHR_WIN32_SURFACE_EXTENSION_NAME
    };
    vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
    createInfo.enabledExtensionCount = extensionCount;
    createInfo.ppEnabledExtensionNames = extensionNames;
    createInfo.enabledLayerCount = 0;

    if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
        throw std::runtime_error("failed to create instance!");
    }

it breaks vkCreateInstance(&createInfo, nullptr, &instance) at &instance : Access violation reading location 0xCCCCCCCC

Upvotes: 0

Views: 2730

Answers (1)

Sascha Willems
Sascha Willems

Reputation: 5828

Your list of instance extension names is fixed to two entries:

const char* extensionNames[] = {
    VK_KHR_SURFACE_EXTENSION_NAME,
    VK_KHR_WIN32_SURFACE_EXTENSION_NAME
};

But you read and pass the total number of all available extensions to the create info:

vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);

createInfo.enabledExtensionCount = extensionCount;
createInfo.ppEnabledExtensionNames = extensionNames;

It's very likely that extensionCount is greater than two and that's why instance creation fails (as it expects more than the two extension names).

So either also query and pass all available extension names (if you really want to enable all) as you did with the extension count or set createInfo.enabledExtensionCount to the size of the array of your predefined extension list.

Upvotes: 4

Related Questions