Skip to main content

RuntimeShader

Struct RuntimeShader 

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

A custom WGSL shader effect, analogous to Android’s RuntimeShader.

The shader source must be a complete WGSL module that declares:

@group(0) @binding(0) var input_texture: texture_2d<f32>;
@group(0) @binding(1) var input_sampler: sampler;
@group(1) @binding(0) var<uniform> u: array<vec4<f32>, 64>;

Float uniforms are packed linearly into the u array. Access them in WGSL as u[index / 4][index % 4] for individual floats, or u[index / 4].xy for vec2, etc. User uniforms may use indices 0..224; slots 224..256 are reserved for renderer metadata:

slotscontent
224..236substrate regions (x, y, w, h) in input texels, the third at 224, the second at 228, the first at 232; zero = none
236..240source region (x, y, w, h) in input texels; zero = whole
240..244composite mask rect (x, y, w, h) in region pixels; zero = none
244..248composite mask corner radii (top-left, top-right, bottom-left, bottom-right)
248..252effect rect (x, y, w, h) in region pixels
252..254logical size the input represents; zero = its texel size
254composite alpha

A shader that reads the source region, mask and alpha slots declares it with set_batched_source; one that reads a low-frequency copy of its source declares each with set_substrates and samples it through its substrate region, held to that region’s texel centers, so one tap stands for a neighbourhood the shader would otherwise walk tap by tap. The renderer then packs its input edge to edge beside other effects’ inputs in one texture and draws it straight into the final pass with its clip applied. Such a shader holds every sample coordinate to its region’s texel centers: the texels beside the region belong to other effects, or to no one. Every other shader is given the whole texture as its input and uv spans it.

RuntimeShader pipelines operate on premultiplied-alpha textures. Custom shaders should preserve premultiplied output semantics.

Implementations§

Source§

impl RuntimeShader

Source

pub const MAX_UNIFORMS: usize = 256

Total uniform storage size in floats (64 vec4s = 256 floats).

The final slots are reserved for renderer-managed data.

Source

pub const RESERVED_UNIFORM_START: usize = 224

First renderer-reserved uniform slot.

Source

pub const SUBSTRATE_REGION_UNIFORMS: [usize; 3]

Reserved slots of the substrate regions (x, y, w, h) in input texels, in declaration order.

Source

pub const SOURCE_REGION_UNIFORM: usize = 236

Reserved slot of the source region (x, y, w, h) in input texels.

Source

pub const MASK_RECT_UNIFORM: usize = 240

Reserved slot of the composite mask rect (x, y, w, h) in region pixels.

Source

pub const MASK_RADII_UNIFORM: usize = 244

Reserved slot of the composite mask corner radii.

Source

pub const EFFECT_RECT_UNIFORM: usize = 248

Reserved slot of the effect rect (x, y, w, h) in region pixels.

Source

pub const LOGICAL_SIZE_UNIFORM: usize = 252

Reserved slot of the logical size the input represents.

Source

pub const ALPHA_UNIFORM: usize = 254

Reserved slot of the composite alpha.

Source

pub const MAX_USER_UNIFORMS: usize = Self::RESERVED_UNIFORM_START

Maximum user-addressable uniform count.

Source

pub fn new(wgsl_source: &str) -> RuntimeShader

Create a new RuntimeShader from WGSL source code.

Source

pub fn from_shared_source(source: Arc<str>) -> RuntimeShader

Create a RuntimeShader from shared WGSL source code.

This avoids repeatedly copying large shader modules for animated effects that rebuild only their uniform payload every frame.

Source

