Is on the fly (runtime-compiled) ShaderFunction for ShaderLibrary possible?
07:28 18 Apr 2025

I need to render the output of a fragment shader in a SwiftUI-based MacOS app.

I am aware that it's possible to create a MTLLibrary from the shader(s) source text in a running application using the MTLDevice makeLibrary(source:options:) method. To render the output of shaders from it, however, I need a Metal enabled view (so probably NSViewRepresentable from a MTKView) and the entire rendering pipeline setup.

I'd much rather render using a shader function in a SwiftUI Canvas view but the latter uses ShaderLibrary, not MTLLibrary. ShaderLibrary appears to require to have been compiled in the app.

I've searched for ways to "translate" between MTLLibrary and ShaderLibrary (or between the shader function types) but have come up empty-handed. Is there a way to do what I want that I have missed/not found, or, is MTLLibrary the only known way forward?

macos swiftui metal