Issue #919

WWDC23 introduces lots of new additions to SwiftUI, notably Metal shader support with these modifiers

  • colorEffect: Returns a new view that applies shader to self as a filter effect on the color of each pixel.
  • layerEffect: Returns a new view that applies shader to self as a filter on the raster layer created from self.
  • distortionEffect: Returns a new view that applies shader to self as a geometric distortion effect on the location of each pixel.

So you can create effects like these

Before we could also use Metal with SwiftUI by wrapping MTKView in UIViewRepresentable or SpriteView with shader, so learning a bit about Metal helps a long way

Here are the resources I’ve found to be very useful

ShaderToy

Shader Toy is a shader community where people share very creative and interesting shader effects in OpenGL. OpenGL and Metal language are somewhat similar so you can read and understand the logic

Screenshot 2023-06-16 at 10 37 03

Metal by example

Great book by Warren Moore, who worked on camera and photo apps at Apple. As of March 2023 you can download the book for free on this site.

Warren also has a very nice 30 days of Metal tutorials that we can follow along the basics like device, buffer and texture.

Shader Playground

A playground for shading languages.

Screenshot 2023-06-16 at 10 40 15

You can compile HLSL and GLSL using fxc.exe, dxc.exe, glslangValidator.exe, spirv-cross.exe, and mali-sc.exe. You can see the compiler output and disassembly. You can chain multiple shader compilers together to transpile one language to another. You can share permalinks to shaders

The book of shaders

Gentle step-by-step guide through the abstract and complex universe of Fragment Shaders, by Patricio Gonzalez Vivo

Screenshot 2023-06-16 at 10 40 49

Accelerate graphics and much more with Metal

Resource and links from Apple how to best use Metal in apps and games.

Screenshot 2023-06-16 at 10 43 22

There are videos about Metal at WWDC Be sure to check out Metal documentation for code examples and tutorial links.

Screenshot 2023-06-16 at 10 50 34

Bringing OpenGL Apps to Metal

Screenshot 2023-06-16 at 11 03 21

Get introduced to the architecture and feature set of Metal and learn a step-by-step approach for transitioning OpenGL-based apps to the Metal API

Metal shading language specification

Whether you’re new to Metal or trying to port code from OpenGL, understanding the Metal language is essential. In this spec you’ll learn about primitive types like float, float4, float4x4

In Metal you can access vector component in a very convenient way.

Screenshot 2023-06-16 at 10 46 17

Metal with SwiftUI

A Youtube playlist on how to use Metal from easy to advanced features.

Screenshot 2023-06-16 at 10 51 52

Introduction to Metal Compute

Great tutorials by Eugene on how to use Metal compute and kernel shader to apply cool effects to texture

Screenshot 2023-06-16 at 10 52 08