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
impl ShaderLibrary
Sourcepub fn register_wgsl(
&mut self,
name: impl Into<String>,
src: impl Into<Cow<'static, str>>,
) -> ShaderId
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.
Sourcepub fn insert_wgsl(
&mut self,
device: &Device,
name: impl Into<String>,
src: &str,
) -> ShaderId
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.
Sourcepub fn module(&self, device: &Device, id: ShaderId) -> Option<&ShaderModule>
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.
Sourcepub fn compile_all(&self, device: &Device)
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.
Sourcepub fn get(&self, id: ShaderId) -> Option<&ShaderModule>
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.
Sourcepub fn is_compiled(&self, id: ShaderId) -> bool
pub fn is_compiled(&self, id: ShaderId) -> bool
Whether id’s module has been created (false for a foreign id).
Sourcepub fn name_of(&self, id: ShaderId) -> Option<&str>
pub fn name_of(&self, id: ShaderId) -> Option<&str>
The name id was registered under — used for pipeline labels.
Sourcepub fn compiled_len(&self) -> usize
pub fn compiled_len(&self) -> usize
How many of the registered modules have been created so far.