-1

我正在阅读 Vulkan 教程并坚持创建一个 我在 MacBook Pro 15 (2019) 上使用 macOS 10.15.6 (19G2021) 的实例,所有环境变量如 VK_ICD_FILENAMES 和 VK_LAYER_PATH 均按照教程正确设置。

glfwVulkanSupported() 返回 True glfwGetRequiredInstanceExtensions(&glfwExtensionCount) 返回 NULL

这是我的 main.cpp:

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include <iostream>
#include <stdexcept>
#include <cstdlib>

#include <stdio.h>

const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;

class HelloTriangleApplication {
public:
    void run() {
        initWindow();
        initVulkan();
        mainLoop();
        cleanup();
    }

private:
    void initWindow() {
        glfwInit();
        
        glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
        window_ = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
    }
    
    // initiate vulkan objects
    void initVulkan() {
        if (glfwVulkanSupported()) {
            std::cout << "Vulkan Supported" << std::endl;
            createInstance();
        }
    }

    void createInstance() {
        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;

        uint32_t glfwExtensionCount = 0;
        const char** glfwExtensions;
        
        glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
        printf("glfwExtensions %d\n", glfwExtensionCount);
        
        createInfo.enabledExtensionCount = glfwExtensionCount;
        createInfo.ppEnabledExtensionNames = glfwExtensions;

        createInfo.enabledLayerCount = 0;
        
        VkResult result = vkCreateInstance(&createInfo, nullptr, &instance_);
        if (result != VK_SUCCESS) {
            printf("failed to create instance with error_code %d", result);
            throw std::runtime_error("failed to create instance!");
        } else {
            std::cout << "Instance for application " << appInfo.pApplicationName << " created successfully";
        }
    }
    
    void mainLoop() {
        while (!glfwWindowShouldClose(window_)) {
            glfwPollEvents();
        }
    }

    void cleanup() {
        glfwDestroyWindow(window_);
        
        glfwTerminate();
    }
    
    GLFWwindow* window_;
    VkInstance instance_;
};

int main() {
    HelloTriangleApplication app;

    try {
        app.run();
    } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
4

2 回答 2

1

我自己也遇到了这个问题,我发现删除任何相关文件路径~/{stuff}并用实际路径替换它们/user/{user}/{stuff}解决了我的问题,这在我头疼了半天并考虑在 linux 上完全重新启动之后。这发生在我的 xcode 架构下的环境变量中

于 2021-05-19T02:19:09.163 回答
-1

我之前运行过 ./install_vulkan.py,所以环境变量已经设置好了。我的猜测是,试图在 XCode 中将它们覆盖到本地 sdk 路径会混淆依赖项。所以我刚刚走下由脚本设置环境变量并将库复制到 /usr/local/lib 的路径(使用系统路径的替代方法)https://vulkan.lunarg.com/doc/sdk/ 1.2.148.1/mac/getting_started.html

于 2020-09-29T08:13:24.477 回答