Does Metal work in Swift playgrounds?

I posted a question in the playgrounds section of the forums regarding an issue with Metal in Swift playgrounds: Metal playground not setting up MTLLibrary with makeDefaultLibrary(), but I figured someone might better know on a general level whether Metal even works in playgrounds or not. I'm not sure if I'm putting my shader file in the correct location in the playground - I tried putting it in both the sources and resources folders of the playground.

Answered by jackxujh in 353855022

I believe these playgrounds created by others use Metal. I do not know if they work for iPad, but they certainly compile and run in Xcode:



There should be some other blogs that have a focus on Metal tutorials using Playground.

Are you planning to run this playground on Mac or iPad? I'm not sure about iPad, but playgrounds designed for Mac supports Metal.

I've tried on both, but it keeps giving me issues with finding the shader file. On the Mac (where I can see more debug info), the MTLLibrary instance is nil when trying to set up the default library. From what I can see on the iPad, something similar is happening.

Accepted Answer

I believe these playgrounds created by others use Metal. I do not know if they work for iPad, but they certainly compile and run in Xcode:



There should be some other blogs that have a focus on Metal tutorials using Playground.

Thanks for sending those links - that at least gets it so the MTLLibrary loads, even though I'm not seeing anything rendering (but it also could be that the MTKView isn't in a viewable area as I'm having some UI layout issues too)\


Update: Still not getting anything rendering even though I can with identical rendering code (other than the MTLLibrary stuff) in an actual app running on my iPad. It wouldn't seem like there'd be anything that'd need to be changed in the rendering code from an actual app besides the MTLLibrary stuff since that does depend on file locations, etc.

Yeah, it surely is an important issue to look into deeper.

No, there is something wrong about loading shader from bundle in playground. need someone's help

Though somewhat late with the answer, here is the link to the Swift Playgrounds book that I made recently:

https://github.com/gadirom/Metal-Compute-Offscreen-Render-and-Postprocess

It will run on an iPad or a Mac.

Instead of loading shaders from a file I load them from a String constant. The downside is that you won't have syntax highlights.

Anyway, to edit and run the Metal Shader code and research different pipeline logic right on the device is a nice experience! :)

Does Metal work in Swift playgrounds?
 
 
Q