Metal implementation of differentiable rasterization primitives for Apple Silicon.
Based on Modular Primitives for High-Performance Differentiable Rendering (Laine et al. 2020).
- Rasterize: Triangle rasterization with differentiable barycentrics
- Interpolate: Vertex attribute interpolation with screen-space derivatives
- Texture: Texture sampling with mipmapping (nearest, linear, trilinear)
- Antialias: Differentiable antialiasing for silhouette edges
- Zero-copy: MPS and CPU tensors bind directly to Metal via unified memory
All operations support torch.autograd for end-to-end differentiable rendering.
API-compatible with nvdiffrast for drop-in use in existing pipelines.
Requires macOS 14+ on Apple Silicon, Xcode command line tools, and PyTorch >= 2.0.
pip install --no-build-isolation -e .
python setup.py build_ext --inplaceimport mtldiffrast.torch as dr
ctx = dr.MtlRasterizeContext()
rast, rast_db = dr.rasterize(ctx, pos, tri, resolution=[512, 512])
out, _ = dr.interpolate(attr, rast, tri)
tex_out = dr.texture(tex, uv)
aa_out = dr.antialias(color, rast, pos, tri)MIT License
Cross-repo follow-up work (deferred mtlgemm perf items, fp16/bf16 shader
specializations, universal tiled sparse attention, etc.) is tracked in this
repo's GitHub issues. Bug reports and PRs are welcome on any of the four
mtl* repos — mtlbvh, mtlgemm, mtlmesh and mtldiffrast — and on
trellis2-apple, which depends on all of them.