pub fn set_override(&mut self, name: &'static str, value: f64)

Fixes a pipeline-overridable constant (override NAME: T = ...; in the WGSL) for every pipeline compiled from this shader. The value is converted to the constant’s declared scalar type the way WebGPU does (a bool is value != 0). Each distinct override set compiles its own pipeline; renderers use this to fold a material’s inactive features away without changing the shader text.

Source

pub fn clear_override(&mut self, name: &str) -> bool

Removes a pipeline override by name, returning whether one was present.

Source

pub fn overrides(&self) -> &[(&'static str, f64)]

The pipeline-overridable constants fixed by Self::set_override, ordered by name.

Source

pub fn overrides_hash(&self) -> u64

Hash of the fixed override set; zero when no override is fixed.

Source

pub fn set_input_padding(&mut self, padding: f32)

Declares how far the shader may sample outside its effect rect, in logical pixels. Backdrop rendering uses this to capture enough input around refractive and displacement shaders.

Source

pub fn input_padding(&self) -> f32

Returns the declared input padding in logical pixels.

Source

pub fn set_output_padding(&mut self, padding: f32)

Declares how far the shader WRITES outside its effect rect, in logical pixels. Backdrop compositing widens its scissor by this amount so SDF-driven coverage (rim glow, wobble, glued neighbor shapes) can extend past the node bounds instead of being clipped to them.

Source

pub fn output_padding(&self) -> f32

Returns the declared output padding in logical pixels.

Source

pub fn set_output_support(&mut self, support: Option<Rect>)

Declares the rect outside which the shader writes nothing: every pixel its coverage can make nonzero at its current uniforms, the output padding’s reach included, in logical pixels with the origin at the effect rect’s top-left. A renderer composites only the part of the effect rect inside it; the capture it reads stays whole, so a node that carries headroom around a smaller material pays the composite for the material alone. It says nothing about sampling: see Self::set_sample_domain. None, the default, means the whole effect rect and its output padding. A rect with a non-finite side clears the declaration.

Source

pub fn output_support(&self) -> Option<Rect>

The declared output support, when the shader gave one.

Source

pub fn set_sample_domain(&mut self, domain: Option<Rect>)

Declares the rect outside which the shader never samples its input, in logical pixels with the origin at the effect rect’s top-left. A renderer may leave the input outside it unresolved: a blur feeding this shader need only write the domain. The default, None, is the whole effect rect and its input padding, which the input padding contract already promises; an output support says nothing about sampling, so a shader that shades a small region but reads a far one keeps the default. A rect with a non-finite side clears it.

Source

pub fn sample_domain(&self) -> Option<Rect>

The declared sample domain, when the shader gave one.

Source

pub fn set_float(&mut self, index: usize, value: f32)

Set a single float uniform at the given index.

Invalid renderer-reserved ranges are ignored. Use Self::try_set_float when the caller needs to handle invalid uniform writes explicitly.

Source

pub fn try_set_float( &mut self, index: usize, value: f32, ) -> Result<(), RuntimeShaderUniformError>

Set a single float uniform at the given index.

Source

pub fn set_float2(&mut self, index: usize, x: f32, y: f32)

Set a vec2 uniform at the given index (consumes indices [index, index+1]).

Invalid renderer-reserved ranges are ignored. Use Self::try_set_float2 when the caller needs to handle invalid uniform writes explicitly.

Source

pub fn try_set_float2( &mut self, index: usize, x: f32, y: f32, ) -> Result<(), RuntimeShaderUniformError>

Set a vec2 uniform at the given index (consumes indices [index, index+1]).

Source

pub fn set_float4(&mut self, index: usize, x: f32, y: f32, z: f32, w: f32)

Set a vec4 uniform at the given index (consumes indices [index..index+4]).

Invalid renderer-reserved ranges are ignored. Use Self::try_set_float4 when the caller needs to handle invalid uniform writes explicitly.

Source

pub fn try_set_float4( &mut self, index: usize, x: f32, y: f32, z: f32, w: f32, ) -> Result<(), RuntimeShaderUniformError>

Set a vec4 uniform at the given index (consumes indices [index..index+4]).

Source

pub fn set_batched_source(&mut self, batched: bool)

Declares that the shader reads the reserved source region, mask and alpha slots and samples only within its region’s texel centers, so the renderer may hand it an input region packed edge to edge beside others and draw it straight into the final pass with its clip applied.

Source

pub fn batched_source(&self) -> bool

Whether the shader reads the reserved source region, mask and alpha slots.

Source

pub fn set_substrates(&mut self, substrates: &[SubstrateSpec])

Declares the low-frequency copies of its source the shader reads through the reserved substrate region slots, in slot order. Only a batched shader packed with its stage is handed them; a shader without finds the slots zero and samples the source itself.

§Panics

When more than MAX_SUBSTRATES are declared.

Source

pub fn substrates(&self) -> &[SubstrateSpec]

The substrates the shader declared, in slot order.

Source

pub fn hash_substrates<H>(&self, state: &mut H)
where H: Hasher,

Hashes the declared substrates and the draw split into state.

Source

pub fn set_draw_split(&mut self, override_name: Option<&'static str>)

Declares an override NAME: i32 the renderer sets to 1 and 2 to draw the shader twice in the final pass, once for its interior and once for its rim, each pipeline compiled without the other’s work and discarding the other’s fragments before its fetches. Nothing else about the draw changes: the two draws partition the pixels the one draw shaded and land on the same bits.

Source

pub fn draw_split(&self) -> Option<&'static str>

The override selecting the interior or the rim draw, when declared.

Source

pub fn source(&self) -> &str

Get the WGSL source code.

Source

pub fn uniforms(&self) -> &[f32]

Get the uniform data as a float slice (for uploading to GPU).

Source

pub fn uniforms_padded(&self) -> [f32; 256]

Get the uniform data padded to full 256-float array (for GPU uniform buffer).

Source

pub fn source_hash(&self) -> u64

Compute a hash of the shader source for pipeline caching.

Trait Implementations§

Source§

impl Clone for RuntimeShader

Source§

fn clone(&self) -> RuntimeShader

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RuntimeShader

Source§

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

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

impl PartialEq for RuntimeShader

Source§

fn eq(&self, other: &RuntimeShader) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl RenderHash for RuntimeShader

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.