From a32fc9ec646b7bd02fe8737aff8708b543330601 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Oct 2022 02:21:44 +0200 Subject: [PATCH] More code refactoring --- .../PixelShaderEffect{T}.ICanvasImage.cs | 22 +++ ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 4 +- ...PixelShaderEffect{T}.Properties.Interop.cs | 108 +++++++++++++++ .../PixelShaderEffect{T}.Properties.cs | 71 ++++++++++ .../PixelShaderEffect{T}.cs | 129 ------------------ 5 files changed, 203 insertions(+), 131 deletions(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs create mode 100644 src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs create mode 100644 src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs new file mode 100644 index 00000000..8c026cc7 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs @@ -0,0 +1,22 @@ +using System; +using System.Numerics; +using Microsoft.Graphics.Canvas; +using Windows.Foundation; + +namespace ComputeSharp.D2D1.Uwp; + +/// +partial class PixelShaderEffect +{ + /// + Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator) + { + throw new NotSupportedException(); + } + + /// + Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) + { + throw new NotSupportedException(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index f7235629..d4652e8c 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -248,7 +248,7 @@ unsafe partial class PixelShaderEffect // TODO: port base CanvasEffect::Realize logic // Also set the current constants for the effect - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(Value, this.d2D1Effect.Get()); } /// @@ -259,7 +259,7 @@ unsafe partial class PixelShaderEffect /// The instance in use. private void RefreshInputs(CanvasImageGetD2DImageFlags flags, float targetDpi, ID2D1DeviceContext* deviceContext) { - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(Value, this.d2D1Effect.Get()); // TODO } diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs new file mode 100644 index 00000000..e5a12ea1 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -0,0 +1,108 @@ +using ComputeSharp.D2D1.Uwp.Extensions; +using ComputeSharp.Interop; +using Microsoft.Graphics.Canvas; +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.D2D1.Uwp; + +/// +unsafe partial class PixelShaderEffect +{ + /// + /// Gets the marshalled value for . + /// + /// The marshalled value for . + private bool GetCacheOutput() + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // If the effect is realized, get the latest value from it + if (this.d2D1Effect.Get() is not null) + { + this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); + } + + return this.cacheOutput; + } + } + + /// + /// Sets the marshalled value for . + /// + /// The value to set for . + private void SetCacheOutput(bool value) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + this.cacheOutput = value; + + // If the effect is realized, set the property on the underlying ID2D1Effect object too + if (this.d2D1Effect.Get() is not null) + { + this.d2D1Effect.Get()->SetCachedProperty(value); + } + } + } + + /// + /// Gets the marshalled value for . + /// + /// The marshalled value for . + private CanvasBufferPrecision? GetBufferPrecision() + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // If the effect is realized, get the latest value from it + if (this.d2D1Effect.Get() is not null) + { + this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); + } + + // Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN + return this.d2D1BufferPrecision switch + { + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM => CanvasBufferPrecision.Precision8UIntNormalized, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB => CanvasBufferPrecision.Precision8UIntNormalizedSrgb, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM => CanvasBufferPrecision.Precision16UIntNormalized, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT => CanvasBufferPrecision.Precision16Float, + D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT => CanvasBufferPrecision.Precision32Float, + _ => null + }; + } + } + + /// + /// Sets the marshalled value for . + /// + /// The value to set for . + private void SetBufferPrecision(CanvasBufferPrecision? value) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + // Map back from CanvasBufferPrecision? to D2D1_BUFFER_PRECISION (null results in D2D1_BUFFER_PRECISION_UNKNOWN) + this.d2D1BufferPrecision = value switch + { + CanvasBufferPrecision.Precision8UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM, + CanvasBufferPrecision.Precision8UIntNormalizedSrgb => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB, + CanvasBufferPrecision.Precision16UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM, + CanvasBufferPrecision.Precision16Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT, + CanvasBufferPrecision.Precision32Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT, + _ => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN + }; + + // If the effect is realized, set the property on the underlying ID2D1Effect object too + if (this.d2D1Effect.Get() is not null) + { + this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision); + } + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs new file mode 100644 index 00000000..2042522d --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs @@ -0,0 +1,71 @@ +using System; +using ComputeSharp.D2D1.Interop.Effects; +using Microsoft.Graphics.Canvas; +using Windows.Graphics.Effects; + +namespace ComputeSharp.D2D1.Uwp; + +/// +partial class PixelShaderEffect +{ + /// + /// Configures the to use for effects of type . + /// + /// The factory of instances to use for each created effect. + /// Thrown if initialization is attempted with a mismatched transform factory. + /// + /// Initialization can only be done once, and is shared across all effects of type . + /// Initializing an effect is not necessary before using it: if no transform has been set, the default one will be used. + /// + public static void ConfigureD2D1TransformMapperFactory(ID2D1TransformMapperFactory? d2D1DrawTransformMapperFactory) + { + PixelShaderEffect.For.Initialize(d2D1DrawTransformMapperFactory); + } + + /// + /// Gets or sets the current value in use. + /// + public T Value { get; set; } + + /// + /// Gets the object associated with the current instance. + /// + public SourceCollection Sources { get; } = new(); + + /// + /// Gets or sets a value indicating whether to enable caching the output from drawing this effect. + /// + public bool CacheOutput + { + get => GetCacheOutput(); + set => SetCacheOutput(value); + } + + /// + /// Gets or sets the precision to use for intermediate buffers when drawing this effect. + /// + /// If , the default precision for effects will be used. + public CanvasBufferPrecision? BufferPrecision + { + get => GetBufferPrecision(); + set => SetBufferPrecision(value); + } + + /// + /// Represents the collection of sources in a instance. + /// + public sealed class SourceCollection + { + /// + /// Gets or sets the source at a specified index. + /// + /// The index of the source to get or set. + /// The source at the specified index. + /// Thrown if is not a valid index for the current effect. + public IGraphicsEffectSource this[int index] + { + get => null!; + set { } + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index 2069042c..68e11821 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -1,11 +1,6 @@ -using System; -using System.Numerics; using ABI.Microsoft.Graphics.Canvas; -using ComputeSharp.D2D1.Interop.Effects; -using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; -using Windows.Foundation; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -57,128 +52,4 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// The backing field for . /// private D2D1_BUFFER_PRECISION d2D1BufferPrecision; - - /// - /// The current value in use. - /// - public T Value; - - /// - /// Configures the to use for effects of type . - /// - /// The factory of instances to use for each created effect. - /// Thrown if initialization is attempted with a mismatched transform factory. - /// - /// Initialization can only be done once, and is shared across all effects of type . - /// Initializing an effect is not necessary before using it: if no transform has been set, the default one will be used. - /// - public static void ConfigureD2D1TransformMapperFactory(ID2D1TransformMapperFactory? d2D1DrawTransformMapperFactory) - { - PixelShaderEffect.For.Initialize(d2D1DrawTransformMapperFactory); - } - - /// - /// Gets or sets a value indicating whether to enable caching the output from drawing this effect. - /// - public unsafe bool CacheOutput - { - get - { - using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - - lock (this.lockObject) - { - // If the effect is realized, get the latest value from it - if (this.d2D1Effect.Get() is not null) - { - this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); - } - - return this.cacheOutput; - } - } - set - { - using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - - lock (this.lockObject) - { - this.cacheOutput = value; - - // If the effect is realized, set the property on the underlying ID2D1Effect object too - if (this.d2D1Effect.Get() is not null) - { - this.d2D1Effect.Get()->SetCachedProperty(value); - } - } - } - } - - /// - /// Gets or sets the precision to use for intermediate buffers when drawing this effect. - /// - /// If , the default precision for effects will be used. - public unsafe CanvasBufferPrecision? BufferPrecision - { - get - { - using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - - lock (this.lockObject) - { - // If the effect is realized, get the latest value from it - if (this.d2D1Effect.Get() is not null) - { - this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); - } - - // Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN - return this.d2D1BufferPrecision switch - { - D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM => CanvasBufferPrecision.Precision8UIntNormalized, - D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB => CanvasBufferPrecision.Precision8UIntNormalizedSrgb, - D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM => CanvasBufferPrecision.Precision16UIntNormalized, - D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT => CanvasBufferPrecision.Precision16Float, - D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT => CanvasBufferPrecision.Precision32Float, - _ => null - }; - } - } - set - { - using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - - lock (this.lockObject) - { - // Map back from CanvasBufferPrecision? to D2D1_BUFFER_PRECISION (null results in D2D1_BUFFER_PRECISION_UNKNOWN) - this.d2D1BufferPrecision = value switch - { - CanvasBufferPrecision.Precision8UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM, - CanvasBufferPrecision.Precision8UIntNormalizedSrgb => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB, - CanvasBufferPrecision.Precision16UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM, - CanvasBufferPrecision.Precision16Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT, - CanvasBufferPrecision.Precision32Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT, - _ => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN - }; - - // If the effect is realized, set the property on the underlying ID2D1Effect object too - if (this.d2D1Effect.Get() is not null) - { - this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision); - } - } - } - } - - /// - Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator) - { - throw new NotSupportedException(); - } - - /// - Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) - { - throw new NotSupportedException(); - } } \ No newline at end of file