From 6897ecfcb2722ccc9f0d6bbed1bae892addc4700 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 22 Oct 2023 13:05:30 +0200 Subject: [PATCH] Use primary constructors in remaining projects --- .../Primitives/Complex64.cs | 19 ++++--------- .../Processors/HlslBokehBlurProcessor.cs | 2 ++ .../Processors/HlslGaussianBlurProcessor.cs | 2 ++ .../Backend/PixelShaderEffect.cs | 19 +++---------- .../Shaders/PixelShaderEffect.cs | 19 +++---------- .../ViewModels/ShaderRunnerViewModel.cs | 27 +++++++------------ .../Backend/Win32Application.cs | 19 +++---------- .../Helpers/TolerantImageComparer.cs | 26 +++++------------- .../Attributes/AdditionalDataAttribute.cs | 14 +++------- .../Attributes/AdditionalResourceAttribute.cs | 14 +++------- .../Attributes/DataAttribute.cs | 14 +++------- .../Attributes/DeviceAttribute.cs | 14 +++------- .../Attributes/ResourceAttribute.cs | 14 +++------- .../Helpers/TolerantImageComparer.cs | 26 +++++------------- 14 files changed, 55 insertions(+), 174 deletions(-) diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs b/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs index 03c2ab44..e630a091 100644 --- a/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs +++ b/samples/ComputeSharp.ImageProcessing/Primitives/Complex64.cs @@ -11,28 +11,19 @@ namespace SixLabors.ImageSharp; /// /// This is a more efficient version of the type. /// -internal readonly struct Complex64 +/// The real part in the complex number. +/// The imaginary part in the complex number. +internal readonly struct Complex64(float real, float imaginary) { /// /// The real part of the complex number. /// - public readonly float Real; + public readonly float Real = real; /// /// The imaginary part of the complex number. /// - public readonly float Imaginary; - - /// - /// Initializes a new instance of the struct. - /// - /// The real part in the complex number. - /// The imaginary part in the complex number. - public Complex64(float real, float imaginary) - { - this.Real = real; - this.Imaginary = imaginary; - } + public readonly float Imaginary = imaginary; /// /// Performs the multiplication operation between a instance and a scalar. diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index f6435bbc..f1aac6fb 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -5,6 +5,8 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; +#pragma warning disable IDE0290 + namespace ComputeSharp.BokehBlur.Processors; /// diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs index c2e67e9d..3aa51825 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -5,6 +5,8 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; +#pragma warning disable IDE0290 + namespace ComputeSharp.BokehBlur.Processors; /// diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs index c31a4a82..d8d8501e 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/PixelShaderEffect.cs @@ -56,7 +56,8 @@ internal abstract class PixelShaderEffect : CanvasEffect /// An effect for an animated pixel shader. /// /// The type of pixel shader to render. - public sealed class For : PixelShaderEffect + /// The input factory. + public sealed class For(For.Factory factory) : PixelShaderEffect where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { /// @@ -64,20 +65,6 @@ internal abstract class PixelShaderEffect : CanvasEffect /// private static readonly EffectNode> Effect = new(); - /// - /// The factory to use. - /// - private readonly Factory factory; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The input factory. - public For(Factory factory) - { - this.factory = factory; - } - /// protected override void BuildEffectGraph(EffectGraph effectGraph) { @@ -87,7 +74,7 @@ internal abstract class PixelShaderEffect : CanvasEffect /// protected override void ConfigureEffectGraph(EffectGraph effectGraph) { - effectGraph.GetNode(Effect).ConstantBuffer = this.factory(ElapsedTime, ScreenWidth, ScreenHeight); + effectGraph.GetNode(Effect).ConstantBuffer = factory(ElapsedTime, ScreenWidth, ScreenHeight); } /// diff --git a/samples/ComputeSharp.SwapChain.WinUI/Shaders/PixelShaderEffect.cs b/samples/ComputeSharp.SwapChain.WinUI/Shaders/PixelShaderEffect.cs index 62e38acb..3d61ad23 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Shaders/PixelShaderEffect.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/Shaders/PixelShaderEffect.cs @@ -56,7 +56,8 @@ public abstract class PixelShaderEffect : CanvasEffect /// An effect for an animated pixel shader. /// /// The type of pixel shader to render. - public sealed class For : PixelShaderEffect + /// The input factory. + public sealed class For(For.Factory factory) : PixelShaderEffect where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { /// @@ -64,20 +65,6 @@ public abstract class PixelShaderEffect : CanvasEffect /// private static readonly EffectNode> Effect = new(); - /// - /// The factory to use. - /// - private readonly Factory factory; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The input factory. - public For(Factory factory) - { - this.factory = factory; - } - /// protected override void BuildEffectGraph(EffectGraph effectGraph) { @@ -87,7 +74,7 @@ public abstract class PixelShaderEffect : CanvasEffect /// protected override void ConfigureEffectGraph(EffectGraph effectGraph) { - effectGraph.GetNode(Effect).ConstantBuffer = this.factory(ElapsedTime, ScreenWidth, ScreenHeight); + effectGraph.GetNode(Effect).ConstantBuffer = factory(ElapsedTime, ScreenWidth, ScreenHeight); } /// diff --git a/samples/ComputeSharp.SwapChain.WinUI/ViewModels/ShaderRunnerViewModel.cs b/samples/ComputeSharp.SwapChain.WinUI/ViewModels/ShaderRunnerViewModel.cs index db930863..443689ec 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ViewModels/ShaderRunnerViewModel.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/ViewModels/ShaderRunnerViewModel.cs @@ -7,35 +7,28 @@ namespace ComputeSharp.SwapChain.Core.ViewModels; /// /// A viewmodel for a compute shader. /// -public sealed partial class ShaderRunnerViewModel : ObservableObject +/// The name of the shader type to execute. +/// The instance to execute. +/// The instance to execute. +public sealed partial class ShaderRunnerViewModel( + string shaderType, + IShaderRunner shaderRunner, + PixelShaderEffect pixelShaderEffect) : ObservableObject { - /// - /// Creates a new instance. - /// - /// The name of the shader type to execute. - /// The instance to execute. - /// The instance to execute. - public ShaderRunnerViewModel(string shaderType, IShaderRunner shaderRunner, PixelShaderEffect pixelShaderEffect) - { - ShaderType = shaderType; - ShaderRunner = shaderRunner; - PixelShaderEffect = pixelShaderEffect; - } - /// /// Gets the name of the shader type to execute. /// - public string ShaderType { get; } + public string ShaderType => shaderType; /// /// Gets the instance to execute. /// - public IShaderRunner ShaderRunner { get; } + public IShaderRunner ShaderRunner => shaderRunner; /// /// Gets the instance to execute. /// - public PixelShaderEffect PixelShaderEffect { get; } + public PixelShaderEffect PixelShaderEffect => pixelShaderEffect; /// /// Gets or sets whether the current shader is selected. diff --git a/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs index d56ae268..080820a5 100644 --- a/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs @@ -8,13 +8,9 @@ namespace ComputeSharp.SwapChain.Backend; /// /// A class representing a simple Win32 application. /// -internal sealed unsafe class Win32Application +/// The function pointer used to run a shader on a target texture. +internal sealed unsafe class Win32Application(delegate*, TimeSpan, void> shaderCallback) { - /// - /// The function pointer used to run a shader on a target texture. - /// - private readonly delegate*, TimeSpan, void> shaderCallback; - /// /// The pointer for the device currently in use. /// @@ -75,15 +71,6 @@ internal sealed unsafe class Win32Application /// private volatile bool isResizePending; - /// - /// Creates a new instance with the specified parameters. - /// - /// The function pointer used to run a shader on a target texture. - public Win32Application(delegate*, TimeSpan, void> shaderCallback) - { - this.shaderCallback = shaderCallback; - } - /// /// Initializes the current application. /// @@ -241,7 +228,7 @@ internal sealed unsafe class Win32Application } // Generate the new frame by invoking the callback - this.shaderCallback(this.texture!, time); + shaderCallback(this.texture!, time); using ComPtr d3D12Resource = default; diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Helpers/TolerantImageComparer.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Helpers/TolerantImageComparer.cs index 8017b876..c220ead5 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Helpers/TolerantImageComparer.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Helpers/TolerantImageComparer.cs @@ -87,31 +87,17 @@ internal static class TolerantImageComparer /// /// A simple model repressenting info about a difference between two pixels. /// - private readonly struct PixelDifference + private readonly struct PixelDifference(Point position, Color expected, Color actual) { - public PixelDifference(Point position, Color expected, Color actual) - { - this.Position = position; - this.RedDifference = actual.R - expected.R; - this.GreenDifference = actual.G - expected.G; - this.BlueDifference = actual.B - expected.B; - this.AlphaDifference = actual.A - expected.A; - } - - public readonly Point Position; - - public readonly int RedDifference; - - public readonly int GreenDifference; - - public readonly int BlueDifference; - - public readonly int AlphaDifference; + private readonly int redDifference = actual.R - expected.R; + private readonly int greenDifference = actual.G - expected.G; + private readonly int blueDifference = actual.B - expected.B; + private readonly int alphaDifference = actual.A - expected.A; /// public override string ToString() { - return $"[delta({this.RedDifference},{this.GreenDifference},{this.BlueDifference},{this.AlphaDifference}) @ ({this.Position.X},{this.Position.Y})]"; + return $"[delta({this.redDifference},{this.greenDifference},{this.blueDifference},{this.alphaDifference}) @ ({position.X},{position.Y})]"; } } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs index 1014107b..f434e703 100644 --- a/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalDataAttribute.cs @@ -5,20 +5,12 @@ namespace ComputeSharp.Tests.Attributes; /// /// An attribute to use with adding additional arbitrary input data. /// +/// The additional input data. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] -public sealed class AdditionalDataAttribute : Attribute +public sealed class AdditionalDataAttribute(params object[] data) : Attribute { - /// - /// Creates a new instance with the specified parameters. - /// - /// The additional input data. - public AdditionalDataAttribute(params object[] data) - { - Data = data; - } - /// /// Gets the additional input data for the current attribute. /// - public object[] Data { get; } + public object[] Data => data; } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs index 70936ae0..83c12feb 100644 --- a/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/AdditionalResourceAttribute.cs @@ -5,20 +5,12 @@ namespace ComputeSharp.Tests.Attributes; /// /// An attribute to use with targeting an additional specific resource type. /// +/// The additional resource type. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] -public sealed class AdditionalResourceAttribute : Attribute +public sealed class AdditionalResourceAttribute(Type type) : Attribute { - /// - /// Creates a new instance with the specified parameters. - /// - /// The additional resource type. - public AdditionalResourceAttribute(Type type) - { - Type = type; - } - /// /// Gets the additional resource type for the current attribute. /// - public Type Type { get; } + public Type Type => type; } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs index c8a133b3..4b983fe3 100644 --- a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs @@ -5,20 +5,12 @@ namespace ComputeSharp.Tests.Attributes; /// /// An attribute to use with adding arbitrary input data. /// +/// The input data. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] -public sealed class DataAttribute : Attribute +public sealed class DataAttribute(params object[] data) : Attribute { - /// - /// Creates a new instance with the specified parameters. - /// - /// The input data. - public DataAttribute(params object[] data) - { - Data = data; - } - /// /// Gets the input data for the current attribute. /// - public object[] Data { get; } + public object[] Data => data; } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs index 2aad8c70..5071db14 100644 --- a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs @@ -6,20 +6,12 @@ namespace ComputeSharp.Tests.Attributes; /// /// An attribute to use with adding a specific input. /// +/// The target device type to use.. [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] -public sealed class DeviceAttribute : Attribute +public sealed class DeviceAttribute(Device device) : Attribute { - /// - /// Creates a new instance with the specified parameters. - /// - /// The target device type to use.. - public DeviceAttribute(Device device) - { - Device = device; - } - /// /// Gets the target to use. /// - public Device Device { get; } + public Device Device => device; } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs index 1498ebf9..8be863c5 100644 --- a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs +++ b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs @@ -5,20 +5,12 @@ namespace ComputeSharp.Tests.Attributes; /// /// An attribute to use with targeting a specific resource type. /// +/// The resource type. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] -public sealed class ResourceAttribute : Attribute +public sealed class ResourceAttribute(Type type) : Attribute { - /// - /// Creates a new instance with the specified parameters. - /// - /// The resource type. - public ResourceAttribute(Type type) - { - Type = type; - } - /// /// Gets the resource type for the current attribute. /// - public Type Type { get; } + public Type Type => type; } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs b/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs index 904a0997..9e84f79d 100644 --- a/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs +++ b/tests/ComputeSharp.Tests/Helpers/TolerantImageComparer.cs @@ -117,31 +117,17 @@ internal static class TolerantImageComparer /// /// A simple model repressenting info about a difference between two pixels. /// - private readonly struct PixelDifference + private readonly struct PixelDifference(Point position, ImageSharpRgba32 expected, ImageSharpRgba32 actual) { - public PixelDifference(Point position, ImageSharpRgba32 expected, ImageSharpRgba32 actual) - { - this.Position = position; - this.RedDifference = actual.R - expected.R; - this.GreenDifference = actual.G - expected.G; - this.BlueDifference = actual.B - expected.B; - this.AlphaDifference = actual.A - expected.A; - } - - public readonly Point Position; - - public readonly int RedDifference; - - public readonly int GreenDifference; - - public readonly int BlueDifference; - - public readonly int AlphaDifference; + private readonly int redDifference = actual.R - expected.R; + private readonly int greenDifference = actual.G - expected.G; + private readonly int blueDifference = actual.B - expected.B; + private readonly int alphaDifference = actual.A - expected.A; /// public override string ToString() { - return $"[delta({this.RedDifference},{this.GreenDifference},{this.BlueDifference},{this.AlphaDifference}) @ ({this.Position.X},{this.Position.Y})]"; + return $"[delta({this.redDifference},{this.greenDifference},{this.blueDifference},{this.alphaDifference}) @ ({position.X},{position.Y})]"; } } } \ No newline at end of file