Introduction
A .metal file holds shaders written in the Metal Shading Language, the
language of Apple’s Metal graphics and
compute API on macOS, iOS and Apple’s other systems. The language is based on C++, with
vector and matrix types and attributes that tie functions and their arguments to the
pipeline.
Xcode compiles the .metal files in a project into a Metal library,
default.metallib, which is shipped inside the app and loaded at run time.
Source can also be compiled while the app runs.
Structure
A Metal file is plain text in C++ syntax. It usually includes
<metal_stdlib> and uses the metal namespace. Each shader
is a function marked vertex, fragment or kernel
(for compute). Attributes in double square brackets say where values come from, such as
[[buffer(0)]], [[stage_in]] or [[position]].
#include <metal_stdlib>
using namespace metal;
struct VertexOut {
float4 position [[position]];
float4 colour;
};
fragment float4 fragmentMain(VertexOut in [[stage_in]]) {
return in.colour;
}
Links
Software
- Xcode, which compiles and debugs Metal shaders (macOS)
- SPIRV-Cross, which translates SPIR-V shaders into Metal (Windows, macOS, Linux)
Reference
- Metal resources, Apple, with the Metal Shading Language Specification