A .metal file is source text in the Metal Shading Language (MSL). Apple
defines the language in its Metal Shading Language Specification, a PDF of
about 300 pages (version 3.2 is dated June 2024). That PDF is larger than the tools used
for this page could read, so the language’s grammar, types, attributes and standard
library are not reproduced here; this page gives what Apple’s developer documentation and
its metal-cpp headers state, and points to the specification for the
rest.
Language versions
A program chooses the version when it compiles source at run time
(MTLLanguageVersion), and the command-line compiler takes it as
-std=metal4.0 and so on. The API value is the major version in the upper
16 bits and the minor version in the lower 16.
| Version | API value | Version | API value |
|---|---|---|---|
| 1.0 | 65536 (0x10000) | 2.3 | 131075 (0x20003) |
| 1.1 | 65537 (0x10001) | 2.4 | 131076 (0x20004) |
| 1.2 | 65538 (0x10002) | 3.0 | 196608 (0x30000) |
| 2.0 | 131072 (0x20000) | 3.1 | 196609 (0x30001) |
| 2.1 | 131073 (0x20001) | 3.2 | 196610 (0x30002) |
| 2.2 | 131074 (0x20002) | 4.0 | 262144 (0x40000) |
Apple’s documentation also lists version 4.1; its numeric value was not in the header
copy read for this page. The compiler defines __METAL_VERSION__ while it
compiles MSL, which shared C++/MSL headers test to tell the two apart.
What a file contains
MSL is based on C++, so a file is C++-style source: preprocessor directives,
declarations of types and helper functions, and entry-point functions marked with a
function-type keyword such as kernel or fragment. Pointers
and references carry an address-space keyword (see below). How arguments are bound to
the pipeline, with attributes, is defined in the specification and not reproduced
here.
Function types
The type of a top-level function, as the Metal API reports it
(MTLFunctionType):
| Type | API value | Meaning |
|---|---|---|
| vertex | 1 | A vertex function for a render pipeline. |
| fragment | 2 | A fragment function for a render pipeline. |
| kernel | 3 | A kernel for a compute pipeline. |
| visible | 5 | A function for a visible function table. |
| intersection | 6 | A function for an intersection function table. |
| mesh | 7 | A mesh function. |
| object | 8 | An object function. |
Address spaces
Address spaces named in Apple’s documentation: device,
constant, thread, threadgroup,
threadgroup_imageblock, ray_data and
object_data. In MSL 4.0 and earlier every pointer needs one of these
keywords; the implicit this of a member function without one is
thread.
MSL 4.1 adds generic pointers: a pointer without a keyword may point into
thread, threadgroup or device memory (but not
the other spaces, which still need their keyword, and not resource parameters of an
entry point). Related additions:
| Feature | Meaning |
|---|---|
static_cast<space T*>(p) | Converts a generic pointer to a concrete one without a check. |
addrspace_cast<space T*>(p) | Converts with a run-time check; gives nullptr if the pointer is in another space. |
is_thread_addrspace(), is_device_addrspace(), is_threadgroup_addrspace() | Test which space a generic pointer refers to (false for nullptr). |
MSL 4.1 also changes how function names are decorated, so binaries compiled for 4.0 and 4.1 must not be linked together.
From source to library
xcrun -sdk macosx metal -o Shadow.ir -c Shadow.metal source → intermediate file
xcrun -sdk macosx metal-ar -q Lights.metalar A.ir B.ir intermediate files → archive
xcrun -sdk macosx metal -o Lib.metallib Lights.metalar Shadow.ir → Metal library
.metal files in a project.The app loads the library at run time (makeLibrary(URL:)). The layout of
the library file is on the Metal library
page.
Scalar and vector types
From chapter 2 of the Metal Shading Language Specification 4.1 (4 June 2026). Metal
does not support double, long long,
unsigned long long or long double.
| Type (aliases) | Meaning | Size and alignment (bytes) |
|---|---|---|
bool | true (1) or false (0) | 1, 1 |
char (int8_t) | signed two’s complement 8-bit integer | 1, 1 |
unsigned char (uchar, uint8_t) | unsigned 8-bit integer | 1, 1 |
short (int16_t) | signed 16-bit integer | 2, 2 |
unsigned short (ushort, uint16_t) | unsigned 16-bit integer | 2, 2 |
int (int32_t) | signed 32-bit integer | 4, 4 |
unsigned int (uint, uint32_t) | unsigned 32-bit integer | 4, 4 |
long (int64_t), unsigned long (ulong, uint64_t) | 64-bit integers; Metal 2.2 and later, all OS | 8, 8 |
half | 16-bit floating point, IEEE 754 binary16 storage format | 2, 2 |
bfloat | 16-bit brain floating point (8-bit exponent, 7 stored mantissa bits); Metal 3.1 and later, all OS | 2, 2 |
float | 32-bit floating point, IEEE 754 single precision storage format | 4, 4 |
size_t | unsigned 64-bit result of sizeof | 8, 8 |
ptrdiff_t | signed 64-bit result of subtracting two pointers | |
void | an incomplete type with no values |
Literal suffixes: f or F single precision, h
or H half, bf or BF brain float,
u or U unsigned integer, l or L
signed long.
Vector types are bool, char, short,
int, long, uchar, ushort,
uint, ulong, half, bfloat (Metal
3.1 and later) and float followed by 2, 3 or 4 (float4), or
vec<T, n> for a scalar type T. Components are read by
index, or by .xyzw or .rgba. Size and alignment in bytes
(the same for both):
| n | bool | char, uchar | short, ushort | int, uint | long, ulong | half, bfloat | float |
|---|---|---|---|---|---|---|---|
| 2 | 2 | 2 | 4 | 8 | 16 | 4 | 8 |
| 3 | 4 | 4 | 8 | 16 | 32 | 8 | 16 |
| 4 | 4 | 4 | 8 | 16 | 32 | 8 | 16 |
Matrix, SIMD-group matrix, packed vector, atomic, pixel, texture, sampler and imageblock types are in the specification and are not covered here.
Vertex function attributes
Built-in attributes written in double brackets, from section 5.2.3 of the specification. The other function kinds (fragment, kernel, intersection, object, mesh, tile) have their own tables, not covered here.
| Attribute | Where | Types | Meaning |
|---|---|---|---|
amplification_count | vertex input | ushort or uint | Number of output vertices produced for each vertex instance; the default 1 means vertex amplification is off. macOS: Metal 2.3 and later; iOS: Metal 2.2 and later; iPadOS and visionOS: always. |
amplification_id | vertex input | ushort or uint | Array index offset for viewport and render target array indices, routing an amplified vertex; in the range [0, amplification_count). Same availability. |
base_instance | vertex and post-tessellation vertex input | ushort or uint | Base instance value added to each instance identifier before reading per-instance data. |
base_vertex | vertex input | ushort or uint | Base vertex value added to each vertex identifier before reading per-vertex data. |
instance_id | vertex and post-tessellation vertex input | ushort or uint | Per-instance identifier including the base instance value if specified. If it is declared uint, base_instance must be uint or ushort; if ushort, base_instance must be ushort. |
vertex_id | vertex input | ushort or uint | Per-vertex identifier including the base vertex value; the same typing rule against base_vertex. |
patch_id | post-tessellation vertex input | ushort or uint | The patch identifier. |
position_in_patch | post-tessellation vertex input | float2 or float3 | Location on the patch being evaluated: float2 for quad patches, float3 for triangle patches. (All attributes of table 5.3 need Metal 1.2 or later on every OS.) |
clip_distance | vertex output | float or float[n], n known at compile time | Distance from the vertex to the clipping plane. |
invariant | vertex output, with position | not applicable | Marks the output position so identical operation sequences in several vertex shaders give the same value; needs -fpreserve-invariance. Metal 2.1 and later. |
point_size | vertex output | float | Size of a point primitive. |
position | vertex output | float4 | The transformed vertex position. A float4 return type is the position and takes no attribute; a structure return must have a member with it. |
render_target_array_index | vertex output | uchar, ushort or uint | Array slice of a texture array, depth of a 3D texture, cubemap face, or face of a slice of a cubemap array (slice × 6 + face). macOS: Metal 1.1 and later; iOS: Metal 2.1 and later; iPadOS and visionOS: always. Must be the same for every vertex in a primitive; out-of-range values read as 0. |
shared | vertex output | not applicable | The output has the same value for every amplification_id. macOS: Metal 2.3 and later; iOS: Metal 2.2 and later; iPadOS and visionOS: always. |
viewport_array_index | vertex output | uchar, ushort or uint | Viewport and scissor rectangle index of the primitive; 0 if not given. macOS: Metal 2 and later; iOS: Metal 2.1 and later; iPadOS and visionOS: always. |
Sources
- Metal Shading Language Specification, version 4.1, 4 June 2026, 383 pages – Apple; the defining document. The PDF (14.5 MB) was fetched with curl on 13 November 2026 and its text read with PDFKit; sections 2.1, 2.2 and 5.2.3.1 to 5.2.3.3 are used on this page and the rest was not read
- MTLLanguageVersion and MTLFunctionType – Apple developer documentation
- Writing reusable GPU functions with generic pointers – Apple: address spaces, generic pointers, casts and the MSL 4.1 ABI change
- Building a shader library by precompiling source files – Apple: the compile steps
- MTLLibrary.hpp – Apple’s metal-cpp header (a copy published with Babylon Native), for the numeric values
Not covered (in the specification only): MSL’s full grammar, matrix and packed types, the attributes of the other function kinds, textures and samplers, and the standard library.