diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs index 9cdc9de8..d73d7534 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs @@ -24,7 +24,7 @@ internal static unsafe class ICanvasDeviceExtensions // Get the ICanvasResourceWrapperNative wrapper around the input device canvasDevice.QueryInterface( riid: Win32.__uuidof(), - ppvObject: (void**)canvasDeviceResourceWrapperNative.GetAddressOf()).Assert(); + ppvObject: canvasDeviceResourceWrapperNative.GetVoidAddressOf()).Assert(); // Get the underlying ID2D1Device1 instance in use HRESULT hresult = canvasDeviceResourceWrapperNative.Get()->GetNativeResource( diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs index c36f62bb..736d3fb0 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -2,6 +2,7 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; @@ -9,6 +10,7 @@ using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +using Windows.Graphics.Effects; #pragma warning disable CS0618 @@ -86,6 +88,50 @@ internal static unsafe class ID2D1EffectExtensions return (D2D1TransformMapper)GCHandle.FromIntPtr(handlePtr).Target!; } + /// + /// Gets the source from a given object, at a specified index. + /// + /// The type of shader being used. + /// The input instance. + /// The realization device currently in use. + /// The input object for the target index. + /// The index for the source to retrieve. + /// The source from a given object, at a specified index. + public static IGraphicsEffectSource? GetSource( + this ref ID2D1Effect d2D1Effect, + ICanvasDevice* canvasDevice, + ref PixelShaderEffect.SourceCollection.SourceReference source, + int index) + where T : unmanaged, ID2D1PixelShader + { + using ComPtr d2D1Image = default; + + // Get the ID2D1Image input from the effect + d2D1Effect.GetInput( + index: (uint)index, + input: d2D1Image.GetAddressOf()); + + // Check whether the input had a DPI compensation effect added to it, and skip it if so + if (source.HasDpiCompensationEffect) + { + // Since a realized effect is the only source of authority for the effect state, we also + // need to check that the actual source is in fact the same that was previous set by the + // managed wrapper. If it is, we get the input and return that (ie. the underlying image). + // If not, we just return it directly and reset the locally stored DPI compensation effect. + if (d2D1Image.Get()->IsSameInstance(source.GetDpiCompensationEffect())) + { + source.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf()); + } + else + { + source.SetDpiCompensationEffect(null); + } + } + + // Get or create a wrapper for the input image + return source.GetOrCreateWrapper(null, d2D1Image.Get()); + } + /// /// Gets the instance from a given object. /// @@ -121,7 +167,7 @@ internal static unsafe class ID2D1EffectExtensions source.GetD2D1ResourceTextureManager(d2D1ResourceTextureManagerSource.GetAddressOf()); // Check if the instances match, and return the managed wrapper we already have - if (d2D1ResourceTextureManager.IsSameInstance(in d2D1ResourceTextureManagerSource)) + if (d2D1ResourceTextureManager.Get()->IsSameInstance(d2D1ResourceTextureManagerSource.Get())) { return source; } diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/IUnknownExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/IUnknownExtensions.cs index 75fcb82d..8b1e1b0c 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/IUnknownExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/IUnknownExtensions.cs @@ -35,8 +35,8 @@ internal static class IUnknownExtensions using ComPtr leftUnknown = default; using ComPtr rightUnknown = default; - ((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof(), (void**)leftUnknown.GetAddressOf()).Assert(); - ((IUnknown*)right)->QueryInterface(Win32.__uuidof(), (void**)rightUnknown.GetAddressOf()).Assert(); + ((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof(), leftUnknown.GetVoidAddressOf()).Assert(); + ((IUnknown*)right)->QueryInterface(Win32.__uuidof(), rightUnknown.GetVoidAddressOf()).Assert(); return leftUnknown.Get() == rightUnknown.Get(); } diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs index eb9d81d0..5e3b1847 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -3,6 +3,7 @@ using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; using TerraFX.Interop.DirectX; +using Windows.Graphics.Effects; namespace ComputeSharp.D2D1.Uwp; @@ -89,6 +90,27 @@ unsafe partial class PixelShaderEffect } } + /// + /// Gets the marshalled value for . + /// + /// The index of the source to get or set. + /// The marshalled value for . + private IGraphicsEffectSource? GetSource(int index) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + ref SourceCollection.SourceReference source = ref Sources.Storage[index]; + + return this.d2D1Effect.Get() switch + { + not null => this.d2D1Effect.Get()->GetSource(this.canvasDevice.Get(), ref source, index), + _ => source.GetWrapper() + }; + } + } + /// /// Gets the marshalled value for . /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs index c1d9654a..938dced2 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs @@ -1,7 +1,11 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Helpers; +using ComputeSharp.D2D1.Uwp.Extensions; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; using Windows.Graphics.Effects; namespace ComputeSharp.D2D1.Uwp; @@ -56,93 +60,93 @@ partial class PixelShaderEffect internal struct FixedBuffer { /// - /// The instance at index 0. + /// The instance at index 0. /// - public IGraphicsEffectSource? Source0; + public SourceReference Source0; /// - /// The instance at index 1. + /// The instance at index 1. /// - public IGraphicsEffectSource? Source1; + public SourceReference Source1; /// - /// The instance at index 2. + /// The instance at index 2. /// - public IGraphicsEffectSource? Source2; + public SourceReference Source2; /// - /// The instance at index 3. + /// The instance at index 3. /// - public IGraphicsEffectSource? Source3; + public SourceReference Source3; /// - /// The instance at index 4. + /// The instance at index 4. /// - public IGraphicsEffectSource? Source4; + public SourceReference Source4; /// - /// The instance at index 5. + /// The instance at index 5. /// - public IGraphicsEffectSource? Source5; + public SourceReference Source5; /// - /// The instance at index 6. + /// The instance at index 6. /// - public IGraphicsEffectSource? Source6; + public SourceReference Source6; /// - /// The instance at index 7. + /// The instance at index 7. /// - public IGraphicsEffectSource? Source7; + public SourceReference Source7; /// - /// The instance at index 8. + /// The instance at index 8. /// - public IGraphicsEffectSource? Source8; + public SourceReference Source8; /// - /// The instance at index 9. + /// The instance at index 9. /// - public IGraphicsEffectSource? Source9; + public SourceReference Source9; /// - /// The instance at index 10. + /// The instance at index 10. /// - public IGraphicsEffectSource? Source10; + public SourceReference Source10; /// - /// The instance at index 11. + /// The instance at index 11. /// - public IGraphicsEffectSource? Source11; + public SourceReference Source11; /// - /// The instance at index 12. + /// The instance at index 12. /// - public IGraphicsEffectSource? Source12; + public SourceReference Source12; /// - /// The instance at index 13. + /// The instance at index 13. /// - public IGraphicsEffectSource? Source13; + public SourceReference Source13; /// - /// The instance at index 14. + /// The instance at index 14. /// - public IGraphicsEffectSource? Source14; + public SourceReference Source14; /// - /// The instance at index 15. + /// The instance at index 15. /// - public IGraphicsEffectSource? Source15; + public SourceReference Source15; /// - /// Gets a reference to the value at a given index. + /// Gets a reference to the value at a given index. /// - /// The index of the value to get a reference to. - /// A reference to the value at a given index. + /// The index of the value to get a reference to. + /// A reference to the value at a given index. /// Thrown if is not a valid index for the current effect. [UnscopedRef] - public ref IGraphicsEffectSource? this[int index] + public ref SourceReference this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get @@ -152,12 +156,135 @@ partial class PixelShaderEffect ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); } - ref IGraphicsEffectSource? r0 = ref Unsafe.As(ref this); - ref IGraphicsEffectSource? r1 = ref Unsafe.Add(ref r0, index); + ref SourceReference r0 = ref Unsafe.As(ref this); + ref SourceReference r1 = ref Unsafe.Add(ref r0, index); return ref r1; } } } + + /// + /// A wrapper type for an input and its underlying native D2D images. + /// + /// + /// + /// This type is meant to support these two common operations: + /// + /// Look up a D2D resource from a property of some other D2D interface. + /// Get a managed wrapper from a D2D interface. + /// + /// With this, the generated D2D objects can be associated with each original managed wrapper. Similarly, + /// when the other way around is used, this type can also store the generated wrapper for future use. + /// + /// + /// This gives the following improvements: + /// + /// Improved performance for the common case of repeated calls using the same D2D instance. + /// Maintaining a reference to the wrapper avoids it being repeatedly released and recreated. + /// + /// + /// + /// Just like with all other members of an effect, values in this object are considered authoritative when the + /// effect is not realized, otherwise they just act as a cache to speedup the reverse managed wrapper lookups. + /// + /// + /// For realized effects, tracks both the resource and its + /// wrapper (and optional being the DPI compensation effect, + /// if one is needed. For unrealized effects, the resource is and only the wrapper part is used. + /// + /// + internal unsafe struct SourceReference + { + /// + /// The produced by calling + /// on the input managed wrapper used as source for the effect (ie. ). + /// + private ComPtr d2D1ImageSource; + + /// + /// The for DPI compensation wrapping , if needed. + /// + private ComPtr d2D1EffectDpiCompensation; + + /// + /// The instance being a managed wrapper for the current effect input. + /// + private IGraphicsEffectSource? graphicsEffectSourceWrapper; + + /// + /// Gets whether or not this source ference has a DPI compensation effect. + /// + public readonly bool HasDpiCompensationEffect + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.d2D1EffectDpiCompensation.Get() is not null; + } + + /// + /// Gets the object for the DPI compensation effect used, if any. + /// + /// The object for the DPI compensation effect used, if any. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly ID2D1Effect* GetDpiCompensationEffect() + { + return this.d2D1EffectDpiCompensation.Get(); + } + + /// + /// Sets the DPI compensation effet being used, + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetDpiCompensationEffect(ID2D1Effect* d2D1Effect) + { + _ = this.d2D1EffectDpiCompensation.Reset(); + + if (d2D1Effect is not null) + { + this.d2D1EffectDpiCompensation = new ComPtr(d2D1Effect); + } + } + + /// + /// Gets the instance being a managed wrapper for the current effect input. + /// + /// The instance being a managed wrapper for the current effect input. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly IGraphicsEffectSource? GetWrapper() + { + return this.graphicsEffectSourceWrapper; + } + + /// + /// Gets or creates an wrapper for a given object. + /// + /// The input object the source effect is realized on. + /// The input object to get or create a wrapper for. + /// An wrapper for a given object. + public IGraphicsEffectSource? GetOrCreateWrapper(ICanvasDevice* canvasDevice, ID2D1Image* d2D1Image) + { + // Check if there already is a matching managed wrapper for the input image + if (!this.d2D1ImageSource.Get()->IsSameInstance(d2D1Image)) + { + // If that's not the case, create a new wrapper if the input image is not null + if (d2D1Image is not null) + { + // m_wrapper = ResourceManager::GetOrCreate(device, resource); + throw new NotImplementedException(); + } + else + { + _ = this.d2D1ImageSource.Reset(); + } + + _ = this.d2D1ImageSource.Reset(); + + this.d2D1ImageSource = new ComPtr(d2D1Image); + } + + return this.graphicsEffectSourceWrapper; + } + } } } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs index 1d5dcb4a..43851234 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Effect.cs @@ -32,4 +32,11 @@ internal unsafe partial struct ID2D1Effect { return ((delegate* unmanaged[Stdcall])(lpVtbl[11]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, type, data, dataSize); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(16)] + public void GetInput([NativeTypeName("UINT32")] uint index, ID2D1Image** input) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[16]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, input); + } } \ No newline at end of file