Skip to content

Commit b241e69

Browse files
author
Jonah Williams
authored
[ui] reland add docs to FragmentShader (flutter#37699)
1 parent fa7e196 commit b241e69

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lib/ui/painting.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,13 +4267,58 @@ class FragmentShader extends Shader {
42674267
Float32List _floats = _kEmptyFloat32List;
42684268

42694269
/// Sets the float uniform at [index] to [value].
4270+
///
4271+
/// All uniforms defined in a fragment shader that are not samplers must be
4272+
/// set through this method. This includes floats and vec2, vec3, and vec4.
4273+
/// The correct index for each uniform is determined by the order of the
4274+
/// uniforms as defined in the fragment program, ignoring any samplers. For
4275+
/// data types that are composed of multiple floats such as a vec4, more than
4276+
/// one call to [setFloat] is required.
4277+
///
4278+
/// For example, given the following uniforms in a fragment program:
4279+
///
4280+
/// ```glsl
4281+
/// uniform float uScale;
4282+
/// uniform sampler2D uTexture;
4283+
/// uniform vec2 uMagnitude;
4284+
/// uniform vec4 uColor;
4285+
/// ```
4286+
///
4287+
/// Then the corresponding Dart code to correctly initialize these uniforms
4288+
/// is:
4289+
///
4290+
/// ```dart
4291+
/// void updateShader(ui.FragmentShader shader, Color color, ImageShader sampler) {
4292+
/// shader.setFloat(0, 23); // uScale
4293+
/// shader.setFloat(1, 114); // uMagnitude x
4294+
/// shader.setFloat(2, 83); // uMagnitude y
4295+
///
4296+
/// // Convert color to premultiplied opacity.
4297+
/// shader.setFloat(3, color.red / 255 * color.opacity); // uColor r
4298+
/// shader.setFloat(4, color.green / 255 * color.opacity); // uColor g
4299+
/// shader.setFloat(5, color.blue / 255 * color.opacity); // uColor b
4300+
/// shader.setFloat(6, color.opacity); // uColor a
4301+
///
4302+
/// // initialize sampler uniform.
4303+
/// shader.setSampler(0, sampler);
4304+
/// }
4305+
/// ```
4306+
///
4307+
/// Note how the indexes used does not count the `sampler2D` uniform. This
4308+
/// uniform will be set separately with [setSampler], with the index starting
4309+
/// over at 0.
4310+
///
4311+
/// Any float uniforms that are left uninitialized will default to `0`.
42704312
void setFloat(int index, double value) {
42714313
assert(!debugDisposed, 'Tried to accesss uniforms on a disposed Shader: $this');
42724314
_floats[index] = value;
42734315
}
42744316

42754317
/// Sets the sampler uniform at [index] to [sampler].
42764318
///
4319+
/// The index provided to setSampler is the index of the sampler uniform defined
4320+
/// in the fragment program, excluding all non-sampler uniforms.
4321+
///
42774322
/// All the sampler uniforms that a shader expects must be provided or the
42784323
/// results will be undefined.
42794324
void setSampler(int index, ImageShader sampler) {

0 commit comments

Comments
 (0)