Skip to content

Commit

Permalink
feat(composition): Implement WhiteNoiseEffect + Sample + Win2D + Refa…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
ahmed605 committed Mar 2, 2024
1 parent aefc389 commit 3dc1fc2
Show file tree
Hide file tree
Showing 38 changed files with 2,465 additions and 2,027 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
<Grid x:Name="psGrid" Width="200" Height="200" VerticalAlignment="Top" HorizontalAlignment="Left" ToolTipService.ToolTip="PointSpecularEffect (Turn on Dark Mode)"/>
<Grid x:Name="maskGrid" Width="200" Height="200" VerticalAlignment="Top" HorizontalAlignment="Left" ToolTipService.ToolTip="AlphaMaskEffect"/>
<Grid x:Name="saturationGrid" Width="200" Height="200" VerticalAlignment="Top" HorizontalAlignment="Left" ToolTipService.ToolTip="SaturationEffect"/>
<Grid x:Name="noiseGrid" Width="200" Height="200" VerticalAlignment="Top" HorizontalAlignment="Left" ToolTipService.ToolTip="WhiteNoiseEffect"/>
</GridView>
</UserControl>
2,066 changes: 39 additions & 2,027 deletions src/SamplesApp/UITests.Shared/Windows_UI_Composition/EffectBrushTests.xaml.cs

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions src/Uno.UI.Composition/Composition/CompositionEffectBrush.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,61 @@ half4 main()
sourceFilter, new(bounds));
}

return null;
}
case EffectType.WhiteNoiseEffect:
{
if (effectInterop.GetPropertyCount() == 2)
{
effectInterop.GetNamedPropertyMapping("Frequency", out uint frequencyProp, out _);
effectInterop.GetNamedPropertyMapping("Offset", out uint offsetProp, out _);

Vector2 frequency = (Vector2)effectInterop.GetProperty(frequencyProp);
Vector2 offset = (Vector2)effectInterop.GetProperty(offsetProp);

string shader = $@"
uniform half2 frequency;
uniform half2 offset;
half Hash(half2 p)
{{
return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x))));
}}
half4 main(float2 coords)
{{
float2 coord = coords * 0.81 * frequency + offset;
float2 px00 = floor(coord - 0.5) + 0.5;
float2 px11 = px00 + 1;
float2 px10 = float2(px11.x, px00.y);
float2 px01 = float2(px00.x, px11.y);
float2 factor = coord - px00;
float sample00 = Hash(px00);
float sample10 = Hash(px10);
float sample01 = Hash(px01);
float sample11 = Hash(px11);
float result = mix(mix(sample00, sample10, factor.x), mix(sample01, sample11, factor.x), factor.y);
return half4(result.xxx, 1);
}}
";

SKRuntimeEffect runtimeEffect = SKRuntimeEffect.Create(shader, out string errors);
if (errors is not null)
{
return null;
}

SKRuntimeEffectUniforms uniforms = new(runtimeEffect)
{
{ "frequency", new float[2] { frequency.X, frequency.Y } },
{ "offset", new float[2] { offset.X, offset.Y } }
};

return SKImageFilter.CreatePaint(new SKPaint() { Shader = runtimeEffect.ToShader(false, uniforms), IsAntialias = true, FilterQuality = SKFilterQuality.High }, new(bounds));
}

return null;
}
case EffectType.Unsupported:
Expand Down
69 changes: 69 additions & 0 deletions src/Uno.UI.Composition/Composition/Effects/WhiteNoiseEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using Windows.Graphics.Effects;
using Windows.Graphics.Effects.Interop;

