Skip to main content

Shaders & Textures

Textures

Declare sampled and storage textures, upload sources, and configure sampling.


Texture definitions establish allocation, sampling, upload, and shader-visibility contracts. Runtime values provide or replace source images without changing that declaration.

Definition

Field Default Purpose
source null Initial TextureValue; unsupported for storage textures
colorSpace 'srgb' Default format is rgba8unorm-srgb; linear uses rgba8unorm
format Color-space default Explicit allocation format; required for storage
flipY true Flip external source during upload
premultipliedAlpha false External-image upload behavior
generateMipmaps false Generate the full mip chain on the GPU
update Source-dependent Upload policy
filter 'linear' Min/mag and mipmap filter
addressModeU/V 'clamp-to-edge' Sampler wrapping
anisotropy 1 Floored and clamped to 1..16
storage false Make the texture compute-writable
width, height Required explicit dimensions for storage
fragmentVisible Format-aware Include the sampled binding in fragment WGSL

Each fragment-visible texture uImage produces uImage and uImageSampler bindings. A null runtime source uses a valid fallback texture.

Runtime values

source and state.setTexture(...) accept ImageBitmap, image, canvas, video, { source, width?, height?, colorSpace?, flipY?, premultipliedAlpha?, generateMipmaps?, update? }, or null.

Update mode precedence is runtime override, definition default, then automatic fallback:

Mode Uploads
'once' First bind and source, size, or format changes
'onInvalidate' On invalidation or source change
'perFrame' Every rendered frame

Video sources default to perFrame; other sources default to once.

Sampling constraints

Integer *uint and *sint storage formats default to fragmentVisible: false, because fragment bindings use texture_2d<f32>. Explicitly enabling fragment visibility for one of those formats throws.

r32float, rg32float, and rgba32float use non-filtering sampling unless the device supports float32-filterable. When unsupported, a requested linear filter is coerced to nearest to keep bind-group and sampler types valid.

Storage textures

storage: true requires an explicit storage-compatible format, width, and height, and rejects source. Compute bindings are generated independently of fragmentVisible.

Use PingPongComputePass with a storage texture. A PingPongShaderPass target must instead be a normal fragment-visible sampled texture; the feedback pass owns its A/B render textures.

Allocation and mipmaps

The renderer reallocates when source dimensions or allocation format change and otherwise follows the update mode. Mipmaps are generated by rendering each level from the previous level in the same GPU allocation.

See Texture Loading for URL loading, Compute Shaders for storage bindings, and the Glass Pane demo for an editable loaded texture.