我正在阅读 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;
}