Skip to main content

ShaderLibrary

Struct ShaderLibrary 

Source
pub struct ShaderLibrary(/* private fields */);
Expand description

A compiled Metal library.

Implementations§

Source§

impl ShaderLibrary

Source

pub fn from_path(path: &str) -> Self

Load a .metallib from disk.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.