# ShaderEffectDescription

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-base/media/effects/struct-shadereffectdescription/

**struct** · [`ferroui-base`](/docs/0.5.0/api/ferroui-base/) · v0.5.0

```rust
struct ShaderEffectDescription
```

The description of a runtime shader effect (#455, E4): what
`CustomEffect::shader` returns, read once per type.

It carries two programs with one convention: SkSL for Skia, drawn on
every surface of the backend, and WGSL for Vello, drawn by a pass of the
device in its hybrid and GPU modes (E5; the module of the pass is
`wgsl_module`). Vello's CPU mode draws the CPU
reference of the effect (`CustomEffect::cpu_reference`) instead.
## The uniform convention

The uniform block of a shader effect is, in this order:

1. `layer_size` ([`SHADER_LAYER_SIZE`](/docs/0.5.0/api/ferroui-base/media/effects/constant-shader_layer_size/)), a `float2` (`vec2<f32>`): the
   width and the height of the area of the effect in device pixels (the
   content and the padding of the effect, rounded out to whole pixels);
2. `layer_scale` ([`SHADER_LAYER_SCALE`](/docs/0.5.0/api/ferroui-base/media/effects/constant-shader_layer_scale/)), a `float` (`f32`): the device
   pixels per device independent pixel of the layer;
3. the parameters of the effect, in the order of
   `CustomEffect::for_each_parameter`, each named as the parameter with
   every `-` written `_` (`cell-size` is `cell_size`): a `Float` is a
   `float`, a `Point`, `Vector` or `Size` a `float2`, a `Color` a
   premultiplied `float4` (red, green and blue times the alpha, 0 to 1), a
   `Rect` a `float4` (x, y, width, height), a `Bool` (0 or 1) or an `Int` an
   `int`. A `Text` parameter has no uniform, nor has an `Image` parameter,
   which is a child (below).

The values of the parameters are passed as they are: a length in device
independent pixels is turned into device pixels by the program, with
`layer_scale`. The program is given the coordinates of the pixel it
colours in device pixels of the layer: `(0, 0)` is the top left corner of
the area of the effect, the centre of the first pixel `(0.5, 0.5)`.
## The programs

The SkSL program of Skia declares the uniforms above, in that order and
with those types, and, when it samples the content
(`ShaderEffectMode::ContentSampling`), the child `uniform shader content;`
(`CUSTOM_EFFECT_CONTENT`), which it
samples with `content.eval(coord)` (premultiplied, transparent beyond the
area); its entry point is `half4 main(float2 coord)`, which returns a
premultiplied colour.

The WGSL program of Vello declares the uniforms as
the members of `struct Uniforms`, in the same order, and the function
`fn effect(coord: vec2<f32>) -> vec4<f32>`. The pass of the backend adds
the bindings: `uniforms`, the block, and `fn sample_content(coord:
vec2<f32>) -> vec4<f32>`, the content at `coord` (content-sampling
programs only), and calls `effect` at the centre of each pixel.
## Images: the children

An image parameter of the effect (`EffectParameterValue::Image`) named
in `children` is a child of the program (#455, E6):

- in SkSL, `uniform shader <name>;`, declared after the uniforms in the
  order of `children` (`<name>` the parameter's name with every `-`
  written `_`), sampled with `<name>.eval(coord)`;
- in WGSL, `fn sample_<name>(coord: vec2<f32>) -> vec4<f32>`, which the
  pass adds with the texture of the image (binding
  [`SHADER_WGSL_CHILDREN_BINDING`](/docs/0.5.0/api/ferroui-base/media/effects/constant-shader_wgsl_children_binding/) and up, in the order of `children`).

Both give the image at `coord`, in the device pixels of the area of the
effect: the image stretched over the whole area (`layer_size`), the texel
under `coord * image_size / layer_size` (nearest, as the content is
sampled), premultiplied, transparent beyond the area. An empty image
(`EffectImage::empty`) is transparent. The
backends upload an image once (per context or device) and bind the upload
while the image lives, so a frame that changes only uniforms uploads
nothing; the CPU reference of an effect samples
`EffectImage::pixels` the same way
(`EffectImagePixels::sample_stretched`).

Children are for overlays (`ShaderEffectMode::Overlay`): the shape of
Effector's page transitions, programs drawn over the content from images
(with the blend mode `Source` they replace it). A program that samples the
content takes no images: Skia draws it as a runtime shader image filter,
which takes its other children from the builder of the program, and
skia-safe 0.153 binds no setter for them; both backends refuse it, so that
a program draws the same everywhere.

Each backend checks its program against the parameters when it compiles
it (decision 1 of the plan): a program whose uniforms are not those of the
convention is reported once and the effect is drawn as an effect without
a factory (its content unchanged), as is a program that does not compile.

```ignore
fn shader() -> Option<ShaderEffectDescription> {
    Some(ShaderEffectDescription::content_sampling(PIXELATE_SKSL, PIXELATE_WGSL).with_sample_radius(64.0))
}
```

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L148)

## Members

- [`sksl`](#struct_field-sksl)
- [`wgsl`](#struct_field-wgsl)
- [`mode`](#struct_field-mode)
- [`blend_mode`](#struct_field-blend_mode)
- [`sample_radius`](#struct_field-sample_radius)
- [`padding`](#struct_field-padding)
- [`children`](#struct_field-children)
- [`masked_by_content`](#struct_field-masked_by_content)
- [`content_sampling`](#function-content_sampling)
- [`overlay`](#function-overlay)
- [`with_sample_radius`](#function-with_sample_radius)
- [`with_padding`](#function-with_padding)
- [`with_children`](#function-with_children)
- [`with_content_mask`](#function-with_content_mask)
- [`samples_content`](#function-samples_content)
- [`wgsl_module`](#function-wgsl_module)

<a id="struct_field-sksl"></a>

### sksl

```rust
sksl: std::borrow::Cow<'static, str>
```

The SkSL program of Skia: `half4 main(float2 coord)`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L150)

<a id="struct_field-wgsl"></a>

### wgsl

```rust
wgsl: std::borrow::Cow<'static, str>
```

The WGSL program of Vello: `fn effect(coord: vec2<f32>) -> vec4<f32>`
and `struct Uniforms`, wrapped by `wgsl_module`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L153)

<a id="struct_field-mode"></a>

### mode

```rust
mode: ShaderEffectMode
```

Whether the program samples the content or is an overlay.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L155)

<a id="struct_field-blend_mode"></a>

### blend_mode

```rust
blend_mode: crate::media::imaging::BitmapBlendingMode
```

How an overlay is drawn over the content (source over by default);
not used by a content-sampling program, whose output replaces the
content.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L159)

<a id="struct_field-sample_radius"></a>

### sample_radius

```rust
sample_radius: f64
```

How far from the pixel it colours the program samples the content at
most, in device independent pixels (0 by default): the backend
provides the content that far beyond the part of the area it draws
(a dirty rectangle), so that the result does not depend on what is
redrawn.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L165)

<a id="struct_field-padding"></a>

### padding

```rust
padding: ShaderEffectPadding
```

How far the output reaches beyond the content.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L167)

<a id="struct_field-children"></a>

### children

```rust
children: Vec<std::borrow::Cow<'static, str>>
```

The images the program samples (#455, E6): the names of image
parameters of the effect, in the order the program declares them as
children (see "Images: the children" above). At most
`MAX_SHADER_CHILDREN`, each an image parameter, for an overlay only;
a description that breaks this is reported once and drawn as an effect
without a factory
(`ImmutableCustomEffect::check_shader_children`).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L175)

<a id="struct_field-masked_by_content"></a>

### masked_by_content

```rust
masked_by_content: bool
```

Whether the output of an overlay is masked by the content (#580): the
output `S` is multiplied by the alpha of the content `D` under it
before it is blended, `blend(S * D.a, D)`, so that the overlay draws
nothing over transparent content (the corners of a rounded panel, the
gaps between its children), as Effector draws its shader overlays
(`EffectorRuntime.DrawMaskedShaderOverlay`: the output masked
`DstIn` by the snapshot of the content, then blended over it). Skia
draws it with a runtime blender, the pass of Vello multiplies the
output by the alpha of the content texel, and the CPU reference does
the same. `false` by default; not used by a content-sampling program,
which composes its output with the content itself.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L187)

<a id="function-content_sampling"></a>

### content_sampling

```rust
fn content_sampling(sksl: impl Into<Cow<'static, str>>, wgsl: impl Into<Cow<'static, str>>) -> Self
```

A program that samples the content and replaces it
(`ShaderEffectMode::ContentSampling`).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L193)

<a id="function-overlay"></a>

### overlay

```rust
fn overlay(sksl: impl Into<Cow<'static, str>>, wgsl: impl Into<Cow<'static, str>>, blend_mode: BitmapBlendingMode) -> Self
```

A program drawn over the content with `blend_mode`
(`ShaderEffectMode::Overlay`).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L208)

<a id="function-with_sample_radius"></a>

### with_sample_radius

```rust
fn with_sample_radius(self: Self, radius: f64) -> Self
```

The description with the sample radius
`radius` (device independent pixels; a negative or not finite value is
0).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L219)

<a id="function-with_padding"></a>

### with_padding

```rust
fn with_padding(self: Self, padding: ShaderEffectPadding) -> Self
```

The description with the padding `padding`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L225)

<a id="function-with_children"></a>

### with_children

```rust
fn with_children<S: Into<Cow<'static, str>>>(self: Self, children: impl IntoIterator<Item = S>) -> Self
```

The description with the children `children`: the names of the image
parameters the program samples, in the order it declares them.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L232)

<a id="function-with_content_mask"></a>

### with_content_mask

```rust
fn with_content_mask(self: Self) -> Self
```

The description of an overlay [masked by the
content](Self::masked_by_content): its output is multiplied by the
alpha of the content before it is blended.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L240)

<a id="function-samples_content"></a>

### samples_content

```rust
fn samples_content(self: &Self) -> bool
```

Whether the program samples the content.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L246)

<a id="function-wgsl_module"></a>

### wgsl_module

```rust
fn wgsl_module(self: &Self) -> String
```

The WGSL module of the pass that draws this description on the GPU
(Vello's hybrid and GPU modes, E5; the source of the GLSL ES of
WebGL2 that `ferroui-build` translates at build time): the program
(`wgsl`) followed by what the pass adds to it.

- `@group(0) @binding(0) var<uniform> uniforms: Uniforms;`
  (`SHADER_WGSL_UNIFORMS_BINDING`): the block of the convention,
  whose type is the `struct Uniforms` of the program.
- `@group(0) @binding(1) var<uniform> ferroui_pass: FerroUiShaderPass;`
  (`SHADER_WGSL_PASS_BINDING`): one `vec4<f32>` `area`, the device
  pixel of the area of the effect at the top left texel of the
  textures (`x`, `y`, whole pixels) and the width and the height of
  the part of the area they hold (`z`, `w`).
- `@group(0) @binding(2) var ferroui_content: texture_2d<f32>;`
  (`SHADER_WGSL_CONTENT_BINDING`): the content, premultiplied, one
  texel per device pixel, read with `textureLoad` (no sampler).
- For a content-sampling program, `fn sample_content(coord:
  vec2<f32>) -> vec4<f32>`: the texel under `coord` (device pixels of
  the area), as Skia samples the child `content` of a runtime shader
  image filter at the centre of a texel; transparent beyond the area.
  An overlay has no `sample_content`: a call of it does not compile.
- The entry points `SHADER_WGSL_VERTEX_ENTRY` (a triangle over the
  target) and `SHADER_WGSL_FRAGMENT_ENTRY`, which calls `effect` at
  the centre of each pixel of the area: a content-sampling program's
  output is the result; an overlay's output, clamped to 0..1 (and
  multiplied by the alpha of the content texel when the description is
  masked by the content), is composed over
  the content with the blend mode
  (every mode of `BitmapBlendingMode`, premultiplied, the formulas of
  Skia's blend modes; `Unspecified` is source over). Pixels beyond the
  part of the area the textures hold are transparent.

- For each child (an image, E6), at binding
  `SHADER_WGSL_CHILDREN_BINDING` plus its index,
  `var ferroui_child_<name>: texture_2d<f32>;` and `fn
  sample_<name>(coord: vec2<f32>) -> vec4<f32>`: the image stretched
  over the area, the texel under `coord * size / layer_size`,
  transparent beyond it.

The names the pass adds start with `ferroui_` (and `FerroUi`), but
`uniforms`, `sample_content`, `sample_<name>` of the children,
`vs_main` and `fs_main`: a program that declares one of them does not
compile.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/effects/custom_effect_shader.rs#L293)

## Trait implementations

- `Send`
- `Sync`
- `Freeze`
- `UnsafeUnpin`
- `Unpin`
- `UnwindSafe`
- `RefUnwindSafe`
- [`PropertyValue`](/docs/0.5.0/api/ferroui-base/trait-propertyvalue/)
- [`AnyValue`](/docs/0.5.0/api/ferroui-base/trait-anyvalue/)
- [`ClassDefaults`](/docs/0.5.0/api/ferroui-base/trait-classdefaults/)
- `Borrow<T>`
- `BorrowMut<T>`
- `CloneToUninit`
- `Into<U>`
- `From<T>`
- `TryInto<U>`
- `TryFrom<U>`
- `Any`
- `ToOwned`
- `ErasedDestructor`
- `IntoEither`
- `Clone`
- `Debug`
- `StructuralPartialEq`
- `PartialEq`

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
