在 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):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 by setting \u003ccode\u003eArTextureUpdateMode\u003c/code\u003e to \u003ccode\u003eAR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThis hardware buffer can be accessed using \u003ccode\u003eArFrame_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 is supported on Android API levels 27 and above, requiring devices to support the \u003ccode\u003eVK_ANDROID_external_memory_android_hardware_buffer\u003c/code\u003e extension.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure compatibility, declare Vulkan requirements in your app's manifest using \u003ccode\u003e<uses-feature>\u003c/code\u003e, targeting devices with Vulkan 1.1 for guaranteed support on Android 10 and above.\u003c/p\u003e\n"],["\u003cp\u003eA sample implementation of Vulkan rendering with ARCore can be found in the \u003ccode\u003ehello_ar_vulkan_c\u003c/code\u003e sample app.\u003c/p\u003e\n"]]],["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"],null,["# Render your AR app using Vulkan on Android NDK (C)\n\nWhen the [`ArTextureUpdateMode`](/ar/reference/c/group/ar-config#artextureupdatemode) is set to [`AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER`](/ar/reference/c/group/ar-config#ar_texture_update_mode_expose_hardware_buffer), ARCore will provide an Android [hardware buffer](https://developer.android.com/ndk/reference/group/a-hardware-buffer) when [`ArSession_update()`](/ar/reference/c/group/ar-session#arsession_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 [`ArTextureUpdateMode`](/ar/reference/c/group/ar-config#artextureupdatemode) determines how ARCore will update the camera texture. When it is set to [`AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER`](/ar/reference/c/group/ar-config#ar_texture_update_mode_expose_hardware_buffer), ARCore will provide the camera image through a [`AHardwareBuffer`](https://developer.android.com/ndk/reference/group/a-hardware-buffer).\n\nConfigure the session to use [`AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER`](/ar/reference/c/group/ar-config#ar_texture_update_mode_expose_hardware_buffer): \n\n```c\nArConfig* ar_config = NULL;\nArConfig_create(ar_session, &ar_config);\nArConfig_setTextureUpdateMode(ar_session, ar_config,\n AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER);\nCHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);\nArConfig_destroy(ar_config);\n```\n| **Note:** When using [`AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER`](/ar/reference/c/group/ar-config#ar_texture_update_mode_expose_hardware_buffer) on incompatible devices, [`ArSession_configure()`](/ar/reference/c/group/ar-session#arsession_configure) will fail and return [`AR_ERROR_UNSUPPORTED_CONFIGURATION`](/ar/reference/c/group/shared-types#ar_error_unsupported_configuration).\n\nObtain the hardware buffer\n--------------------------\n\nWhen [`AR_TEXTURE_UPDATE_MODE_EXPOSE_HARDWARE_BUFFER`](/ar/reference/c/group/ar-config#ar_texture_update_mode_expose_hardware_buffer) is enabled, use [`ArFrame_getHardwareBuffer()`](/ar/reference/c/group/ar-frame#arframe_gethardwarebuffer) to get the hardware buffer: \n\n```c\nvoid* native_hardware_buffer = NULL;\nArFrame_getHardwareBuffer(ar_session, ar_frame, &native_hardware_buffer);\n\nif ((int64_t)native_hardware_buffer == 0) {\n // The hardware buffer isn't ready yet.\n return;\n}\n```\n| **Note:** This [`AHardwareBuffer`](https://developer.android.com/ndk/reference/group/a-hardware-buffer) is only guaranteed to be valid until the next call to `ArSession_update()`. If you want to use the hardware buffer beyond that, such as for rendering, you must call [`AHardwareBuffer_acquire()`](https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer_acquire) and then call [`AHardwareBuffer_release()`](https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer_release) after your rendering is complete.\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)."]]