Skip to content

Commit 1d3c72d

Browse files
authored
Adds example glsl and opengles instructions for ImageFilter fragment shaders (#12610)
_Description of what this PR is changing or adding, and why:_ Added further clarification for opengles and an extra example. _Issues fixed by this PR (if any):_ _PRs or commits this PR depends on (if any):_ ## Presubmit checklist - [x] If you are unwilling, or unable, to sign the CLA, even for a _tiny_, one-word PR, please file an issue instead of a PR. - [x] If this PR is not meant to land until a future stable release, mark it as draft with an explanation. - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style)—for example, it doesn't use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first-person pronouns). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer.
1 parent 9031978 commit 1d3c72d

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/content/ui/design/graphics/fragment-shaders.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ to apply shaders to already rendered content.
122122
[`ImageFilter`][] provides a constructor, [`ImageFilter.shader`][],
123123
for creating an [`ImageFilter`][] with a custom fragment shader.
124124

125-
Fragment shaders that use the `ImageFilter` API receive some
126-
values automatically from the engine. The `sampler2D` value at index 0
127-
is set to the filter input image, and the `float` values at indices 0
128-
and 1 are set to the image's width and height.
129-
Your shader must specify this constructor to accept these values (for example,
130-
a `sampler2D` and a `vec2`), but you should not set them from your Dart code.
131-
132125
```dart
133126
Widget build(BuildContext context, FragmentShader shader) {
134127
return ClipRect(
@@ -150,6 +143,38 @@ When using [`ImageFilter`][] with [`BackdropFilter`][], a [`ClipRect`][] can be
150143
used to limit the area that is affected by the [`ImageFilter`][]. Without a
151144
[`ClipRect`][] the [`BackdropFilter`][] will be applied to the whole screen.
152145

146+
`ImageFilter` fragment shaders receive some uniforms automatically from the
147+
engine. The `sampler2D` value at index 0 is set to the filter input image, and
148+
the `float` values at indices 0 and 1 are set to the image's width and height.
149+
Your shader must specify this constructor to accept these values (for example, a
150+
`sampler2D` and a `vec2`), but you should not set them from your Dart code.
151+
152+
When targeting OpenGLES the y-coordinates of the texture will be flipped so the
153+
fragment shader should un-flip the UVs when sampling from textures provided by
154+
the engine.
155+
156+
```glsl
157+
#version 460 core
158+
#include <flutter/runtime_effect.glsl>
159+
160+
out vec4 fragColor;
161+
162+
// These uniforms are automatically set by the engine.
163+
uniform vec2 u_size;
164+
uniform sampler2D u_texture;
165+
166+
void main() {
167+
vec2 uv = FlutterFragCoord().xy / u_size;
168+
#ifdef IMPELLER_TARGET_OPENGLES
169+
// When sampling from u_texture on OpenGLES the y-coordinates will be flipped.
170+
uv.y = 1.0 - uv.y;
171+
#endif
172+
vec4 color = texture(u_texture, uv);
173+
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
174+
fragColor = vec4(vec3(gray), color.a);
175+
}
176+
```
177+
153178
[`ImageFilter`]: {{site.api}}/flutter/dart-ui/ImageFilter-class.html
154179
[`ImageFiltered`]: {{site.api}}/flutter/widgets/ImageFiltered-class.html
155180
[`BackdropFilter`]: {{site.api}}/flutter/widgets/BackdropFilter-class.html

0 commit comments

Comments
 (0)