ShaderEffectDescription
struct · ferroui-base · v0.5.0
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:
layer_size(SHADER_LAYER_SIZE), afloat2(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);layer_scale(SHADER_LAYER_SCALE), afloat(f32): the device pixels per device independent pixel of the layer;- the parameters of the effect, in the order of
CustomEffect::for_each_parameter, each named as the parameter with every-written_(cell-sizeiscell_size): aFloatis afloat, aPoint,VectororSizeafloat2, aColora premultipliedfloat4(red, green and blue times the alpha, 0 to 1), aRectafloat4(x, y, width, height), aBool(0 or 1) or anIntanint. ATextparameter has no uniform, nor has anImageparameter, 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 ofchildren(<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 (bindingSHADER_WGSL_CHILDREN_BINDINGand up, in the order ofchildren).
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.
fn shader() -> Option<ShaderEffectDescription> {
Some(ShaderEffectDescription::content_sampling(PIXELATE_SKSL, PIXELATE_WGSL).with_sample_radius(64.0))
}
Members
skslwgslmodeblend_modesample_radiuspaddingchildrenmasked_by_contentcontent_samplingoverlaywith_sample_radiuswith_paddingwith_childrenwith_content_masksamples_contentwgsl_module
sksl
sksl: std::borrow::Cow<'static, str>
The SkSL program of Skia: half4 main(float2 coord).
wgsl
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.
mode
mode: ShaderEffectMode
Whether the program samples the content or is an overlay.
blend_mode
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.
sample_radius
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.
padding
padding: ShaderEffectPadding
How far the output reaches beyond the content.
children
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).
masked_by_content
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.
content_sampling
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).
overlay
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).
with_sample_radius
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).
with_padding
fn with_padding(self: Self, padding: ShaderEffectPadding) -> Self
The description with the padding padding.
with_children
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.
with_content_mask
fn with_content_mask(self: Self) -> Self
The description of an overlay masked by the content: its output is multiplied by the alpha of the content before it is blended.
samples_content
fn samples_content(self: &Self) -> bool
Whether the program samples the content.
wgsl_module
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 thestruct Uniformsof the program.@group(0) @binding(1) var<uniform> ferroui_pass: FerroUiShaderPass;(SHADER_WGSL_PASS_BINDING): onevec4<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 withtextureLoad(no sampler).For a content-sampling program,
fn sample_content(coord: vec2<f32>) -> vec4<f32>: the texel undercoord(device pixels of the area), as Skia samples the childcontentof a runtime shader image filter at the centre of a texel; transparent beyond the area. An overlay has nosample_content: a call of it does not compile.The entry points
SHADER_WGSL_VERTEX_ENTRY(a triangle over the target) andSHADER_WGSL_FRAGMENT_ENTRY, which callseffectat 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 ofBitmapBlendingMode, premultiplied, the formulas of Skia's blend modes;Unspecifiedis 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_BINDINGplus its index,var ferroui_child_<name>: texture_2d<f32>;andfn sample_<name>(coord: vec2<f32>) -> vec4<f32>: the image stretched over the area, the texel undercoord * 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.
Trait implementations
SendSyncFreezeUnsafeUnpinUnpinUnwindSafeRefUnwindSafePropertyValueAnyValueClassDefaultsBorrow<T>BorrowMut<T>CloneToUninitInto<U>From<T>TryInto<U>TryFrom<U>AnyToOwnedErasedDestructorIntoEitherCloneDebugStructuralPartialEqPartialEq
Availability
Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.