Documentation / Reference

shader

Display a bound WGSL program in one portable layout and event target.

Construction

Use fn shader(program: Shader) -> ShaderView. Import the API from refract::prelude::*. Parse WGSL once with Shader::wgsl, bind uniforms and an optional image, then clone the shared program into the view. The Shaders guide supplies the wave.wgsl source used here.

shader.rs
use refract::prelude::*;
use std::sync::LazyLock;

static WAVE: LazyLock<Shader> = LazyLock::new(|| {
    Shader::wgsl(include_str!("wave.wgsl"))
        .expect("valid embedded shader")
        .fallback(Color::rgb(25, 35, 65))
});

fn wave() -> impl View {
    shader(WAVE.clone()
        .uniform("intensity", 0.8_f32)
        .uniform("tint", Color::BLUE))
        .schedule(ShaderSchedule::Animation)
        .width(320.0)
        .height(180.0)
}

Program and bindings

Shader::wgsl(source) returns Result<Shader, ViewError> and validates the WGSL program. Define Uniforms and shade(frame: ShaderFrame) -> vec4<f32>; Refract supplies resource bindings and entry points. Shader clones share program storage.

uniform(name, value) accepts f32, [f32; 2], [f32; 3], [f32; 4], or Color matching the WGSL member. Missing bindings are zero. Names, dimensions, and finite values are validated before host mutation; Shader::validate() checks them explicitly. The portable uniform-storage limit is 16 KiB.

image(ShaderImage) attaches one immutable RGBA8 image for sample_image(uv). fallback(Color) sets the browser and prerender fallback. These configure a Shader, not ShaderView; bind them before calling shader. See uniforms and image input.

Rendering and lifetime

Desktop composites the wgpu shader texture through Vello; browsers use WebGPU and require HTTPS or localhost. The fallback color appears during browser setup, in prerendered HTML, or after GPU failure. No WebGL fallback is provided.

Pipelines and textures are retained across updates and released when the view is removed or its mount is disposed. Browser exit transitions use a static bitmap. Use shader_with for separately tracked preparation instead of reading graph state inside a GPU frame.

View-specific reference

These methods preserve ShaderView. Configure them before wrappers consume the concrete builder.

MethodPurpose
scheduleChooses OnChange (the default), Animation, or Paused. OnChange renders at time zero when inputs or dimensions change. Animation advances visible time and requests frames; Paused retains the clock phase while still accepting input and size updates. ShaderSchedule aliases CanvasSchedule and follows its visibility and reduced-motion policy.
animatable_inputsOpts bound scalar and vector uniforms into animation transactions. Use with_animation or a value-scoped animation wrapper to supply animation intent. Keep bound names and vector dimensions stable. Images and fallback colors switch immediately. Uniform transitions can continue while the shader clock is paused.

schedule

Chooses OnChange (the default), Animation, or Paused. OnChange renders at time zero when inputs or dimensions change. Animation advances visible time and requests frames; Paused retains the clock phase while still accepting input and size updates. ShaderSchedule aliases CanvasSchedule and follows its visibility and reduced-motion policy.

Signature
fn schedule(self, schedule: ShaderSchedule) -> Self

animatable_inputs

Opts bound scalar and vector uniforms into animation transactions. Use with_animation or a value-scoped animation wrapper to supply animation intent. Keep bound names and vector dimensions stable. Images and fallback colors switch immediately. Uniform transitions can continue while the shader clock is paused.

Signature
fn animatable_inputs(self) -> Self

Common view modifiers

Dimensions, paint, labels, and typed event callbacks preserve ShaderView. Configure schedule and animatable_inputs before padding, frame, animation, transaction, or other wrappers. Shader views have no view_box or child methods: use WGSL coordinates for pixels and compose separate views around the surface.

Allocate dimensions through the view or its parent; shaders have no intrinsic size. Use radius with clipped for rounded clipping. The containing view owns input and accessibility metadata; pixels are not individual controls. See View Modifiers and Shaders.

Source reference

The implementation is in crates/core/src/drawing/shader.rs. The shared example is in examples/shader.