Android 圖形著色語言 (AGSL)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 13 以上版本會使用 Android 圖形著色語言 (AGSL) 定義可設定程式的 RuntimeShader
物件行為。AGSL 與 GLSL 片段著色器共用大部分的語法,但可在 Android 圖形轉譯系統中自訂 Canvas
中的繪製作業,以及篩選 View
內容。
運算理論
AGSL 效果屬於大型 Android 圖形管道的一部分。Android 發出 GPU 加速繪圖作業時,會組合單一 GPU 片段著色器來執行必要工作。這個著色器通常包含數個部分。例如:
- 評估像素是否位於繪製的形狀內部或外部 (或邊框上可能會套用反鋸齒)。
- 評估像素是否位於裁切區域內部或外部 (同樣可以對邊框像素有可能的反鋸齒邏輯)。
Paint
中的 Shader
邏輯。著色器實際上可以是物件的樹狀結構 (由於 ComposeShader
和下文所述的其他功能)。
ColorFilter
的類似邏輯。
- 混合程式碼 (適用於特定類型的
BlendMode
)。
- 色域轉換程式碼,這是 Android 色彩管理的一部分。
- 當
Paint
在 Shader
、ColorFilter
或 BlendMode
欄位中含有複雜的物件樹狀結構時,仍然只有一個 GPU 片段著色器。該樹狀結構中的每個節點都會建立一個函式。剪輯程式碼和幾何圖形程式碼會建立一個函式。混合程式碼可能會建立函式。接著,整體片段著色器會呼叫所有這些函式 (可能會呼叫其他函式,例如使用著色器樹狀結構的情況)。
AGSL 效果會為 GPU 的片段著色器提供函式 (或函式)。
基本語法
AGSL (和 GLSL) 是 C 式網域語言。bool
和 int
等類型會密切追蹤其 C 的對等項目;您還可以使用其他類型支援網域功能的向量和矩陣。
限定詞可套用至類型以提供精確度提示,這是對語言底色的獨特方式。if-else
陳述式等控制結構的運作方式與在 C 中非常類似,語言也支援 switch
陳述式和 for
迴圈,但有限制。部分控制項結構需要可在編譯期間評估的常數運算式。
AGSL 支援函式;每個著色器程式都是從 main
函式開頭。支援使用者定義函式,不支援任何類型的遞迴。函式採用「值傳回」的呼叫慣例;傳送至函式的值會在呼叫函式時複製到參數中,並複製輸出內容 (取決於 in
、out
和 inout
限定詞)。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Android Graphics Shading Language (AGSL) is used by Android 13 and above to\ndefine the behavior of programmable\n[`RuntimeShader`](/reference/android/graphics/RuntimeShader) objects. AGSL\nshares much of its syntax with GLSL fragment shaders, but works within the\nAndroid graphics rendering system to both customize painting within `Canvas`\nand filter `View` content.\n\nTheory of operation\n-------------------\n\nAGSL effects exist as part of the larger Android graphics pipeline. When Android\nissues a GPU accelerated drawing operation, it assembles a single GPU fragment\nshader to do the required work. This shader typically includes several pieces.\nFor example, it might include:\n\n- Evaluating whether a pixel falls inside or outside of the shape being drawn (or on the border, where it might apply anti-aliasing).\n- Evaluating whether a pixel falls inside or outside of the clipping region (again, with possible anti-aliasing logic for border pixels).\n- Logic for the [`Shader`](/reference/android/graphics/Shader) on the [`Paint`](/reference/android/graphics/Paint). The Shader can actually be a tree of objects (due to [`ComposeShader`](/reference/android/graphics/ComposeShader) and other features described below).\n- Similar logic for the [`ColorFilter`](/reference/android/graphics/ColorFilter).\n- Blending code (for certain types of [`BlendMode`](/reference/android/graphics/BlendMode)).\n- Color space conversion code, as part of Android's color management.\n- When the `Paint` has a complex tree of objects in the `Shader`, `ColorFilter`, or `BlendMode` fields, there is still only a single GPU fragment shader. Each node in that tree creates a single function. The clipping code and geometry code each create a function. The blending code might create a function. The overall fragment shader then calls all of these functions (which may call other functions, e.g. in the case of a shader tree).\n\nYour AGSL effect contributes a function (or functions) to the GPU's fragment shader.\n\nBasic syntax\n------------\n\nAGSL (and GLSL) are C-style domain specific languages. Types such as `bool` and\n`int` closely track their C equivalents; there are additional types to\nsupport vectors and matrices that support domain functionality.\n\nQualifiers can be applied to types for precision hints in a way that's unique to shading languages. Control structures such as `if-else` statements work much\nlike they do in C; the language also provides support for `switch` statements\nand `for` loops with limitations. Some control structures require constant expressions that can be evaluated at compile time.\n\nAGSL supports functions; every shader program begins with the `main` function.\nUser defined functions are supported, without support for recursion of any kind.\nFunctions use a \"value-return\" calling convention; values passed to functions are\ncopied into parameters when the function is called, and outputs are copied\nback; this is determined by the `in`, `out`, and `inout` qualifiers."]]