Android 图形着色语言 (AGSL)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 13 及更高版本使用 Android 图形着色语言 (AGSL) 来定义可编程 RuntimeShader
对象的行为。AGSL 与 GLSL fragment 着色器共用大部分语法,但在 Android 图形渲染系统中,可以自定义 Canvas
中的绘制以及过滤 View
内容。
运算理论
AGSL 效果是更大的 Android 图形管道的一部分。当 Android 发出 GPU 加速的绘制操作时,它会组建单个 GPU 片段着色器来执行所需的工作。该着色器通常包含多个部分。例如:
- 评估像素是落在要绘制的形状内部还是外部(或者位于边框上,此时可能会应用抗锯齿)。
- 评估像素是位于裁剪区域内部还是外部(同样,针对边框像素采用可能的抗锯齿逻辑)。
Paint
上 Shader
的逻辑。着色器实际上可以是对象树(借助 ComposeShader
和下文介绍的其他功能)。
- 与
ColorFilter
的逻辑类似。
- 混合代码(适用于特定类型的
BlendMode
)。
- 颜色空间转换代码,是 Android 颜色管理的一部分。
- 当
Paint
在 Shader
、ColorFilter
或 BlendMode
字段中具有复杂的对象树时,仍然只有一个 GPU fragment 着色器。该树中的每个节点都会创建一个函数。裁剪代码和几何图形代码都会创建一个函数。混合代码可能会创建一个函数。然后,整个 fragment 着色器会调用所有这些函数(可能会调用其他函数,例如在着色器树的情况下)。
您的 AGSL 效果会为 GPU 的 fragment 着色器贡献一个或多个函数。
基本语法
AGSL(和 GLSL)是 C 样式的领域特定语言。bool
和 int
等类型会密切跟踪其 C 语言等效项;还有其他类型可支持支持域功能的向量和矩阵。
限定符可以按着色语言独有的方式应用于精度提示的类型。if-else
语句等控制结构的工作方式与在 C 语言中非常相似;该语言还支持 switch
语句和 for
循环,但存在一些限制。某些控制结构需要使用可以在编译时求值的常量表达式。
AGSL 支持函数;每个着色器程序都以 main
函数开头。支持用户定义的函数,不支持任何类型的递归。函数使用“值返回”调用规范;在调用函数时,传递给函数的值会被复制到参数中,而输出会被复制回;这由 in
、out
和 inout
限定符决定。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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."]]