Skip to content

Commit e2377b3

Browse files
authored
Update FrostedGlassEffect (#3610)
1 parent 2df8b61 commit e2377b3

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

hub/apps/develop/win2d/custom-effects.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ And this part is done! This shader will generate our custom noise texture whenev
341341
### Creating a custom effect
342342

343343
For our easy to use, packaged effect, we can use the `CanvasEffect` type from ComputeSharp. This type provides a straightforward way to setup all the necessary logic to create an effect graph and update it via public properties that users of the effect can interact with. There are two main methods we'll need to implement:
344-
- `BuildEffectGraph`: this method is responsible for building the effect graph and returning the output `ICanvasImage` that we want to draw (ie. the `ICanvasImage` object representing the output node of the effect graph being constructed). It also needs to store any effects that can be updated at a later time into fields of the effect class, so they can be accessed when needed.
344+
- `BuildEffectGraph`: this method is responsible for building the effect graph that we want to draw. That is, it needs to create all effects we need, and register the output node for the graph. For effects that can be updated at a later time, the registration is done with an associated `EffectNode<T>` value, which acts as lookup key to retrieve the effects from the graph when needed.
345345
- `ConfigureEffectGraph`: this method refreshes the effect graph by applying the settings that the user has configured. This method is automatically invoked when needed, right before drawing the effect, and only if at least one effect property has been modified since the last time the effect was used.
346346

347347
Our custom effect can be defined as follows:
@@ -353,8 +353,9 @@ using Microsoft.Graphics.Canvas.Effects;
353353

354354
public sealed class FrostedGlassEffect : CanvasEffect
355355
{
356-
private GaussianBlurEffect? _blurEffect;
357-
private PixelShaderEffect<NoiseShader>? _noiseEffect;
356+
private static readonly EffectNode<GaussianBlurEffect> BlurNode = new();
357+
private static readonly EffectNode<PixelShaderEffect<NoiseShader>> NoiseNode = new();
358+
358359
private ICanvasImage? _source;
359360
private double _blurAmount;
360361
private double _noiseAmount;
@@ -378,7 +379,7 @@ public sealed class FrostedGlassEffect : CanvasEffect
378379
}
379380

380381
/// <inheritdoc/>
381-
protected override ICanvasImage BuildEffectGraph()
382+
protected override void BuildEffectGraph(EffectGraph effectGraph)
382383
{
383384
// Create the effect graph as follows:
384385
//
@@ -402,25 +403,27 @@ public sealed class FrostedGlassEffect : CanvasEffect
402403
blendEffect.Background = gaussianBlurEffect;
403404
blendEffect.Foreground = premultiplyEffect;
404405

405-
// Save the effects that have mutable properties
406-
_blurEffect = gaussianBlurEffect;
407-
_noiseEffect = noiseEffect;
408-
409-
// Return the output node from the graph
410-
return blendEffect;
406+
// Register all effects. For those that need to be referenced later (ie. the ones with
407+
// properties that can change), we use a node as a key, so we can perform lookup on
408+
// them later. For others, we register them anonymously. This allows the effect
409+
// to autommatically and correctly handle disposal for all effects in the graph.
410+
effectGraph.RegisterNode(BlurNode, gaussianBlurEffect);
411+
effectGraph.RegisterNode(NoiseNode, noiseEffect);
412+
effectGraph.RegisterNode(premultiplyEffect);
413+
effectGraph.RegisterOutputNode(blendEffect);
411414
}
412415

413416
/// <inheritdoc/>
414-
protected override void ConfigureEffectGraph()
417+
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
415418
{
416419
// Set the effect source
417-
_blurEffect!.Source = Source;
420+
effectGraph.GetNode(BlurNode).Source = Source;
418421

419422
// Configure the blur amount
420-
_blurEffect!.BlurAmount = (float)BlurAmount;
423+
effectGraph.GetNode(BlurNode).BlurAmount = (float)BlurAmount;
421424

422425
// Set the constant buffer of the shader
423-
_noiseEffect!.ConstantBuffer = new NoiseShader((float)NoiseAmount);
426+
effectGraph.GetNode(NoiseNode).ConstantBuffer = new NoiseShader((float)NoiseAmount);
424427
}
425428
}
426429
```

0 commit comments

Comments
 (0)