我在 Rust 应用程序中创建 Vulkan 实例时遇到问题。
Vulkan SDK 安装在 /Users/verhage/VulkanSDK 中。
我按照 Ash 文档导出了所需的环境变量:
export VULKAN_SDK=$HOME/VulkanSDK/1.3.275.0/macOS
export DYLD_FALLBACK_LIBRARY_PATH=$VULKAN_SDK/lib
export VK_LAYER_PATH=$VULKAN_SDK/share/vulkan/explicit_layer.d
export VK_LAYER_PATH=$VULKAN_SDK/share/vulkan/explicit_layer.d
但是当我做
cargo run
时,我得到:
Error: Unable to find a Vulkan driver
与我未设置任何环境变量时的错误相同。我还尝试将环境变量添加到 IntelliJ 中的运行配置中,如下所示:
但是当我运行它时,我收到一条更奇怪的错误消息:
dyld[26946]: Library not loaded: @rpath/libvulkan.1.dylib
Referenced from: <D96150F6-E457-355F-B797-E3268BB2412B> /Users/verhage/dev/myapp/target/debug/piston
Reason: tried: '/Users/verhage/dev/myapp/target/debug/deps/libvulkan.1.dylib' (no such file), '/Users/verhage/dev/myapp/target/debug/libvulkan.1.dylib' (no such file), '/Users/verhage/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libvulkan.1.dylib' (no such file), '/Users/verhage/.rustup/toolchains/stable-aarch64-apple-darwin/lib/libvulkan.1.dylib' (no such file), '$VULKAN_SDK/lib/libvulkan.1.dylib' (no such file)
我不明白出了什么问题,我仔细检查了所有文件路径,它们都在那里。而且
$VULKAN_SDK/lib/libvulkan.1.dylib
正是预期的位置。
这是我尝试运行的代码:
use anyhow::Result;
use ash::{self, vk};
fn main() -> Result<()> {
let entry = unsafe { ash::Entry::load() }?;
let application_info = vk::ApplicationInfo::builder().api_version(vk::API_VERSION_1_3);
let create_info = vk::InstanceCreateInfo::builder().application_info(&application_info);
let instance = unsafe { entry.create_instance(&create_info, None) }?;
Ok(())
}
我做错了什么?
我明白这是怎么回事了。在 macOS 上,您需要加载一些默认情况下不会加载的特定扩展,因为 macOS 没有对 Vulkan 的本机支持。
事实证明环境变量根本不重要。
这一切都是为了加载正确的扩展。这是添加了扩展的代码:
use anyhow::Result;
use ash::{self, vk};
fn main() -> Result<()> {
let mut extension_names = Vec::new();
extension_names.push(vk::KhrPortabilityEnumerationFn::name().as_ptr());
extension_names.push(vk::KhrGetPhysicalDeviceProperties2Fn::name().as_ptr());
let entry = unsafe { ash::Entry::load() }?;
let application_info = vk::ApplicationInfo::builder().api_version(vk::API_VERSION_1_3);
let create_flags = vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR;
let create_info = vk::InstanceCreateInfo::builder()
.application_info(&application_info)
.enabled_extension_names(&extension_names)
.flags(create_flags);
let instance = unsafe { entry.create_instance(&create_info, None) }?;
Ok(())
}
现在它可以找到驱动程序并且运行没有问题。