Using Metal in Swift Playgrounds 4 | by E Skils | Apr, 2022

Two workarounds for making Metallic sing like a fowl in Swift Playgrounds

Unwell: Apple Inc.

Swift Playgrounds 4 was an enormous replace, bringing alongside full app growth in SwiftUI to the iPad in addition to making the app sooner and higher to make use of. Though Playgrounds works nicely for prototyping and dealing on the go, it’s lacking some superior growth options—like Metallic.

This text will present two methods of constructing Metallic work in Swift Playgrounds 4 each for app tasks and playgrounds.

One possibility is simply to compile the Metallic supply in Xcode and duplicate the compiled library to Playgrounds.

This preserves code-completion and syntax-highlighting whereas engaged on the Metallic supply, and is quicker to execute. This technique can nonetheless be fairly cumbersome and solely works on one platform (the identical compiled library is not going to work on each iOS and macOS).

The process is as follows:

  1. Open a brand new iOS venture in Xcode
  2. Create a brand new .steel file and write your shaders
  3. Construct the venture to your platform
  4. Press Product > Present Construct Folder in Finder, and navigate to Merchandise/Debug-[platform]. As an illustration, constructing for Mac Catalyst yields the trail Merchandise/Debug-maccatalyst.
  5. Proper-click in your app and select Present Bundle Contents , then copy the filedefault.metallib (could be positioned within the Assets folder)
  6. Then in Swift Playgrounds. When you’ve got an app: paste the file within the Assets folder of your app (or drag the file into the playgrounds-app). When you’ve got a playground: paste the file in Edits/UserResources of your playground (or select the file with a file literal)
  7. Write code to execute your steel operate as you’ll in a standard app, utilizing system.makeDefaultLibrary()!
Animated image showing how to navigate to the compiled metal library.
Compiling the Metallic-source in Xcode and copying the compiled library. Positioned at <build-folder>/Merchandise/Debug-<Platform>.

Strengths

  • Protect code-completion and syntax-highlighting
  • Executes sooner (if you actually need to optimize)

Limitations

  • Cumbersome
  • Solely works on the platform it was compiled for.

One other technique is to retailer the Metallic-source in plaintext as a useful resource and compile on-device.

The profit right here is you could edit the shader in Playgrounds, and the playground will work cross-platform. And should you open the shader in Xcode (or a unique editor with the language set to c++), you’ll get syntax highlighting.

The strategy goes like so:

  1. Create a brand new textual content file and write your shaders. Word, it’s possible you’ll finish the file with.steel, nonetheless this is not going to work in a playgrounds app — the app will fail compilation.
  2. When you’ve got an app: paste the file within the Assets folder of your app (or drag the file into the playgrounds-app). When you’ve got a playground: paste the file in Edits/UserResources of your playground (or select the file with a file-literal)
  3. Write code to execute your Metallic operate as you’ll in a standard app, utilizing strive system.makeLibrary(supply: String, choices: MTLCompileOptions?)

Strengths

  • Supply may be edited in Swift Playgrounds
  • Works on any platform

Limitations

  • No syntax-highlighting nor code-completion
  • Barely slower to execute

Compiling the library in Xcode will result in barely sooner execution. I did a check to check the time it takes to load a steel library in Swift Playgrounds utilizing the 2 strategies above. The check was achieved in Swift Playgrounds 4.0 on an M1 Mac utilizing a Metallic shader counting 11 strains of code. The outcomes are within the magnitude of microseconds (i.e. lower than milliseconds), however reveals that compiling in Xcode will load the library sooner.

Compiled: 641 μs. Source: 981 μs.
Comparability of time for playground to load a Metallic library on an M1 Mac utilizing the 2 strategies.

These are two strategies to make Metallic work in Swift Playgrounds. Compiling the Metallic-library in Xcode is probably most popular if one already has a completed steel library, whereas storing the Metallic-source as a useful resource is most popular if one must edit the supply throughout growth within the playground.

More Posts