開始使用 Vulkan
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文件概述如何開始使用 Vulkan 圖形庫
下載、編譯並執行 Khronos© 範例應用程式。
必要條件
在開始之前,請先確認您已準備好適當的硬體和平台版本。
您必須使用支援 Vulkan、
搭載 Android 7.0 (Nougat) 的 API 級別 24 以上版本。
如要確認您的 Android 版本,請前往「設定」選單。
,然後選取「關於手機」>Android 版本。確認
硬體和平台版本均已設定妥當,即可下載必要的軟體。
下載
在開始之前,您必須先下載幾項工具和其他軟體。請注意,在 Windows 主機上
建議您避免為工具和原始碼檔案設定階層過多的路徑。
是為瞭解決部分 Windows 作業系統版本的檔案路徑限制。
- 如果您尚未安裝 Android Studio,請先下載 Android Studio,
這包括最新版的 Android SDK。
- 從 Android Studio 中安裝 NDK 和 CMake
或個別下載並安裝這些項目
- 建構及執行 Hello JNI 範例
確保 Android Studio 正常運作
- 安裝 python3 和 build.md 中列出的其他元件
以及主機平台的 10 層
匯入
您將在本節中下載 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 新增至 $PATH,用於產生 Android 建構指令碼:
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。選擇 [檔案] > 開啟並選取
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)」>建構 APK。
完成後,Android Studio 的「Build」(建構) 視窗應會顯示建構成功的訊息。
假如您看到錯誤,請修正錯誤並重新編譯。
圖 2.建構成功的範例。
執行
執行範例專案之前,請確認 Android Studio 可辨識
。您應該會看到類似
包括:
圖 3.將測試裝置連結至 Android Studio。
如要執行專案,請按照下列步驟操作:
- 使用選單 [執行] >執行 vulkan_sample,或點選執行按鈕
,然後等待
以便在連結的裝置上安裝並開始使用。
- 在已連結的 Android 裝置上,為必要的存取要求提供授權。
- 啟用「允許管理所有檔案」,然後輕觸「返回」按鈕。
,系統會傳回範例的啟動主畫面。
- 授予磁碟存取權:
圖 4.允許存取磁碟。
- 您看到的範例主選單畫面應如下所示:
圖 5.範例主選單。
- 瀏覽範例清單,然後選取要執行的幾個範例。
如果您是第一次使用 Vulkan 進行開發,可以先從「API」範例開始執行。
例如,輕觸「Hello Triangle」後,畫面上應會顯示算繪的三角形
類似這樣:
圖 6.Hello Triange 範例。
現在開發系統已設定完畢,可在測試裝置上執行範例專案。
Vulkan 範例專為在多種作業系統上執行而開發,包括
電腦和行動裝置「成效」、「擴充功能」及
工具可能導致裝置不穩定或當機。可能的原因包括
為:
- 特定 Vulkan 功能並非專為 Android 設計。
- 系統不支援您的 Android 作業系統版本。
- 硬體平台的 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
。下列
清單包含一些用於開發 Vulkan 應用程式的資源:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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)."]]