diff --git a/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems b/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems index e130179f..d973ffe7 100644 --- a/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems +++ b/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems @@ -11,6 +11,7 @@ + diff --git a/src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/DisallowNullAttribute.cs b/src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/DisallowNullAttribute.cs new file mode 100644 index 00000000..a584aa30 --- /dev/null +++ b/src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/DisallowNullAttribute.cs @@ -0,0 +1,13 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Specifies that null is disallowed as an input even if the corresponding type allows it. +/// +[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] +internal sealed class DisallowNullAttribute : Attribute +{ +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs index 38968217..7111671b 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -4,6 +4,9 @@ using System; using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.DirectX; using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; +using TerraFX.Interop.Windows; +using System.Runtime.InteropServices; #pragma warning disable CS0618 @@ -17,6 +20,7 @@ internal static unsafe class ID2D1EffectExtensions /// /// Gets the constant buffer of type from a given object. /// + /// The type of shader being used. /// The input instance. /// The constant buffer of type for . public static T GetConstantBuffer(this ref ID2D1Effect d2D1Effect) @@ -42,6 +46,44 @@ internal static unsafe class ID2D1EffectExtensions return shader; } + /// + /// Gets the instance from a given object. + /// + /// The type of shader being used. + /// The input instance. + /// The instance for . + public static D2D1TransformMapper? GetTransformMapper(this ref ID2D1Effect d2D1Effect) + where T : unmanaged, ID2D1PixelShader + { + using ComPtr d2D1TransformMapper = default; + + // Get the ID2D1TransformMapper object from the effect + d2D1Effect.GetValue( + index: D2D1PixelShaderEffectProperty.TransformMapper, + type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, + data: (byte*)d2D1TransformMapper.GetAddressOf(), + dataSize: (uint)sizeof(void*)).Assert(); + + // If there is no transform, just return null (this is the default state) + if (d2D1TransformMapper.Get() is null) + { + return null; + } + + // Otherwise, check if it's an internal CCW and retrieve the managed wrapper + using ComPtr d2D1TransformMapperInternal = default; + + d2D1TransformMapper.CopyTo(d2D1TransformMapperInternal.GetAddressOf()).Assert(); + + IntPtr handlePtr; + + // Get the underlying GCHandle value + d2D1TransformMapperInternal.Get()->GetManagedWrapperHandle((void**)&handlePtr).Assert(); + + // Retrieve the managed wrapper from the GCHandle + return (D2D1TransformMapper)GCHandle.FromIntPtr(handlePtr).Target!; + } + /// /// Gets the property for a given object. /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index cce1ce96..01db5a66 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -168,6 +168,7 @@ unsafe partial class PixelShaderEffect // Cache the last update values for the constant buffer, cache output and buffer precision properties this.value = this.d2D1Effect.Get()->GetConstantBuffer(); + this.transformMapper = this.d2D1Effect.Get()->GetTransformMapper(); this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); @@ -221,6 +222,12 @@ unsafe partial class PixelShaderEffect // Set the constant buffer for the effect D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.value, this.d2D1Effect.Get()); + // If there is a transform mapper, set it in the effect + if (this.transformMapper is not null) + { + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(this.d2D1Effect.Get(), this.transformMapper); + } + // If cache input has been set, forward that to the new effect if (this.cacheOutput) { diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs index 324c7a06..24e3f12d 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -50,6 +50,45 @@ unsafe partial class PixelShaderEffect } } + /// + /// Gets the marshalled value for . + /// + /// The marshalled value for . + private D2D1TransformMapper? GetTransformMapper() + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + return this.d2D1Effect.Get() switch + { + not null => this.d2D1Effect.Get()->GetTransformMapper(), + _ => this.transformMapper + }; + } + } + + /// + /// Sets the marshalled value for . + /// + /// The value to set for . + private void SetTransformMapper(D2D1TransformMapper value) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + if (this.d2D1Effect.Get() is not null) + { + D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(this.d2D1Effect.Get(), value); + } + else + { + this.transformMapper = value; + } + } + } + /// /// Gets the marshalled value for . /// @@ -78,7 +117,6 @@ unsafe partial class PixelShaderEffect lock (this.lockObject) { - // If the effect is realized, set the property on the underlying ID2D1Effect object if (this.d2D1Effect.Get() is not null) { this.d2D1Effect.Get()->SetCachedProperty(value); diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs index 92d56049..2fc34204 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics.CodeAnalysis; +using ComputeSharp.D2D1.Interop; using Microsoft.Graphics.Canvas; using Windows.Graphics.Effects; @@ -17,9 +19,15 @@ partial class PixelShaderEffect } /// - /// Gets the object associated with the current instance. + /// Gets or sets the instance to use for the effect, if any. /// - public SourceCollection Sources { get; } = new(); + /// Thrown if the value is set to . + [DisallowNull] + public D2D1TransformMapper? TransformMapper + { + get => GetTransformMapper(); + set => SetTransformMapper(value); + } /// /// Gets or sets a value indicating whether to enable caching the output from drawing this effect. @@ -40,6 +48,11 @@ partial class PixelShaderEffect set => SetBufferPrecision(value); } + /// + /// Gets the object associated with the current instance. + /// + public SourceCollection Sources { get; } = new(); + /// /// Represents the collection of sources in a instance. /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index 1b3a1754..b98ef02e 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -1,4 +1,5 @@ using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Interop; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; using TerraFX.Interop.DirectX; @@ -48,6 +49,11 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// private T value; + /// + /// The backing field for . + /// + private D2D1TransformMapper? transformMapper; + /// /// The backing field for . ///