namespace Windows.UI.Composition.Effects
{
[Guid("6152DFC6-9FBA-4810-8CBA-B280AA27BFF6")]
internal class WhiteNoiseEffect : IGraphicsEffect, IGraphicsEffectSource, IGraphicsEffectD2D1Interop
{
private string _name = "WhiteNoiseEffect";
private Guid _id = new Guid("6152DFC6-9FBA-4810-8CBA-B280AA27BFF6");

public string Name
{
get => _name;
set => _name = value;
}

public Vector2 Frequency { get; set; } = new(0.01f);

public Vector2 Offset { get; set; }

public Guid GetEffectId() => _id;

public void GetNamedPropertyMapping(string name, out uint index, out GraphicsEffectPropertyMapping mapping)
{
switch (name)
{
case "Frequency":
{
index = 0;
mapping = GraphicsEffectPropertyMapping.Direct;
break;
}
case "Offset":
{
index = 1;
mapping = GraphicsEffectPropertyMapping.Direct;
break;
}
default:
{
index = 0xFF;
mapping = (GraphicsEffectPropertyMapping)0xFF;
break;
}
}
}

public object GetProperty(uint index)
{
switch (index)
{
case 0:
return Frequency;
case 1:
return Offset;
default:
return null;
}
}

public uint GetPropertyCount() => 2;
public IGraphicsEffectSource GetSource(uint index) => throw new NotSupportedException();
public uint GetSourceCount() => 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Microsoft.Graphics.Canvas
{
public enum CanvasComposite
{
SourceOver = 0,
DestinationOver = 1,
SourceIn = 2,
DestinationIn = 3,
SourceOut = 4,
DestinationOut = 5,
SourceAtop = 6,
DestinationAtop = 7,
Xor = 8,
Add = 9,
Copy = 10,
BoundedCopy = 11,
MaskInvert = 12
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Microsoft.Graphics.Canvas
{
public enum CanvasEdgeBehavior
{
Clamp = 0,
Wrap = 1,
Mirror = 2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Runtime.InteropServices;
using Windows.Graphics.Effects;
using Windows.Graphics.Effects.Interop;

namespace Microsoft.Graphics.Canvas.Effects
{
[Guid("C80ECFF0-3FD5-4F05-8328-C5D1724B4F0A")]
public class AlphaMaskEffect : IGraphicsEffect, IGraphicsEffectSource, IGraphicsEffectD2D1Interop
{
private string _name = "AlphaMaskEffect";
private Guid _id = new Guid("C80ECFF0-3FD5-4F05-8328-C5D1724B4F0A");

public string Name
{
get => _name;
set => _name = value;
}

public IGraphicsEffectSource AlphaMask { get; set; }

public IGraphicsEffectSource Source { get; set; }

public Guid GetEffectId() => _id;

public void GetNamedPropertyMapping(string name, out uint index, out GraphicsEffectPropertyMapping mapping) => throw new NotSupportedException();

public object GetProperty(uint index) => throw new NotSupportedException();

public uint GetPropertyCount() => 0;
public IGraphicsEffectSource GetSource(uint index) => index == 0 ? Source : AlphaMask;
public uint GetSourceCount() => 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Runtime.InteropServices;
using Windows.Graphics.Effects;
using Windows.Graphics.Effects.Interop;

namespace Microsoft.Graphics.Canvas.Effects
{
[Guid("81C5B77B-13F8-4CDD-AD20-C890547AC65D")]
public class BlendEffect : IGraphicsEffect, IGraphicsEffectSource, IGraphicsEffectD2D1Interop
{
private string _name = "BlendEffect";
private Guid _id = new Guid("81C5B77B-13F8-4CDD-AD20-C890547AC65D");

public string Name
{
get => _name;
set => _name = value;
}

public BlendEffectMode Mode { get; set; } = BlendEffectMode.Multiply;

public IGraphicsEffectSource Background { get; set; }

public IGraphicsEffectSource Foreground { get; set; }

public Guid GetEffectId() => _id;

public void GetNamedPropertyMapping(string name, out uint index, out GraphicsEffectPropertyMapping mapping)
{
switch (name)
{
case "Mode":
{
index = 0;
mapping = GraphicsEffectPropertyMapping.Direct;
break;
}
default:
{
index = 0xFF;
mapping = (GraphicsEffectPropertyMapping)0xFF;
break;
}
}
}

public object GetProperty(uint index)
{
switch (index)
{
case 0:
return Mode;
default:
return null;
}
}

public uint GetPropertyCount() => 1;

public IGraphicsEffectSource GetSource(uint index) => index is 0 ? Background : Foreground;

public uint GetSourceCount() => 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Microsoft.Graphics.Canvas.Effects
{
public enum BlendEffectMode
{
Multiply = 0,
Screen = 1,
Darken = 2,
Lighten = 3,
Dissolve = 4,
ColorBurn = 5,
LinearBurn = 6,
DarkerColor = 7,
LighterColor = 8,
ColorDodge = 9,
LinearDodge = 10,
Overlay = 11,
SoftLight = 12,
HardLight = 13,
VividLight = 14,
LinearLight = 15,
PinLight = 16,
HardMix = 17,
Difference = 18,
Exclusion = 19,
Hue = 20,
Saturation = 21,
Color = 22,
Luminosity = 23,
Subtract = 24,
Division = 25
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Runtime.InteropServices;
using Windows.Graphics.Effects;
using Windows.Graphics.Effects.Interop;

namespace Microsoft.Graphics.Canvas.Effects
{
[Guid("2A2D49C0-4ACF-43C7-8C6A-7C4A27874D27")]
public class BorderEffect : IGraphicsEffect, IGraphicsEffectSource, IGraphicsEffectD2D1Interop
{
private string _name = "BorderEffect";
private Guid _id = new Guid("2A2D49C0-4ACF-43C7-8C6A-7C4A27874D27");

public string Name
{
get => _name;
set => _name = value;
}

public CanvasEdgeBehavior ExtendX { get; set; } = CanvasEdgeBehavior.Clamp;

public CanvasEdgeBehavior ExtendY { get; set; } = CanvasEdgeBehavior.Clamp;

public IGraphicsEffectSource Source { get; set; }

public Guid GetEffectId() => _id;

public void GetNamedPropertyMapping(string name, out uint index, out GraphicsEffectPropertyMapping mapping)
{
switch (name)
{
case "ExtendX":
{
index = 0;
mapping = GraphicsEffectPropertyMapping.Direct;
break;
}
case "ExtendY":
{
index = 1;
mapping = GraphicsEffectPropertyMapping.Direct;
break;
}
default:
{
index = 0xFF;
mapping = (GraphicsEffectPropertyMapping)0xFF;
break;
}
}
}

public object GetProperty(uint index)
{
switch (index)
{
case 0:
return ExtendX;
case 1:
return ExtendY;
default:
return null;
}
}

public uint GetPropertyCount() => 2;
public IGraphicsEffectSource GetSource(uint index) => Source;
public uint GetSourceCount() => 1;
}
}
Loading

0 comments on commit 3dc1fc2

Please sign in to comment.