Metal not working in Swift Playgrounds (SSC Scene)

Hi everyone,

I'm currently working on a Swift Playgrounds project where I need to incorporate a Metal shader file. However, when I tried to include my shader file (PincushionShader.metal), I encountered the following error:

Is it possible to use Metal shader files within Swift Playgrounds, it is really important for my swift student challenge scene? If not, are there any workarounds or recommended approaches for testing Metal shaders in a similar environment?

Any guidance or suggestions would be greatly appreciated!

Hi Luis,

Swift Playground doesn’t currently support building files with a .metal extension, but you can work around this restriction by using a different extension (such as .txt) and then providing a path to your file to MTLDevice’s makeLibrary(URL:) method to compile your shaders at runtime.

If you feel that having the ability to build Metal shaders at playground compilation time would be valuable, please don’t hesitate to send us a Feedback Assistant request.

Good luck with your challenge project!

@Graphics and Games Engineer Hi! I'm in a similar spot; I have my own small CIKernel written in Metal.

In order to add it to Playgrounds I compiled it myself and added the resulted.metallib directly to the resources folder, quite simlar to what I described on this thread.

Your answer got me wondering, do you think this approach is fine as well, or should I rather try to compile this at runtime?

If the latter is preferred, is it possible to provide the required CLI arguments (in my case, -fcikernel and / or -cikernel)?

I really need to use Metal in my project because my scene is based on a simulation. I used SCNTechnique in SceneKit to create a distortion effect that enhances the immersion of the scene. I implemented it using this code:

func applyMetalShadingAndTechniqueOnScene() {
    if let path = Bundle.main.path(forResource: "NodeTechnique", ofType: "plist"),
       let techniqueDict = NSDictionary(contentsOfFile: path) as? [String: AnyObject],
       let technique = SCNTechnique(dictionary: techniqueDict) {
        sceneView.technique = technique
    }
}

I even compiled a .metallib file using the following commands:

xcrun -sdk iphoneos metal -o PincushionShader.ir -c PincushionShader.metal

xcrun -sdk iphoneos metallib PincushionShader.ir -o PincushionShader.metallib

However, when I try to use ShaderLibrary to load the .metallib file, the SceneKit view just turns black when the shader is applied.

I would really appreciate any insights or solutions to this issue.

Metal not working in Swift Playgrounds (SSC Scene)
 
 
Q