Search code examples
c++openxr

OpenXR hello world program initialization "Failed to find layer XR_APILAYER_LUNARG_core_validation"


I am following a tutorial to write a simple OpenXR application and I've run in to an issue with enabling the XR_APILAYER_LUNARG_core_validation layer. The OpenXR Loader is unable to find the layer despite my having installed the SDK from the nuget repository in Visual Studio.

I have a very simple hello world program that runs when I do not try to load the core validation layer.

#include <openxr/openxr.h>
#include <iostream>

XrInstance createInstance() {
    XrInstance instance;

    static const char* const applicationName = "Void's OpenXR Test";
    static const uint32_t majorVersion = uint32_t(1);
    static const uint32_t minorVersion = uint32_t(1);
    static const uint32_t patchVersion = uint32_t(1);

    // The layer I am trying to load
    static const char* const layerNames[] = {
        "XR_APILAYER_LUNARG_core_validation" 
    };
    static const char* const extensionNames[] = {
        "XR_EXT_debug_utils"
    };

    XrInstanceCreateInfo instanceCreateInfo{};
    instanceCreateInfo.type = XR_TYPE_INSTANCE_CREATE_INFO;
    instanceCreateInfo.createFlags = 0;
    strcpy_s(instanceCreateInfo.applicationInfo.applicationName, applicationName);
    instanceCreateInfo.applicationInfo.applicationVersion = 0;
    strcpy_s(instanceCreateInfo.applicationInfo.engineName, applicationName);
    instanceCreateInfo.applicationInfo.engineVersion = 0;
    instanceCreateInfo.applicationInfo.apiVersion = XR_CURRENT_API_VERSION;
    instanceCreateInfo.enabledApiLayerCount = 1;
    instanceCreateInfo.enabledApiLayerNames = layerNames;
    instanceCreateInfo.enabledExtensionCount = 1;
    instanceCreateInfo.enabledExtensionNames = extensionNames;

    XrResult result = xrCreateInstance(&instanceCreateInfo, &instance);

    if (result != XR_SUCCESS) {
        std::cerr << "Failed to create OpenXR instance: " << result << std::endl;
        return XR_NULL_HANDLE;
    }
    return instance;
}

void destroyInstance(XrInstance instance) {
    xrDestroyInstance(instance);
}

int main() {
    XrInstance instance = createInstance();

    destroyInstance(instance);
    return 0;
}

With this code, I am running into the following error:

Error [GENERAL | xrCreateInstance | OpenXR-Loader] : ApiLayerInterface::LoadApiLayers - failed to find layer XR_APILAYER_LUNARG_core_validation
Error [GENERAL | xrCreateInstance | OpenXR-Loader] : Failed loading layer information
Error [GENERAL | xrCreateInstance | OpenXR-Loader] : xrCreateInstance failed
Failed to create OpenXR instance: -36

This indicates to me that there may be a problem with my environment, or something that I'm missing to allow OpenXR to be able to find this api layer.

I know that my simple hello world program runs without issue when I don't try loading this API layer.

I thought maybe the packages from the nuget repository in visual studio were not actually the SDK, but looking through them, while it is an older version (1.0.10.2) than what I could get through the official git repositories, this is indeed the SDK and so should include the API layers that I want to use. Observe that the packages listed in Visual Studio point directly to the official KhronosGroup OpenXR-SDK

To rule out the versioning being the problem, I went ahead and updated my OpenXR SDK to 1.0.33 Observe that this version is now the latest that can be found in the official KhronosGroup repo

Despite updating the SDK, I am still getting the same error as described above.


Solution

  • So often problems that arise in development come from a false assumption. The false assumption I made was that the packages distributed through the OpenXR-SDK repo do not actually include builds for the API Layers.

    It turns out that I missed some important information on the repo from which I got the package. In the OpenXR-SDK-Source repo they explain that the packages in the OpenXR-SDK repo do not include the the API Layer DLLs

    To address this, I cloned instead the OpenXR-SDK-Source repo, then did a build following their instructions in the BUILDING.md file. Once the build was done, I located the src/api_layers folder and copied the absolute path. Then I configured an environment variable in my hello_world project under Project->Properties->Debugging Add the path to the built API Layers to the environment variables

    Having done this, my hello_world project now runs and is able to find the offending API Layer needed for me to proceed.