| 1 |
Asahi |
| 2 |
===== |
| 3 |
|
| 4 |
The Asahi driver aims to provide an OpenGL implementation for the Apple M1. |
| 5 |
|
| 6 |
Wrap (macOS only) |
| 7 |
----------------- |
| 8 |
|
| 9 |
Mesa includes a library that wraps the key IOKit entrypoints used in the macOS |
| 10 |
UABI for AGX. The wrapped routines print information about the kernel calls made |
| 11 |
and dump work submitted to the GPU using agxdecode. This facilitates |
| 12 |
reverse-engineering the hardware, as glue to get at the "interesting" GPU |
| 13 |
memory. |
| 14 |
|
| 15 |
The library is only built if ``-Dtools=asahi`` is passed. It builds a single |
| 16 |
``wrap.dylib`` file, which should be inserted into a process with the |
| 17 |
``DYLD_INSERT_LIBRARIES`` environment variable. |
| 18 |
|
| 19 |
For example, to trace an app ``./app``, run: |
| 20 |
|
| 21 |
DYLD_INSERT_LIBRARIES=~/mesa/build/src/asahi/lib/libwrap.dylib ./app |
| 22 |
|
| 23 |
Hardware varyings |
| 24 |
----------------- |
| 25 |
|
| 26 |
At an API level, vertex shader outputs need to be interpolated to become |
| 27 |
fragment shader inputs. This process is logically pipelined in AGX, with a value |
| 28 |
traveling from a vertex shader to remapping hardware to coefficient register |
| 29 |
setup to the fragment shader to the iterator hardware. Each stage is described |
| 30 |
below. |
| 31 |
|
| 32 |
Vertex shader |
| 33 |
````````````` |
| 34 |
|
| 35 |
A vertex shader (running on the :term:`Unified Shader Cores`) outputs varyings with the |
| 36 |
``st_var`` instruction. ``st_var`` takes a *vertex output index* and a 32-bit |
| 37 |
value. The maximum number of *vertex outputs* is specified as the "output count" |
| 38 |
of the shader in the "VDM State Vertex Outputs" structure. The value may be interpreted |
| 39 |
consist of a single 32-bit value or an aligned 16-bit register pair, depending |
| 40 |
on whether interpolation should happen at 32-bit or 16-bit. Vertex outputs are |
| 41 |
indexed starting from 0, with the *vertex position* always coming first, the |
| 42 |
32-bit user varyings coming next with perspective, flat, and linear interpolated |
| 43 |
varyings grouped in that order, then 16-bit user varyings with the same groupings, |
| 44 |
and finally *point size*, *layer/viewport*, and *clip distances* at the end if present. Note that |
| 45 |
*clip distances* are not accessible from the fragment shader; if the fragment |
| 46 |
shader needs to read the interpolated clip distance, the vertex shader must |
| 47 |
*also* write the clip distance values to a user varying for the fragment shader |
| 48 |
to interpolate. Also note there is no clip plane enable mask anywhere; that must |
| 49 |
lowered for APIs that require this (OpenGL but not Vulkan). |
| 50 |
|
| 51 |
.. list-table:: Ordering of vertex outputs with all outputs used |
| 52 |
:widths: 25 75 |
| 53 |
:header-rows: 1 |
| 54 |
|
| 55 |
* - Size (words) |
| 56 |
- Value |
| 57 |
* - 4 |
| 58 |
- Vertex position |
| 59 |
* - 1 |
| 60 |
- 32-bit smooth varying 0 |
| 61 |
* - |
| 62 |
- ... |
| 63 |
* - 1 |
| 64 |
- 32-bit smooth varying m |
| 65 |
* - 1 |
| 66 |
- 32-bit flat varying 0 |
| 67 |
* - |
| 68 |
- ... |
| 69 |
* - 1 |
| 70 |
- 32-bit flat varying n |
| 71 |
* - 1 |
| 72 |
- 32-bit linear varying 0 |
| 73 |
* - |
| 74 |
- ... |
| 75 |
* - 1 |
| 76 |
- 32-bit linear varying o |
| 77 |
* - 1 |
| 78 |
- Packed pair of 16-bit smooth varyings 0 |
| 79 |
* - |
| 80 |
- ... |
| 81 |
* - 1 |
| 82 |
- Packed pair of 16-bit smooth varyings p |
| 83 |
* - 1 |
| 84 |
- Packed pair of 16-bit flat varyings 0 |
| 85 |
* - |
| 86 |
- ... |
| 87 |
* - 1 |
| 88 |
- Packed pair of 16-bit flat varyings q |
| 89 |
* - 1 |
| 90 |
- Packed pair of 16-bit linear varyings 0 |
| 91 |
* - |
| 92 |
- ... |
| 93 |
* - 1 |
| 94 |
- Packed pair of 16-bit linear varyings r |
| 95 |
* - 1 |
| 96 |
- Point size |
| 97 |
* - 1 |
| 98 |
- Layer/viewport |
| 99 |
* - 1 |
| 100 |
- Clip distance for plane 0 |
| 101 |
* - |
| 102 |
- ... |
| 103 |
* - 1 |
| 104 |
- Clip distance for plane 16 |
| 105 |
|
| 106 |
Remapping |
| 107 |
````````` |
| 108 |
|
| 109 |
Vertex outputs are remapped to varying slots to be interpolated. |
| 110 |
The output of remapping consists of the following items: the *W* fragment |
| 111 |
coordinate, the *Z* fragment coordinate, user varyings in the vertex |
| 112 |
output order. *Z* may be omitted, but *W* may not be. This remapping is |
| 113 |
configured by the "Output select" word. |
| 114 |
|
| 115 |
.. list-table:: Ordering of remapped slots |
| 116 |
:widths: 25 75 |
| 117 |
:header-rows: 1 |
| 118 |
|
| 119 |
* - Index |
| 120 |
- Value |
| 121 |
* - 0 |
| 122 |
- Fragment coord W |
| 123 |
* - 1 |
| 124 |
- Fragment coord Z |
| 125 |
* - 2 |
| 126 |
- 32-bit varying 0 |
| 127 |
* - |
| 128 |
- ... |
| 129 |
* - 2 + m |
| 130 |
- 32-bit varying m |
| 131 |
* - 2 + m + 1 |
| 132 |
- Packed pair of 16-bit varyings 0 |
| 133 |
* - |
| 134 |
- ... |
| 135 |
* - 2 + m + n + 1 |
| 136 |
- Packed pair of 16-bit varyings n |
| 137 |
|
| 138 |
Coefficient registers |
| 139 |
````````````````````` |
| 140 |
|
| 141 |
The fragment shader does not see the physical slots. |
| 142 |
Instead, it references varyings through *coefficient registers*. A coefficient |
| 143 |
register is a register allocated constant for all fragment shader invocations in |
| 144 |
a given polygon. Physically, it contains the values output by the vertex shader |
| 145 |
for each vertex of the polygon. Coefficient registers are preloaded with values |
| 146 |
from varying slots. This preloading appears to occur in fixed function hardware, |
| 147 |
a simplification from PowerVR which requires a specialized program for the |
| 148 |
programmable data sequencer to do the preload. |
| 149 |
|
| 150 |
The "Fragment Shader" structure points to coefficient register bindings, |
| 151 |
preceded by a header. The header contains the number of 32-bit varying slots. As |
| 152 |
the *W* slot is always present, this field is always nonzero. Slots whose index |
| 153 |
is below this count are treated as 32-bit. The remaining slots are treated as |
| 154 |
16-bits. |
| 155 |
|
| 156 |
The header also contains the total number of coefficient registers bound. |
| 157 |
|
| 158 |
Each binding that follows maps a (vector of) varying slots to a (consecutive) |
| 159 |
coefficient registers. Some details about the varying (perspective |
| 160 |
interpolation, flat shading, point sprites) are configured here. |
| 161 |
|
| 162 |
Coefficient registers may be ordered the same as the internal varying slots. |
| 163 |
However, this may be inconvenient for some APIs that require a separable shader |
| 164 |
model. For these APIs, the flexibility to mix-and-match slots and coefficient |
| 165 |
registers allows mixing shaders without shader variants. In that case, the |
| 166 |
bindings should be generated outside of the compiler. For simple APIs where the |
| 167 |
bindings are fixed and known at compile-time, the bindings could be generated |
| 168 |
within the compiler. |
| 169 |
|
| 170 |
Mathematically, the value of the coefficient register is a vector in |
| 171 |
:math:`\mathbb{R}^3`. The X and Y components are the screen-space partial |
| 172 |
derivatives of the varying with respect to X and Y. The Z component is the |
| 173 |
interpolated value of the varying at the upper-left pixel in the 32x32 tile that |
| 174 |
the pixel belongs to. |
| 175 |
|
| 176 |
Fragment shader |
| 177 |
``````````````` |
| 178 |
|
| 179 |
In the fragment shader, coefficient registers, identified by the prefix ``cf`` |
| 180 |
followed by a decimal index, act as opaque handles to varyings. For flat |
| 181 |
shading, coefficient registers may be loaded into general registers with the |
| 182 |
``ldcf`` instruction. For smooth shading, the coefficient register corresponding |
| 183 |
to the desired varying is passed as an argument to the "iterate" instruction |
| 184 |
``iter`` in order to "iterate" (interpolate) a varying. As perspective correct |
| 185 |
interpolation also requires the W component of the fragment coordinate, the |
| 186 |
coefficient register for W is passed as a second argument. As an example, if |
| 187 |
there's a single varying to interpolate, an instruction like ``iter r0, cf1, cf0`` |
| 188 |
is used. |
| 189 |
|
| 190 |
It is occassionally useful to manipulate the raw coefficient registers, for |
| 191 |
example to implement interpolation modes not natively supported by the hardware. |
| 192 |
``ldcf`` is used for this purpose. |
| 193 |
|
| 194 |
Iterator |
| 195 |
```````` |
| 196 |
|
| 197 |
To actually interpolate varyings, AGX provides fixed-function iteration hardware |
| 198 |
to multiply the specified coefficient registers with the required barycentrics, |
| 199 |
producing an interpolated value, hence the name "coefficient register". This |
| 200 |
operation is purely mathematical and does not require any memory access, as |
| 201 |
the required coefficients are preloaded before the shader begins execution. |
| 202 |
That means the iterate instruction executes in constant time, does not signal |
| 203 |
a data fence, and does not require the shader to wait on a data fence before |
| 204 |
using the value. |
| 205 |
|
| 206 |
Image layouts |
| 207 |
------------- |
| 208 |
|
| 209 |
AGX supports several image layouts, described here. To work with image layouts |
| 210 |
in the drivers, use the ail library, located in ``src/asahi/layout``. |
| 211 |
|
| 212 |
Strided linear |
| 213 |
`````````````` |
| 214 |
|
| 215 |
The simplest layout is **strided linear**. Pixels are stored in raster-order in |
| 216 |
memory with a software-controlled stride. Strided linear images are useful for |
| 217 |
working with modifier-unaware window systems, however performance will suffer. |
| 218 |
Strided linear images have numerous limitations: |
| 219 |
|
| 220 |
- Strides must be a multiple of 16 bytes. |
| 221 |
- Strides must be nonzero. For 1D images where the stride is logically |
| 222 |
irrelevant, ail will internally select the minimal stride. |
| 223 |
- Only 1D, 2D, and 2D Array images may be linear. In particular, no 3D or cubemaps. |
| 224 |
- 2D images must not be mipmapped. |
| 225 |
- Block-compressed formats and multisampled images are unsupported. Elements of |
| 226 |
a strided linear image are simply pixels. |
| 227 |
|
| 228 |
With these limitations, addressing into a strided linear image is as simple as |
| 229 |
|
| 230 |
.. math:: |
| 231 |
|
| 232 |
\text{address} = (y \cdot \text{stride}) + (x \cdot \text{bytes per pixel}) |
| 233 |
|
| 234 |
In practice, this suffices for window system integration and little else. |
| 235 |
|
| 236 |
GPU-tiled |
| 237 |
````````` |
| 238 |
|
| 239 |
The most common uncompressed layout is **GPU-tiled**. The image is divided into |
| 240 |
power-of-two sized tiles. The tiles themselves are stored in raster-order. |
| 241 |
Within each tile, elements (pixels/blocks) are stored in Morton (Z) order. |
| 242 |
|
| 243 |
The tile size used depends on both the image size and the block size of the |
| 244 |
image format. For large images, :math:`n \times n` or :math:`2n \times n` tiles |
| 245 |
are used (:math:`n` power-of-two). :math:`n` is such that each page contains |
| 246 |
exactly one tile. Only power-of-two block sizes are supported in hardware, |
| 247 |
ensuring such a tile size always exists. The hardware uses 16 KiB pages, so tile |
| 248 |
sizes are as follows: |
| 249 |
|
| 250 |
.. list-table:: Tile sizes for large images |
| 251 |
:widths: 50 50 |
| 252 |
:header-rows: 1 |
| 253 |
|
| 254 |
* - Bytes per block |
| 255 |
- Tile size |
| 256 |
* - 1 |
| 257 |
- 128 x 128 |
| 258 |
* - 2 |
| 259 |
- 128 x 64 |
| 260 |
* - 4 |
| 261 |
- 64 x 64 |
| 262 |
* - 8 |
| 263 |
- 64 x 32 |
| 264 |
* - 16 |
| 265 |
- 32 x 32 |
| 266 |
|
| 267 |
The dimensions of large images are rounded up to be multiples of the tile size. |
| 268 |
In addition, non-power-of-two large images have extra padding tiles when |
| 269 |
mipmapping is used, see below. |
| 270 |
|
| 271 |
That rounding would waste a great deal of memory for small images. If |
| 272 |
an image is smaller than this tile size, a smaller tile size is used to reduce |
| 273 |
the memory footprint. For small images, the tile size is :math:`m \times m` |
| 274 |
where |
| 275 |
|
| 276 |
.. math:: |
| 277 |
|
| 278 |
m = 2^{\lceil \log_2( \min \{ \text{width}, \text{ height} \}) \rceil} |
| 279 |
|
| 280 |
In other words, small images use the smallest square power-of-two tile such that |
| 281 |
the image's minor axis fits in one tile. |
| 282 |
|
| 283 |
For mipmapped images, tile sizes are determined independently for each level. |
| 284 |
Typically, the first levels of an image are "large" and the remaining levels are |
| 285 |
"small". This scheme reduces the memory footprint of mipmapping, compared to a |
| 286 |
fixed tile size for the whole image. Each mip level are padded to fill at least |
| 287 |
one cache line (128 bytes), ensure no cache line contains multiple mip levels. |
| 288 |
|
| 289 |
There is a wrinkle: the dimensions of large mip levels in tiles are determined |
| 290 |
by the dimensions of level 0. For power-of-two images, the two calculations are |
| 291 |
equivalent. However, they differ subtly for non-power-of-two images. To |
| 292 |
determine the number of tiles to allocate for level :math:`l`, the number of |
| 293 |
tiles for level 0 should be right-shifted by :math:`2l`. That appears to divide |
| 294 |
by :math:`2^l` in both width and height, matching the definition of mipmapping, |
| 295 |
however it rounds down incorrectly. To compensate, the level contains one extra |
| 296 |
row, column, or both (with the corner) as required if any of the first :math:`l` |
| 297 |
levels were rounded down. This hurt the memory footprint. However, it means |
| 298 |
non-power-of-two integer multiplication is only required for level 0. |
| 299 |
Calculating the sizes for subsequent levels requires only addition and bitwise |
| 300 |
math. That simplifies the hardware (but complicates software). |
| 301 |
|
| 302 |
A 2D image consists of a full miptree (constructed as above) rounded up to the |
| 303 |
page size (16 KiB). |
| 304 |
|
| 305 |
3D images consist simply of an array of 2D layers (constructed as above). That |
| 306 |
means cube maps, 2D arrays, cube map arrays, and 3D images all use the same |
| 307 |
layout. The only difference is the number of layers. Notably, 3D images (like |
| 308 |
``GL_TEXTURE_3D``) reserve space even for mip levels that do not exist |
| 309 |
logically. These extra levels pad out layers of 3D images to the size of the |
| 310 |
first layer, simplifying layout calculations for both software and hardware. |
| 311 |
Although the padding is logically unnecessary, it wastes little space compared |
| 312 |
to the sizes of large mipmapped 3D textures. |
| 313 |
|
| 314 |
Twiddled |
| 315 |
```````` |
| 316 |
|
| 317 |
In addition to GPU-tiled images, AGX also has a fully **twiddled** layout. The |
| 318 |
image is rounded up to power-of-two dimensions, then all elements are stored in |
| 319 |
Morton (Z) order. |
| 320 |
|
| 321 |
A twiddled image is equivalent to a GPU-tiled image with a single square |
| 322 |
power-of-two tile spanning the entire image. That means GPU-tiled and twiddled |
| 323 |
images may share address calculation code, as long as everything is parametrized |
| 324 |
in terms of the tile size. |
| 325 |
|
| 326 |
In general, twiddled images require more memory than GPU-tiled images due to the |
| 327 |
extra padding required. Because GPU tiles are page-sized, twiddling beyond that |
| 328 |
does not offer any cache locality benefit either. The twiddled layout is |
| 329 |
mostly vestigial at this point, but the PBE requires it for sparse mapping. |
| 330 |
|
| 331 |
Sparse page tables |
| 332 |
`````````````````` |
| 333 |
|
| 334 |
The hardware has native support for sparse images. If an texture/PBE descriptor |
| 335 |
is configured in sparse mode, the specified address does not point to the image |
| 336 |
itself. Rather, it points to a **sparse page table**. The extra indirection |
| 337 |
enables on-device sparse binding (e.g. by updating the page table from a compute |
| 338 |
kernel). |
| 339 |
|
| 340 |
At the top level, the sparse page table is an array of **folios**. Each folio |
| 341 |
describes 256 pages. The folio itself has two halves. The first half is the page |
| 342 |
table itself, containing 4-byte "Sparse Block" data structures mapping image |
| 343 |
pages to GPU virtual addresses. The second half is probably sparse texture |
| 344 |
counters, again 4-bytes per page. Each folio therefore consumes :math:`256 \cdot |
| 345 |
4 \cdot 2 = 2 \mathrm{KiB}` in order to describe :math:`256 \cdot 16384 = |
| 346 |
4 \mathrm{MiB}`. |
| 347 |
|
| 348 |
In a layered (array or 3D) image, a given folio only describes a single layer. |
| 349 |
That implies extra padding between layers. |
| 350 |
|
| 351 |
Within a layer, the table works purely at an address level. It maps pages to |
| 352 |
pages, rather than tiles to tiles. It is not a spatial data structure in itself. |
| 353 |
Rather, it inherits the tiling of the image by virtue of the addresses mapped. |
| 354 |
This presumably simplifies the hardware implementation. |
| 355 |
|
| 356 |
drm-shim (Linux only) |
| 357 |
--------------------- |
| 358 |
|
| 359 |
Mesa includes a library that mocks out the DRM UABI used by the Asahi driver |
| 360 |
stack, allowing the Mesa driver to run on non-M1 Linux hardware. This can be |
| 361 |
useful for exercising the compiler. To build, use options: |
| 362 |
|
| 363 |
:: |
| 364 |
|
| 365 |
-Dgallium-drivers=asahi -Dtools=drm-shim |
| 366 |
|
| 367 |
Then run an OpenGL workload with environment variable: |
| 368 |
|
| 369 |
.. code-block:: sh |
| 370 |
|
| 371 |
LD_PRELOAD=~/mesa/build/src/asahi/drm-shim/libasahi_noop_drm_shim.so |
| 372 |
|
| 373 |
For example to compile a shader with shaderdb and print some statistics along |
| 374 |
with the IR: |
| 375 |
|
| 376 |
.. code-block:: sh |
| 377 |
|
| 378 |
~/shader-db$ AGX_MESA_DEBUG=shaders,shaderdb ASAHI_MESA_DEBUG=precompile LD_PRELOAD=~/mesa/build/src/asahi/drm-shim/libasahi_noop_drm_shim.so ./run shaders/glmark/1-12.shader_test |
| 379 |
|
| 380 |
The drm-shim implementation for Asahi is located in ``src/asahi/drm-shim``. The |
| 381 |
drm-shim implementation there should be updated as new UABI is added. |
| 382 |
|
| 383 |
Hardware glossary |
| 384 |
----------------- |
| 385 |
|
| 386 |
AGX is a tiled renderer descended from the PowerVR architecture. Some hardware |
| 387 |
concepts used in PowerVR GPUs appear in AGX. |
| 388 |
|
| 389 |
.. glossary:: :sorted: |
| 390 |
|
| 391 |
VDM |
| 392 |
Vertex Data Master |
| 393 |
Dispatches vertex shaders. |
| 394 |
|
| 395 |
PDM |
| 396 |
Pixel Data Master |
| 397 |
Dispatches pixel shaders. |
| 398 |
|
| 399 |
CDM |
| 400 |
Compute Data Master |
| 401 |
Dispatches compute kernels. |
| 402 |
|
| 403 |
USC |
| 404 |
Unified Shader Cores |
| 405 |
A unified shader core is a small CPU that runs shader code. The core is |
| 406 |
unified because a single ISA is used for vertex, pixel and compute |
| 407 |
shaders. This differs from older GPUs where the vertex, fragment and |
| 408 |
compute have separate ISAs for shader stages. |
| 409 |
|
| 410 |
PPP |
| 411 |
Primitive Processing Pipeline |
| 412 |
The Primitive Processing Pipeline is a hardware unit that does primitive |
| 413 |
assembly. The PPP is between the :term:`VDM` and :term:`ISP`. |
| 414 |
|
| 415 |
ISP |
| 416 |
Image Synthesis Processor |
| 417 |
The Image Synthesis Processor is responsible for the rasterization stage |
| 418 |
of the rendering pipeline. |
| 419 |
|
| 420 |
PBE |
| 421 |
Pixel BackEnd |
| 422 |
Hardware unit which writes to color attachments and images. Also the |
| 423 |
name for a descriptor passed to :term:`PBE` instructions. |
| 424 |
|
| 425 |
UVS |
| 426 |
Unified Vertex Store |
| 427 |
Hardware unit which buffers the outputs of the vertex shader (varyings). |