Skip to main content

ShaderLibrary

Struct ShaderLibrary 

Source
pub struct ShaderLibrary { /* private fields */ }
Expand description

A name-keyed, append-only set of WGSL modules, each created on first use.

Build it at start-up, then share it (Arc<ShaderLibrary>) with the pipeline cache and its warm-up worker. Registration is idempotent per name: re-registering a name that is already present returns the existing id and keeps the first source, so a caller that cannot easily prove it only registers once still pays for one module.

Implementations§

Source§

impl ShaderLibrary

Source

pub fn new() -> Self

An empty library.

Source

pub fn register_wgsl( &mut self, name: impl Into<String>, src: impl Into<Cow<'static, str>>, ) -> ShaderId

Registers src as WGSL under name and returns its id, without creating its module: that happens on the first Self::module request for the id, on whichever thread makes it.

If name is already registered the existing id is returned and src is dropped — the library is append-only, so a name never changes meaning once it is in.

Source

pub fn insert_wgsl( &mut self, device: &Device, name: impl Into<String>, src: &str, ) -> ShaderId

Registers src under name (Self::register_wgsl) and creates its module on the calling thread at once — the eager form, for a caller that wants the module to exist before any pipeline asks for it.

The module is labelled with name. If name is already registered the existing id is returned, src is not used, and the registered source’s module is created if it does not exist yet.

Compilation errors are not raised here: wgpu reports a malformed module through the device’s error scope / uncaptured-error handler, and the resulting pipeline creation fails there. This mirrors frust-render’s shader-effect path, which records the failure rather than panicking.

Source

pub fn module(&self, device: &Device, id: ShaderId) -> Option<&ShaderModule>

The module id addresses, creating it now if no thread has yet — or waiting for the thread creating it right now — or None if id was minted by a different library.

Each module is created at most once however many threads ask at the same time. Errors surface as for Self::insert_wgsl, on the thread that creates the module.

Source

pub fn compile_all(&self, device: &Device)

Creates every registered module that does not exist yet, on the calling thread — the eager start-up behaviour, for tests and for a caller that wants every module in place before its first frame.

Source

pub fn get(&self, id: ShaderId) -> Option<&ShaderModule>

The module id addresses if it has already been created, or None — either because no thread has asked for it yet (see Self::module), or because id was minted by a different library.

Source

pub fn is_compiled(&self, id: ShaderId) -> bool

Whether id’s module has been created (false for a foreign id).

Source

pub fn id_of(&self, name: &str) -> Option<ShaderId>

The id registered under name, if any.

Source

pub fn name_of(&self, id: ShaderId) -> Option<&str>

The name id was registered under — used for pipeline labels.

Source

pub fn len(&self) -> usize

How many distinct modules are registered, created or not.

Source

pub fn compiled_len(&self) -> usize

How many of the registered modules have been created so far.

Source

pub fn is_empty(&self) -> bool

Whether no module is registered yet.

Trait Implementations§

Source§

impl Debug for ShaderLibrary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ShaderLibrary

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,