Skip to content

Commit d16ba4b

Browse files
authored
Added example for toImageSync + setImageSampler (#12686)
_Description of what this PR is changing or adding, and why:_ Adds more clarity about how to chain together fragment shaders. _Issues fixed by this PR (if any):_ fixes flutter/flutter#177133 _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 7638cd2 commit d16ba4b

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ or from part of the application using
327327
[`Picture.toImageSync`]: {{site.api}}/flutter/dart-ui/Picture/toImageSync.html
328328
[`Scene.toImageSync`]: {{site.api}}/flutter/dart-ui/Scene/toImageSync.html
329329

330+
##### Sampler usage in GLSL example
331+
330332
```glsl
331333
#include <flutter/runtime_effect.glsl>
332334
@@ -349,7 +351,45 @@ supported and needs to be emulated in the shader.
349351

350352
[`TileMode.clamp`]: {{site.api}}/flutter/dart-ui/TileMode.html
351353

352-
### Performance considerations
354+
##### `toImageSync` example
355+
356+
```dart
357+
class SDFPainter {
358+
SDFPainter(this.sdfShader, this.renderShader);
359+
360+
FragmentShader sdfShader;
361+
FragmentShader renderShader;
362+
Image? _sdf;
363+
bool isDirty = false;
364+
double radius = 0.5;
365+
366+
void paint(Canvas canvas, Size size) {
367+
if (_sdf == null || isDirty) {
368+
final recorder = PictureRecorder();
369+
final subCanvas = Canvas(recorder);
370+
final paint = Paint()..shader = sdfShader;
371+
sdfShader.setFloat(0, size.width);
372+
sdfShader.setFloat(1, size.height);
373+
sdfShader.setFloat(2, radius);
374+
subCanvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);
375+
final picture = recorder.endRecording();
376+
_sdf = picture.toImageSync(size.width.toInt(), size.height.toInt());
377+
isDirty = false;
378+
}
379+
380+
renderShader.setFloat(0, size.width);
381+
renderShader.setFloat(1, size.height);
382+
renderShader.setImageSampler(0, _sdf!);
383+
384+
canvas.drawRect(
385+
Rect.fromLTWH(0, 0, size.width, size.height),
386+
Paint()..shader = renderShader,
387+
);
388+
}
389+
}
390+
```
391+
392+
## Performance considerations
353393

354394
When targeting the Skia backend,
355395
loading the shader might be expensive since it
@@ -368,7 +408,7 @@ check out [Writing efficient shaders][] on GitHub.
368408

369409
[Writing efficient shaders]: {{site.repo.flutter}}/blob/main/docs/engine/impeller/docs/shader_optimization.md
370410

371-
### Other resources
411+
## Other resources
372412

373413
For more information, here are a few resources.
374414

0 commit comments

Comments
 (0)