pub struct ShaderLibrary(/* private fields */);Expand description
A compiled Metal library.
Implementations§
Source§impl ShaderLibrary
impl ShaderLibrary
Sourcepub fn from_source(source: &str) -> Result<Self, String>
pub fn from_source(source: &str) -> Result<Self, String>
Compile Metal Shading Language source at run time.
No .metallib, no Xcode, and it works on a device — which is what
compile cannot do, since the offline Metal compiler ships with Xcode
alone. Metal itself has always been able to compile at run time; what was
missing was a way to hand the result to SwiftUI, whose ShaderLibrary
takes only an already-compiled library’s bytes.
There is one, as it turns out. A ShaderLibrary is a single word
wrapping an RBShaderLibrary — the RenderBox object SwiftUI actually
renders through — and that class accepts an MTLLibrary directly. So
the source goes through MTLDevice, which reports errors properly, and
the resulting library is wrapped.
RBShaderLibrary is internal to SwiftUI: it is not API, and a future
release may rename it or change its shape. from_data
and from_path go through SwiftUI’s public
initialisers and will not.
§Errors
The compiler’s own diagnostics, or a note that the runtime pieces are missing. Worth propagating: a library built from source that does not compile still renders, as a shader that silently does nothing.
Sourcepub fn from_data(bytes: &[u8]) -> Self
pub fn from_data(bytes: &[u8]) -> Self
Load a .metallib from bytes already in memory.
A .metallib has to be built ahead of time — see compile_for — but
it does not have to be a file at run time. include_bytes! it into the
binary and this needs no bundle resource and no path, which on iOS is
the difference between shipping a shader and shipping an app layout.
Data is bridged from an NSData rather than built directly:
Data.init(bytes:count:) is inlinable and so has no symbol to call,
while init(referencing:) is a real entry point.
Sourcepub fn function(&self, name: &str) -> ShaderFunction
pub fn function(&self, name: &str) -> ShaderFunction
Look up a [[ stitchable ]] function by name.
In Swift this is library.myFunction, spelled through
@dynamicMemberLookup; the underlying subscript takes the name as an
ordinary string.