Vulkan 使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档将通过下载、编译和运行 Khronos© 示例应用的方式,介绍如何开始使用 Vulkan 图形库。
前提条件
首先,请确保您已准备好合适的硬件和平台版本。
您应使用支持 Vulkan 且搭载 Android 7.0 (Nougat)(API 级别 24 或更高级别)的设备或模拟器。
您可以前往设置菜单并依次选择关于手机 > Android 版本,确认您的 Android 版本。确认设置好合适的硬件和平台版本后,您可以下载所需的软件。
下载
首先,您必须下载多个工具和其他软件。请注意,在 Windows 主机上,建议您不要为工具和源代码使用深层文件路径层次结构;这样做是为了绕开部分 Windows 操作系统版本对文件路径的限制。
- 如果您还没有 Android Studio,请下载。这将包含最新的 Android SDK。
- 从 Android Studio 中安装 NDK 和 CMake,或单独下载并安装它们。
- 构建并运行 Hello JNI 示例,确保 Android Studio 正常运行。
- 为您的主机平台安装 build.md 中列出的 python3 和其他组件。
导入
在本部分中,您将下载 Khronos© Vulkan© 示例代码库,生成一个 Android Gradle 项目,然后使用 Android Studio IDE 打开该项目。
- 设置以下环境变量:
export ANDROID_HOME=/path/to/sdk-directory
export ANDROID_NDK_HOME=$ANDROID_HOME/ndk/{your-ndk-version-dir}
- 将 CMake 添加到用于生成 Android 构建脚本的 $PATH:
export PATH=$PATH:$ANDROID_HOME/cmake/{your-cmake-version}/bin
- 打开终端并将源代码下载到您的开发目录:
cd dev-directory
git clone --recursive https://github.com/KhronosGroup/Vulkan-Samples.git
- 请按照以下说明(来自 Build.md)生成 Android 示例项目:
cd Vulkan-Samples
./scripts/generate.py android
- 打开 Android Studio。依次选择 File > Open,然后再选择
Vulkan-Samples/build/android_gradle/build.gradle
。
在 Android Studio 加载项目后,系统会显示类似于下图所示的窗口:
图 1.
Android Studio 中的示例项目。
编译
此代码库中的所有示例会划归到一个 Android 项目中。如需编译项目,请执行以下任一操作:
- 如果只需编译源代码,请使用菜单依次选择 Build > Make Project,或按下 Ctrl-F9 快捷键。
- 如需生成示例 APK,请依次选择菜单 Build > Build Bundle(s)/APK(s) > Build APK(s)。
您应该会在 Android Studio 的 Build 窗口中看到构建成功的消息。如果窗口中显示错误消息,请修复错误并重新编译。
图 2. 示例构建成功。
执行
在运行示例项目之前,请确保 Android Studio 可识别您连接的 Vulkan 设备或 Android 模拟器。您应该会看到类似下图所示的内容:
图 3. 将测试设备连接到 Android Studio。
如需运行项目,请执行以下操作:
- 请依次使用菜单 Run > Run vulkan_sample,或点击工具栏中的“Run”按钮
,然后等待示例在已连接的设备上安装并启动。
- 在已连接的 Android 设备上,授权所需的访问权限请求。
- 启用 Allow access to manage all files,然后点按箭头 Back button 以返回示例的主开始屏幕。
- 允许磁盘访问:
图 4. 启用磁盘访问权限。
- 您应该会看到类似于下图所示的示例主菜单屏幕:
图 5. 示例主菜单。
- 浏览示例列表,然后选择几项运行。
如果您不熟悉 Vulkan 开发,可以从“API”示例着手。例如,点按“Hello Triangle”后,系统应该会显示一个经过渲染的三角形,类似下图所示:
图 6. Hello Triange 示例。
您的开发系统现已设置为在测试设备上运行示例。
Vulkan 示例是面向多种操作系统开发的,包括桌面设备和移动设备的操作系统。Performance、Extensions 和 Tooling 下的部分示例在您的设备上运行时可能会不稳定并发生崩溃。这可能是由各种原因造成的,例如:
- 特定的 Vulkan 功能并非面向 Android 设计。
- Android OS 版本不受支持。
- 硬件平台的 GPU 功能。
探索
Vulkan 示例的 Java 部分派生自 NativeActivity 类。该类会将典型的应用生命周期事件(如应用的创建、启动、停止和销毁)传递给 C/C++ 代码。在示例的 C/C++ 部分中,有一个示例框架可实现运行时子示例切换功能。大体上讲,Android 系统事件/消息会经过以下路径传递给示例应用的 Vulkan 代码:
NativeSampleActivity
Java 部分
NativeSampleActivity
C/C++ 部分
android_native_glue
代码
android_main
- 示例框架
- 各个子示例的代码
android_main
是 NativeSampleActivity
与应用代码之间的桥梁,可作为遵守示例代码的着手点。如果您只想关注特定的 Vulkan 源代码,则可以浏览 Vulkan_Samples\samples
下的代码,其中包含以下内容:
- “API”类别示例。
- “Performance”类别示例。
- “Extensions”类别示例。
- “Tooling”示例。
Vulkan_Samples\shaders
是所有着色器的存储位置。
您可以从浏览“API”类别的示例开始,逐步熟悉 Vulkan 的基本用法和示例框架。然后,您可以继续探索“Performance”和“Extenstions”类别的示例。如需了解着色器代码,您可以使用 Android Studio 中的 Project 视图。
图 7. 使用 Studio 探索着色器。
其他资源
Vulkan API 已经历了几个版本,因此日趋成熟。Vulkan 标准委员会和 Vulkan 社区已经创建了一组内容丰富的 Vulkan 资料,其中演示了该 API 的用法和最佳做法。以下列表包含一些关于 Vulkan 应用开发的资源:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-22。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-08-22。"],[],[],null,["# Get started with Vulkan\n\n| **Note:** Although this page includes [NativeActivity](/reference/android/app/NativeActivity) description, [GameActivity inside AGDK jetpack library](/games/agdk/integrate-game-activity) is an updated and well maintained implementation of `NativeActivity`, with more functionality and fast release cycles. It is highly recommended to use `GameActivity` for your new projects.\n\nThis document outlines how to get started with the Vulkan graphics library\nby downloading, compiling, and running Khronos© sample app.\n\nPrerequisites\n-------------\n\nBefore beginning, make sure you have the right hardware and platform version prepared.\nYou should use a device or an [emulator](/studio/run/emulator) that supports Vulkan,\nrunning Android 7.0 (Nougat), API level 24 or higher.\n\nYou can confirm your Android version by going to the **Settings** menu,\nand selecting **About phone** \\\u003e **Android Version**. Once you've confirmed that\nyou have the right hardware and platform version set up, you can download the necessary software.\n\nDownload\n--------\n\nBefore getting started, you must download several tools and other software. Note that on a Windows host,\nit is recommended that you avoid a deep file path hierarchy for tools and source code;\nthis is to work around file path limits on some Windows OS versions.\n\n1. If you don't already have Android Studio, [download it](/studio). This includes the most recent Android SDK.\n2. [Install the NDK and CMake](/studio/projects/install-ndk) from within Android Studio or [download and install](/ndk/downloads) them separately.\n3. Build and Run the [Hello JNI sample](https://github.com/android/ndk-samples/tree/main/hello-jni) to ensure Android Studio is working properly.\n4. Install python3 and other components listed in [build.md](https://github.com/KhronosGroup/Vulkan-Samples/blob/main/docs/build.adoc#android) for your host platform.\n\nImport\n------\n\nIn this section, you download [the Khronos© Vulkan© sample repository](https://github.com/KhronosGroup/Vulkan-Samples/), generate an Android gradle\nproject, then open it with the Android Studio IDE.\n\n1. Set the following environment variables: \n\n ```\n export ANDROID_HOME=/path/to/sdk-directory\n export ANDROID_NDK_HOME=$ANDROID_HOME/ndk/{your-ndk-version-dir}\n ```\n2. Add CMake to the $PATH, which is used to generate Android build scripts: \n\n ```\n export PATH=$PATH:$ANDROID_HOME/cmake/{your-cmake-version}/bin\n ```\n3. Open a terminal and download the source code to your development directory: \n\n ```\n cd dev-directory\n git clone --recursive https://github.com/KhronosGroup/Vulkan-Samples.git\n ```\n4. Follow these instructions (from [Build.md](https://github.com/KhronosGroup/Vulkan-Samples/blob/master/docs/build.md#android)) to generate the Android sample project: \n\n ```\n cd Vulkan-Samples\n ./scripts/generate.py android\n ```\n5. Open Android Studio. Choose **File \\\u003e Open** and select `Vulkan-Samples/build/android_gradle/build.gradle`. You should see something similar to the following after Android Studio loads the project: \n\n \u003cbr /\u003e\n\n **Figure 1.** The sample project inside Android Studio.\n\n \u003cbr /\u003e\n\nCompile\n-------\n\nAll samples in this repo are organized into one Android project. To compile the\nproject, do one of the following:\n\n- To just compile the sources, use menu, **Build** \\\u003e **Make Project** , or type the **Ctrl-F9** shortcut key.\n- To generate the sample APK, select menu **Build** \\\u003e **Build Bundle(s)/APK(s)** \\\u003e **Build APK(s)**.\n\nYou should see the build successful message inside Android Studio's **Build** window.\nIn case there are errors showing up, fix them and re-compile. \n\n\u003cbr /\u003e\n\n**Figure 2.** A successful sample build.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nExecute\n-------\n\nBefore running the sample project, make sure Android Studio recognizes your\nconnected Vulkan device or Android Emulator. You should see something like the\nfollowing: \n\n\u003cbr /\u003e\n\n**Figure 3.** Connect the test device to Android Studio.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nTo run the project, do the following:\n\n1. Use menu **Run \\\u003e Run vulkan_sample** , or click on the run button on the toolbar, and wait for the sample to get installed and started on your connected device.\n2. On your connected Android device, authorize the needed access requests.\n - enable **Allow access to manage all files** , then tap the arrow **Back button** to return the sample main start screen.\n - allow the disk access: \n\n \u003cbr /\u003e\n\n **Figure 4.** Enable disk access.\n\n \u003cbr /\u003e\n\n3. You should see the sample main menu screen, similar to the following: \n\n \u003cbr /\u003e\n\n **Figure 5.** Sample main menu.\n\n \u003cbr /\u003e\n\n4. Browse through the sample list, and select a few to run. If you are new to Vulkan development, you can start with \"API\" samples. For example, tapping \"Hello Triangle\" should display a rendered triangle similar to the following: \n\n \u003cbr /\u003e\n\n **Figure 6.** Hello Triange sample.\n\n \u003cbr /\u003e\n\nYour development system is now set up to run samples on your test device.\n\nVulkan Samples are developed for multiple operating systems, including those for\ndesktop and mobile. Some samples under **Performance** , **Extensions** , and\n**Tooling** may be unstable and crash on your device. This might be due to various reasons, such\nas:\n\n- The specific Vulkan features weren't designed for Android.\n- Your Android OS version is unsupported.\n- The GPU capability of your hardware platform.\n\nExplore\n-------\n\nThe Java section of the Vulkan sample derives from the [NativeActivity](/reference/android/app/NativeActivity) class. It passes typical application\nlifecycle events, such as app creation, start, stop, and destroy to the C/C++\ncode. In the C/C++ section of the sample, there is a sample framework\nthat implements the run-time sub sample switching functionality. At a very\nhigh level, Android system events/messages go through the following path to\nreach the sample app's Vulkan code:\n\n- `NativeSampleActivity` Java section\n- `NativeSampleActivity` C/C++ section\n- `android_native_glue` code\n- `android_main`\n- Sample framework\n- Individual sub sample's code\n\n`android_main` is the bridge between `NativeSampleActivity` and the app code,\nwhich can be the starting point for you to follow the sample code. If you just\nwant to focus on the Vulkan specific sources, you can explore the code under\n`Vulkan_Samples\\samples`, which contains the following:\n\n- The \"api\" category samples.\n- The \"performance\" category samples.\n- The \"extensions\" category samples.\n- The \"tooling\" samples.\n\nThe `Vulkan_Samples\\shaders` are the home for all shaders.\n\nYou can start browsing the \"API\" category samples to get familiar with basic\nVulkan usage and the sample framework. Then you can progress to the\n\"Performance\" and \"Extenstions\" category samples. For shader code, you can use\nthe **Project** view in Android Studio. \n\n\u003cbr /\u003e\n\n**Figure 7.** Explore shader with Studio.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nAdditional resources\n--------------------\n\nThe Vulkan API has been through a few versions, so it is maturing. The Vulkan\nstandard committee and the Vulkan community have created a rich set of Vulkan\nmaterial that demonstrates the API's usage and best practices. The following\nlist contains some resources for Vulkan application development:\n\n- **Vulkan Specification.**\n The Khronos Group maintains the Vulkan specification. See\n the [Vulkan homepage](https://www.khronos.org/vulkan) for the full specification,\n training, [guides](https://github.com/KhronosGroup/Vulkan-Guide)\n and [tutorials](https://www.vulkan.org/learn#key-resources).\n\n- **Validation Layers.**\n Validation Layers are essential for application development.\n See the [Vulkan validation layers on Android](/ndk/guides/graphics/validation-layer)\n documentation for details.\n\n- **Shaderc.**\n Shaderc code in the NDK is the downstream of the\n [Shaderc repo](https://github.com/google/shaderc). For the usage documentation\n and instructions to get the latest version, see\n [Shader compilers](/ndk/guides/graphics/shader-compilers)."]]