forked from AvaloniaUI/Avalonia
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
421 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<UserControl xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" | ||
x:Class="ControlCatalog.Pages.SkSLEffectPage"> | ||
<UniformGrid Columns="3" Height="200"> | ||
<Rectangle x:Name="Rectangle0" Fill="Red" Margin="20"/> | ||
<Rectangle x:Name="Rectangle1" Fill="Red" Margin="20"/> | ||
<Rectangle x:Name="Rectangle2" Fill="Red" Margin="20"/> | ||
</UniformGrid> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using System; | ||
using Avalonia; | ||
using Avalonia.Animation; | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.Shapes; | ||
using Avalonia.Data; | ||
using Avalonia.Markup.Xaml; | ||
using Avalonia.Platform; | ||
using Avalonia.Skia.Effects; | ||
using Avalonia.Styling; | ||
using SkiaSharp; | ||
|
||
namespace ControlCatalog.Pages | ||
{ | ||
public partial class SkSLEffectPage : UserControl | ||
{ | ||
public SkSLEffectPage() | ||
{ | ||
InitializeComponent(); | ||
InitEffect(); | ||
} | ||
|
||
private void InitializeComponent() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
private void InitEffect() | ||
{ | ||
var rectangle1 = this.FindControl<Rectangle>("Rectangle1")!; | ||
var shaderBuilder = CreateSimpleShaderBuilder(); | ||
if (shaderBuilder != null) | ||
{ | ||
rectangle1.Effect = new SkSLEffect(shaderBuilder) | ||
{ | ||
ChildShaderNames = ["src"], | ||
Inputs = [null], | ||
}; | ||
} | ||
|
||
var rectangle2 = this.FindControl<Rectangle>("Rectangle2")!; | ||
try | ||
{ | ||
var effect = new DissolveSkSLEffect(); | ||
effect.Progress = 0.5f; | ||
effect[!DissolveSkSLEffect.ResolutionProperty] = new Binding | ||
{ | ||
Source = rectangle2, | ||
Path = "Bounds.Size", | ||
}; | ||
rectangle2.Effect = effect; | ||
|
||
var animation = new Animation | ||
{ | ||
Duration = TimeSpan.FromSeconds(2), | ||
IterationCount = IterationCount.Infinite, | ||
PlaybackDirection = PlaybackDirection.Alternate, | ||
Children = { | ||
new KeyFrame | ||
{ | ||
Setters = | ||
{ | ||
new Setter(DissolveSkSLEffect.ProgressProperty, 0f), | ||
}, | ||
KeyTime = TimeSpan.FromSeconds(0), | ||
}, | ||
new KeyFrame | ||
{ | ||
Setters = | ||
{ | ||
new Setter(DissolveSkSLEffect.ProgressProperty, 1f), | ||
}, | ||
KeyTime = TimeSpan.FromSeconds(2), | ||
} | ||
} | ||
}; | ||
|
||
_ = animation.RunAsync(effect); | ||
} | ||
catch | ||
{ | ||
// Do not crash. | ||
} | ||
} | ||
|
||
private SKRuntimeShaderBuilder? CreateSimpleShaderBuilder() | ||
{ | ||
var sksl = @" | ||
uniform shader src; | ||
float4 main(float2 coord) { | ||
return src.eval(coord).bgra; | ||
} | ||
"; | ||
var effect = SKRuntimeEffect.CreateShader(sksl, out var str); | ||
if (effect != null) | ||
{ | ||
return new SKRuntimeShaderBuilder(effect); | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public class DissolveSkSLEffect : SkSLEffect | ||
{ | ||
public static readonly StyledProperty<float> ProgressProperty = AvaloniaProperty.Register<DissolveSkSLEffect, float>(nameof(Progress), default); | ||
|
||
public float Progress | ||
{ | ||
get => GetValue(ProgressProperty); | ||
set => SetValue(ProgressProperty, value); | ||
} | ||
|
||
public static readonly StyledProperty<Size> ResolutionProperty = AvaloniaProperty.Register<DissolveSkSLEffect, Size>(nameof(Resolution), default); | ||
|
||
public Size Resolution | ||
{ | ||
get => GetValue(ResolutionProperty); | ||
set => SetValue(ResolutionProperty, value); | ||
} | ||
|
||
public DissolveSkSLEffect() : base(CreateShaderBuilder()) | ||
{ | ||
ChildShaderNames = ["src"]; | ||
Inputs = [null]; | ||
AffectsRender<SkSLEffect>(ProgressProperty, ResolutionProperty); | ||
|
||
RegisterUniform("progress", ProgressProperty); | ||
RegisterUniform("resolution", ResolutionProperty); | ||
} | ||
|
||
private static SKRuntimeShaderBuilder? s_shaderBuilder; | ||
private static SKRuntimeShaderBuilder CreateShaderBuilder() | ||
{ | ||
if (s_shaderBuilder != null) | ||
{ | ||
return s_shaderBuilder; | ||
} | ||
|
||
var sksl = @" | ||
uniform float2 resolution; | ||
uniform shader src; | ||
uniform shader noise; | ||
uniform float2 noiseResolution; | ||
uniform float progress; | ||
float4 main(float2 coord) { | ||
float val = noise.eval(fract(coord / resolution) * noiseResolution).x; | ||
if(val < progress) | ||
{ | ||
return src.eval(coord); | ||
} | ||
else | ||
{ | ||
return float4(0,0,0,0); | ||
} | ||
} | ||
"; | ||
var effect = SKRuntimeEffect.CreateShader(sksl, out var str); | ||
if (effect != null) | ||
{ | ||
var noise = AssetLoader.Open(new Uri("avares://ControlCatalog/Assets/noise.png")); | ||
var noiseImage = SKImage.FromEncodedData(noise); | ||
var noiseImageShader = SKShader.CreateImage(noiseImage); | ||
var builder = new SKRuntimeShaderBuilder(effect); | ||
builder.Uniforms["noiseResolution"] = new SKSize(noiseImage.Width, noiseImage.Height); | ||
builder.Children["noise"] = noiseImageShader; | ||
s_shaderBuilder = builder; | ||
return s_shaderBuilder; | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Avalonia.Media | ||
{ | ||
public interface IShaderEffect : IEffect | ||
{ | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionSimpleShaderEffect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Avalonia.Media; | ||
using Avalonia.Rendering.Composition.Transport; | ||
|
||
namespace Avalonia.Rendering.Composition.Server | ||
{ | ||
internal sealed class ServerCompositionSimpleShaderEffect : SimpleServerRenderResource, IShaderEffect, IImmutableEffect | ||
{ | ||
public ServerCompositionSimpleShaderEffect(ServerCompositor compositor) : base(compositor) | ||
{ | ||
} | ||
|
||
public object? ShaderObject { get; set; } | ||
|
||
public string[] ChildShaderNames { get; set; } = []; | ||
|
||
public object?[] Inputs = []; | ||
|
||
protected override void DeserializeChangesCore(BatchStreamReader reader, TimeSpan committedAt) | ||
{ | ||
base.DeserializeChangesCore(reader, committedAt); | ||
|
||
(ShaderObject as IDisposable)?.Dispose(); | ||
ShaderObject = reader.ReadObject<object>(); | ||
ChildShaderNames = reader.ReadObject<string[]>(); | ||
Inputs = reader.ReadObject<object?[]>(); | ||
} | ||
|
||
public bool Equals(IEffect? other) | ||
{ | ||
return false; | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
base.Dispose(); | ||
(ShaderObject as IDisposable)?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.