在 Android SDK (Kotlin/Java) 上使用 Vulkan 渲染 AR 应用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
当 Config.TextureUpdateMode
设置为 TextureUpdateMode.EXPOSE_HARDWARE_BUFFER
时,ARCore 会在调用 Session.update()
时提供 Android 硬件缓冲区。此硬件缓冲区可绑定到 Vulkan VkImage
。
查看示例应用
hello_ar_vulkan_c 示例应用演示了 Vulkan 渲染支持。
启用硬件缓冲区输出模式
配置的 Config.TextureUpdateMode
决定了 ARCore 将如何更新相机纹理。将其设置为 TextureUpdateMode.EXPOSE_HARDWARE_BUFFER
后,ARCore 将通过 HardwareBuffer
提供相机图片。
将会话配置为使用 TextureUpdateMode.EXPOSE_HARDWARE_BUFFER
:
Java
Config config = session.getConfig();
config.setTextureUpdateMode(Config.TextureUpdateMode.EXPOSE_HARDWARE_BUFFER);
session.configure(config);
Kotlin
session.configure(
session.config.apply { textureUpdateMode = Config.TextureUpdateMode.EXPOSE_HARDWARE_BUFFER }
)
获取硬件缓冲区
启用 TextureUpdateMode.EXPOSE_HARDWARE_BUFFER
后,请使用 Frame.getHardwareBuffer()
获取硬件缓冲区:
Java
try {
HardwareBuffer buffer = frame.getHardwareBuffer();
// Use the buffer object in your rendering.
} catch (NotYetAvailableException e) {
// The hardware buffer is not ready yet.
}
Kotlin
try {
val buffer = frame.hardwareBuffer
// Use the buffer object in your rendering.
} catch (e: NotYetAvailableException) {
// The hardware buffer is not ready yet.
}
在 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):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eARCore can provide the camera image as an Android hardware buffer for efficient Vulkan rendering when \u003ccode\u003eTextureUpdateMode.EXPOSE_HARDWARE_BUFFER\u003c/code\u003e is enabled.\u003c/p\u003e\n"],["\u003cp\u003eThis hardware buffer can be accessed using \u003ccode\u003eFrame.getHardwareBuffer()\u003c/code\u003e and bound to a Vulkan \u003ccode\u003eVkImage\u003c/code\u003e for rendering.\u003c/p\u003e\n"],["\u003cp\u003eVulkan rendering with ARCore requires Android API level 27 or higher and device support for the \u003ccode\u003eVK_ANDROID_external_memory_android_hardware_buffer\u003c/code\u003e extension.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ehello_ar_vulkan_c\u003c/code\u003e sample app demonstrates how to implement Vulkan rendering with ARCore.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure your app is only available on compatible devices, declare the Vulkan feature requirement in your app's manifest.\u003c/p\u003e\n"]]],["To utilize hardware buffer output, set `Config.TextureUpdateMode` to `EXPOSE_HARDWARE_BUFFER` during session configuration. This enables ARCore to provide a hardware buffer via `Session.update()`. Retrieve this buffer using `Frame.getHardwareBuffer()`. It can then be bound to a Vulkan `VkImage`. Vulkan rendering is demonstrated in the `hello_ar_vulkan_c` sample app. Support requires Android API level 27+, with the `VK_ANDROID_external_memory_android_hardware_buffer` extension and ensure your manifest declares Vulkan usage.\n"],null,["# Render your AR app using Vulkan on Android SDK (Kotlin/Java)\n\nWhen the [`Config.TextureUpdateMode`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode) is set to [`TextureUpdateMode.EXPOSE_HARDWARE_BUFFER`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode#expose_hardware_buffer), ARCore will provide an Android [hardware buffer](https://developer.android.com/ndk/reference/group/a-hardware-buffer) when [`Session.update()`](/ar/reference/java/com/google/ar/core/Session#update-) is called. This hardware buffer can be bound to a Vulkan [`VkImage`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkImage.html).\n\nView the sample application\n---------------------------\n\nVulkan rendering support is demonstrated in the [hello_ar_vulkan_c sample app](https://github.com/google-ar/arcore-android-sdk/tree/master/samples/hello_ar_vulkan_c).\n\nEnable the hardware buffer output mode\n--------------------------------------\n\nThe configured [`Config.TextureUpdateMode`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode) determines how ARCore will update the camera texture. When it is set to [`TextureUpdateMode.EXPOSE_HARDWARE_BUFFER`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode#expose_hardware_buffer), ARCore will provide the camera image through a [`HardwareBuffer`](https://developer.android.com/reference/android/hardware/HardwareBuffer).\n\nConfigure the session to use [`TextureUpdateMode.EXPOSE_HARDWARE_BUFFER`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode#expose_hardware_buffer): \n\n### Java\n\n```java\nConfig config = session.getConfig();\nconfig.setTextureUpdateMode(Config.TextureUpdateMode.EXPOSE_HARDWARE_BUFFER);\nsession.configure(config);\n```\n\n### Kotlin\n\n```kotlin\nsession.configure(\n session.config.apply { textureUpdateMode = Config.TextureUpdateMode.EXPOSE_HARDWARE_BUFFER }\n)\n```\n| **Note:** When using [`TextureUpdateMode.EXPOSE_HARDWARE_BUFFER`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode#expose_hardware_buffer) on incompatible devices, [`Session.configure()`](/ar/reference/java/com/google/ar/core/Session#configure(com.google.ar.core.Config)) will fail and throw [`UnsupportedConfigurationException`](/ar/reference/java/com/google/ar/core/exceptions/UnsupportedConfigurationException).\n\nObtain the hardware buffer\n--------------------------\n\nWhen [`TextureUpdateMode.EXPOSE_HARDWARE_BUFFER`](/ar/reference/java/com/google/ar/core/Config.TextureUpdateMode#expose_hardware_buffer) is enabled, use [`Frame.getHardwareBuffer()`](/ar/reference/java/com/google/ar/core/Frame#getHardwareBuffer-) to get the hardware buffer: \n\n### Java\n\n```java\ntry {\n HardwareBuffer buffer = frame.getHardwareBuffer();\n // Use the buffer object in your rendering.\n} catch (NotYetAvailableException e) {\n // The hardware buffer is not ready yet.\n}\n```\n\n### Kotlin\n\n```kotlin\ntry {\n val buffer = frame.hardwareBuffer\n // Use the buffer object in your rendering.\n} catch (e: NotYetAvailableException) {\n // The hardware buffer is not ready yet.\n}\n```\n\nUse the hardware buffer during Vulkan rendering\n-----------------------------------------------\n\nSee [`vulkan_handler.cc`](https://github.com/google-ar/arcore-android-sdk/blob/master/samples/hello_ar_vulkan_c/app/src/main/cpp/vulkan_handler.cc) for an example of how to render an AR application using Vulkan.\n\nSupported devices\n-----------------\n\nVulkan rendering support is only available on Android API levels **27** and\nabove. Additionally, the device must support the `VK_ANDROID_external_memory_android_hardware_buffer` extension.\n\n### Require Vulkan in your app's manifest\n\nGoogle Play uses `\u003cuses-feature\u003e` declared in your app manifest to filter your\napp from devices that don't meet its hardware and software feature requirements.\nDevices using Vulkan 1.0 **might not** support the required extension, but devices\ncompatible with Vulkan 1.1 **must** have the required extension starting in Android 10 (API level 29)."]]