Sergio0694 / ComputeSharp

A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute and pixel shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
MIT License
2.65k stars 122 forks source link

CanvasEffect property source generator #797

Open Sergio0694 opened 2 months ago

Sergio0694 commented 2 months ago

Description

This issue tracks adding a new source generator for CanvasEffect properties. It will be conceptually similar to the MVVM Toolkit observable property generator.

Proposed API

In ComputeSharp.D2D1.WinUI:

namespace ComputeSharp.D2D1.WinUI;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class GeneratedCanvasEffectPropertyAttribute(CanvasEffectInvalidationType invalidationType = CanvasEffectInvalidationType.Update) : Attribute
{
    public CanvasEffectInvalidationType InvalidationType { get; }
}

Example use

User code:

public sealed partial class MyEffect : CanvasEffect
{
    [GeneratedCanvasEffectProperty]
    public partial int BlurAmount { get; set; }
}

Generated code:

partial class MyEffect : CanvasEffect
{
    public partial int BlurAmount
    {
        get => field;
        set
        {
            if (EqualityComparer<int>.Default.Equals(field, value))
            {
                return;
            }

            int oldValue = field;

            OnBlurAmountChanging(value);
            OnBlurAmountChanging(oldValue, value);

            field = value;

            OnBlurAmountChanged(value);
            OnBlurAmountChanged(oldValue, value);

            InvalidateEffectGraph(invalidationType);
        }
    }

    partial void OnBlurAmountChanging(int newValue);
    partial void OnBlurAmountChanging(int oldValue, int newValue);
    partial void OnBlurAmountChanged(int newValue);
    partial void OnBlurAmountChanged(int oldValue, int newValue);
}

This should contain any drawbacks for the proposal that came up during the discussions.

Alternatives

Just do nothing.

Work items