pub struct Source {
pub id: u64,
pub name: &'static str,
pub imports: &'static [&'static Source],
pub ir_constructor: fn() -> Module,
pub templates: &'static [GenericTemplate],
pub instantiations: &'static [TemplateInstantiation],
pub module_type_params: &'static [&'static str],
}Expand description
A WGSL source.
A Source is the public-facing spec for a #[wgsl] mod foo { ... }
block: the macro emits one pub static foo::WGSL_SOURCE: Source per
module. WGSL itself doesn’t support importing modules, but wgsl-rs
does (glob imports only), so a Source can import other Sources.
Concrete and template sources share the same type. Templates are
detected at runtime via Source::is_template; a template source’s
Source::wgsl_source returns
SourceError::TemplateWgsl and the caller must use the
macro-emitted instantiate::<…>() to obtain a concrete
ir::Module first.
#[wgsl]
pub mod constants {
pub const NUMBER: u32 = 1234;
}
#[wgsl]
pub mod main {
use super::constants::*;
fn add_to_number(n: u32) -> u32 {
n + NUMBER
}
}Fields§
§id: u64Unique identifier for this source, assigned at proc-macro expansion time from the consuming compilation unit’s identity (Cargo package name + version, plus crate/bin target names) and a per-unit counter (wgsl-rs#165). Exactly unique within one compilation unit; across units separated by a 64-bit hash, so collisions are astronomically unlikely but not impossible. Used to deduplicate sources during source assembly (diamond import graphs, same-named sources from different paths).
name: &'static strName of the source.
imports: &'static [&'static Source]Imports of other WGSL sources.
ir_constructor: fn() -> ModuleConstructor function that builds this module’s IR. Called lazily by
wgsl_source() / instantiate().
The returned IR is owned and freshly constructed on each call; the caller is free to clone, mutate, or substitute over it.
templates: &'static [GenericTemplate]Generic function templates defined in this module.
Consuming modules reference these via instantiations to produce
monomorphized functions at source-assembly time. The IR is held as
a constructor, just like the parent module’s IR.
instantiations: &'static [TemplateInstantiation]Cross-module template instantiations.
Each entry references a template from an imported module and provides
the concrete IR type arguments to substitute for the type parameters.
These are resolved at source-assembly time in wgsl_source().
module_type_params: &'static [&'static str]Module-level type parameters, in instantiation order.
Two distinct kinds of declaration contribute to this list:
impl Traitlinkage variables, e.g.uniform!(group(0), binding(0), FRAME: impl Convert<f32>). Each such declaration contributes the variable’s identifier ("FRAME").- Generic shader entry points, e.g.
pub fn frag_main<T: Convert<f32>>() -> Vec4f. Each one of an entry point’s type parameters contributes a positional name of the form"<fn_name>_<index>"(e.g."frag_main_0"). Source names likeTare intentionally not used so that two entry points sharing a letter don’t collide.
The two groups appear in declaration order: linkage variables first, then entry points.
When this slice is empty, the module is concrete and
Self::wgsl_source produces a directly usable WGSL source.
When it is non-empty, callers should use the instantiate
function generated alongside the source, which uses
wgsl_rs::linkage::Type<Is = ...> constraints to check at
compile time that every parameter has been bound.
Implementations§
Source§impl Source
impl Source
Sourcepub fn is_template(&self) -> bool
pub fn is_template(&self) -> bool
Returns whether this source is a template — i.e. has unresolved module-level type parameters.
Template sources cannot be used as shader sources directly; call
instantiate(...) to produce a concrete WGSL source.
Sourcepub fn wgsl_source(&self) -> Result<String, SourceError<'_>>
pub fn wgsl_source(&self) -> Result<String, SourceError<'_>>
Returns the assembled WGSL source for this source and all its imports, including cross-source template instantiations.
If this source is a template, the returned error is
SourceError::TemplateWgsl; the caller must use the
macro-emitted instantiate::<…>() to obtain a concrete
ir::Module first and then call ir::Module::wgsl_source.
Source§impl Source
impl Source
Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
Validates the concatenated WGSL source of this source and its imports.
This method concatenates the WGSL source from this source and all its imports (recursively), then validates the result using naga.
§Returns
Returns Ok(()) if validation succeeds, or an error message
describing the validation failure. Template sources return an
Err(String) instructing the caller to instantiate::<…>()
first (they cannot be validated standalone — their IR contains
__TP{name}__ placeholders that aren’t valid WGSL).
§Example
#[wgsl]
pub mod my_shader {
// ...
}
// Validate at runtime
my_shader::WGSL_SOURCE.validate().expect("WGSL validation failed");Trait Implementations§
Auto Trait Implementations§
impl Freeze for Source
impl RefUnwindSafe for Source
impl Send for Source
impl Sync for Source
impl Unpin for Source
impl UnsafeUnpin for Source
impl UnwindSafe for Source
Blanket Implementations§
impl<T> AnySendSync for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more