在 Android NDK (C) 上使用 Vulkan 渲染 AR 应用
当 ArTextureUpdateMode
设为 AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER
时,ARCore 会在调用 ArSession_update()
时提供 Android 硬件缓冲区。此硬件缓冲区可以绑定到 Vulkan VkImage
。
查看示例应用
hello_ar_vulkan_c 示例应用演示了 Vulkan 渲染支持。
启用硬件缓冲区输出模式
配置的 ArTextureUpdateMode
决定了 ARCore 将如何更新相机纹理。将其设置为 AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER
后,ARCore 将通过 AHardwareBuffer
提供相机图片。
将该会话配置为使用 AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER
:
ArConfig* ar_config = NULL;
ArConfig_create(ar_session, &ar_config);
ArConfig_setTextureUpdateMode(ar_session, ar_config,
AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER);
CHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);
ArConfig_destroy(ar_config);
获取硬件缓冲区
启用 AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER
后,使用 ArFrame_getHardwareBuffer()
获取硬件缓冲区:
void* native_hardware_buffer = NULL;
ArFrame_getHardwareBuffer(ar_session, ar_frame, &native_hardware_buffer);
if ((int64_t)native_hardware_buffer == 0) {
// The hardware buffer isn't ready yet.
return;
}
在 Vulkan 渲染期间使用硬件缓冲区
如需查看有关如何使用 Vulkan 渲染 AR 应用的示例,请参阅 vulkan_handler.cc
。
支持的设备
Vulkan 渲染支持仅适用于 Android API 级别 27 及更高级别。此外,设备还必须支持 VK_ANDROID_external_memory_android_hardware_buffer
扩展。
要求在应用清单中加入 Vulkan
Google Play 会利用应用清单中声明的 <uses-feature>
,从不符合应用硬件和软件功能要求的设备上过滤该应用。使用 Vulkan 1.0 的设备可能不支持所需的扩展,但设备可能
从 Android 10(API 级别 29)开始,与 Vulkan 1.1 兼容的必须具有所需的扩展。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-10-23。
[null,null,["最后更新时间 (UTC):2024-10-23。"],[[["ARCore can provide the camera image as an Android hardware buffer for efficient Vulkan rendering by setting `ArTextureUpdateMode` to `AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER`."],["This hardware buffer can be accessed using `ArFrame_getHardwareBuffer()` and bound to a Vulkan `VkImage` for rendering."],["Vulkan rendering with ARCore is supported on Android API levels 27 and above, requiring devices to support the `VK_ANDROID_external_memory_android_hardware_buffer` extension."],["To ensure compatibility, declare Vulkan requirements in your app's manifest using `\u003cuses-feature\u003e`, targeting devices with Vulkan 1.1 for guaranteed support on Android 10 and above."],["A sample implementation of Vulkan rendering with ARCore can be found in the `hello_ar_vulkan_c` sample app."]]],["To enable hardware buffer access, set `ArTextureUpdateMode` to `AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER` using `ArConfig_setTextureUpdateMode`. After calling `ArSession_update()`, use `ArFrame_getHardwareBuffer()` to obtain the `AHardwareBuffer`. If using the buffer beyond the next `ArSession_update()`, call `AHardwareBuffer_acquire()` and `AHardwareBuffer_release()`. Vulkan rendering is supported on Android API 27+ with the `VK_ANDROID_external_memory_android_hardware_buffer` extension. Incompatible devices will return an error.\n"]]