diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs index 0e106017..25764282 100644 --- a/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs @@ -38,7 +38,7 @@ namespace ComputeSharp.D2D1.Uwp.Buffers; /// if one is needed. For unrealized effects, the resource is and only the wrapper part is used. /// /// -internal unsafe struct SourceReference +internal unsafe struct SourceReference : IDisposable { /// /// The produced by calling @@ -107,6 +107,8 @@ internal unsafe struct SourceReference [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetWrapper(IGraphicsEffectSource? wrapper) { + this.d2D1ImageSource.Dispose(); + this.graphicsEffectSourceWrapper = wrapper; } @@ -151,4 +153,12 @@ internal unsafe struct SourceReference return this.graphicsEffectSourceWrapper; } + + /// + public void Dispose() + { + this.d2D1ImageSource.Dispose(); + this.d2D1EffectDpiCompensation.Dispose(); + this.graphicsEffectSourceWrapper = null; + } } \ 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 59f78ae1..88822c3c 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -2,18 +2,13 @@ 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; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; -using ComputeSharp.D2D1.Uwp.Buffers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; -using Windows.Graphics.Effects; -using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS; -using Win32 = TerraFX.Interop.Windows.Windows; #pragma warning disable CS0618 @@ -91,146 +86,6 @@ internal static unsafe class ID2D1EffectExtensions return (D2D1TransformMapper)GCHandle.FromIntPtr(handlePtr).Target!; } - /// - /// Gets the source from a given object, at a specified index. - /// - /// 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 SourceReference source, - int index) - { - 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()); - } - - /// - /// Sets the source for a given object, at a specified index. - /// - /// The input instance. - /// The realization device currently in use. - /// The device context currently in use. - /// The realization flags to use. - /// The target DPI value for the source. - /// The input value to set. - /// The target value, for caching. - /// The index of the source to set. - /// Whether the operation succeeded (if it didn't the effect should be unrealized). - public static bool TrySetSource( - this ref ID2D1Effect d2D1Effect, - ICanvasDevice* canvasDevice, - ID2D1DeviceContext* deviceContext, - WIN2D_GET_D2D_IMAGE_FLAGS flags, - float targetDpi, - IGraphicsEffectSource? value, - ref SourceReference source, - int index) - { - using ComPtr d2D1Image = default; - - float realizedDpi = 0; - - if (value is not null) - { - using ComPtr canvasImageUnknown = default; - - // Get the underlying IUnknown object for the input source - canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value)); - - using ComPtr canvasImageInterop = default; - - // Try to get the ICanvasImageInterop interface from the input source - HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf()); - - if (!Win32.SUCCEEDED(hresult)) - { - // If unrealization isn't requested for failures, just throw - if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop)."); - } - - return false; - } - - using ComPtr sourceCanvasDevice = default; - - // Get the canvas device the source is realized on, if any - canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf()).Assert(); - - // If a device was retrieved, it must be the same as the input one. If the source has no device, - // that is fine, as it just means that its underlying D2D image has not been realized yet. - if (sourceCanvasDevice.Get() is not null && !canvasDevice->IsSameInstance(sourceCanvasDevice.Get())) - { - if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device."); - } - - return false; - } - - // Try to realize the image from the input source - hresult = canvasImageInterop.Get()->GetD2DImage( - device: canvasDevice, - deviceContext: deviceContext, - flags: flags, - targetDpi: targetDpi, - realizeDpi: &realizedDpi, - ppImage: d2D1Image.GetAddressOf()); - - // Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception - if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - hresult.Assert(); - } - } - else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) - { - // If the source is null and this is not explicitly allowed, the invocation is not valid - ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null."); - } - - // Save the managed wrapper and realized image - source.SetWrapperAndResource(value, d2D1Image.Get()); - - // TODO: handle DPI compensation - - // Set the actual effect input - d2D1Effect.SetInput(index: (uint)index, input: d2D1Image.Get()); - - return true; - } - /// /// Gets the instance from a given object. /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index e2afddb4..957d6c4a 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -8,6 +8,7 @@ using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +using Windows.Graphics.Effects; using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS; using Win32 = TerraFX.Interop.Windows.Windows; @@ -159,7 +160,22 @@ unsafe partial class PixelShaderEffect /// /// Unrealizes the currently realized object. /// - private void Unrealize() + /// The value to set for . + /// The index of the source to get or set. + private void Unrealize(IGraphicsEffectSource source, int index) + { + // Unrealize the effect and indicate to the logic to skip the source at the current index. + // That is, there is no need to marshal it back to the cache, as we're about to do that here. + Unrealize(index); + + // Once the effect is unrealized, also update the cached source managed wrapper + Sources.Storage[index].SetWrapper(source); + } + + /// + /// Unrealizes the currently realized object. + /// + private void Unrealize(int? sourceIndexToSkip = null) { // If there is no ID2D1Effect object, there is nothing left to do if (this.d2D1Effect.Get() is null) @@ -176,7 +192,22 @@ unsafe partial class PixelShaderEffect this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty(); this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty(); - // TODO: read back D2D sources from the effect + // Loop over all effect inputs and update the cache back as well + for (int i = 0; i < 16; i++) + { + // If this index has been explicitly requested to be skipped, just dispose the source reference. + // This is the case when the effect is being unrealized, and callers will set this value on their own. + if (i == sourceIndexToSkip) + { + Sources.Storage[i].Dispose(); + } + else + { + // Otherwise, just get the input, which will automatically update the cached values. The + // returned IGraphicsEffectSource managed wrapper is not used here and is safe to ignore. + _ = GetD2DInput(i); + } + } // Finally release the effect as well this.d2D1Effect.Dispose(); diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs new file mode 100644 index 00000000..6eb2d18b --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -0,0 +1,211 @@ +using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Extensions; +using ComputeSharp.D2D1.Helpers; +using ComputeSharp.D2D1.Uwp.Buffers; +using ComputeSharp.D2D1.Uwp.Extensions; +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using Windows.Graphics.Effects; +using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS; +using Win32 = TerraFX.Interop.Windows.Windows; + +namespace ComputeSharp.D2D1.Uwp; + +/// +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) + { + return this.d2D1Effect.Get() switch + { + not null => GetD2DInput(index), + _ => Sources.Storage[index].GetWrapper() + }; + } + } + + /// + /// Sets the marshalled value for . + /// + /// The value to set for . + /// The index of the source to get or set. + private void SetSource(IGraphicsEffectSource? value, int index) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + lock (this.lockObject) + { + ref SourceReference source = ref Sources.Storage[index]; + + if (this.d2D1Effect.Get() is not null) + { + const WIN2D_GET_D2D_IMAGE_FLAGS flags = + WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION | + WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS | + WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE; + + // The WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE flag is passed, so we just let the effect + // unrealize in case of failure and take no other action (including throwing) if that happens. + _ = SetD2DInput(index, value, flags, targetDpi: 0.0f, d2D1DeviceContext: null); + } + else + { + source.SetWrapper(value); + } + } + } + + /// This method assumes a lock is taken and the effect is already realized. + /// + private IGraphicsEffectSource? GetD2DInput(int index) + { + using ComPtr d2D1Image = default; + + // Get the ID2D1Image input from the effect + this.d2D1Effect.Get()->GetInput( + index: (uint)index, + input: d2D1Image.GetAddressOf()); + + ref SourceReference sourceReference = ref Sources.Storage[index]; + + // Check whether the input had a DPI compensation effect added to it, and skip it if so + if (sourceReference.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(sourceReference.GetDpiCompensationEffect())) + { + sourceReference.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf()); + } + else + { + sourceReference.SetDpiCompensationEffect(null); + } + } + + // Get or create a wrapper for the input image + return sourceReference.GetOrCreateWrapper(null, d2D1Image.Get()); + } + + /// The index of the source to get or set. + /// The value to set for . + /// The flags for . + /// The target DPI for the realized image, if present. + /// The input object to use, if present. + /// Whether the input was set successfully or not. + /// This method assumes a lock is taken and the effect is already realized. + /// + private bool SetD2DInput( + int index, + IGraphicsEffectSource? value, + WIN2D_GET_D2D_IMAGE_FLAGS flags, + float targetDpi, + ID2D1DeviceContext* d2D1DeviceContext) + { + using ComPtr d2D1Image = default; + + float realizedDpi = 0; + + if (value is not null) + { + using ComPtr canvasImageUnknown = default; + + // Get the underlying IUnknown object for the input source + canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value)); + + using ComPtr canvasImageInterop = default; + + // Try to get the ICanvasImageInterop interface from the input source + HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf()); + + if (!Win32.SUCCEEDED(hresult)) + { + // Only potentially handle E_NOINTERFACE, always throw in other cases + if (hresult != E.E_NOINTERFACE) + { + Marshal.ThrowExceptionForHR(hresult); + } + + // If unrealization isn't requested for failures, just throw + if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) + { + ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop)."); + } + + Unrealize(value, index); + + return false; + } + + using ComPtr sourceCanvasDevice = default; + + // Get the canvas device the source is realized on, if any + canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf()).Assert(); + + // If a device was retrieved, it must be the same as the input one. If the source has no device, + // that is fine, as it just means that its underlying D2D image has not been realized yet. + if (sourceCanvasDevice.Get() is not null && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get())) + { + if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) + { + ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device."); + } + + Unrealize(value, index); + + return false; + } + + // Try to realize the image from the input source (this will recurse through the effect graph) + hresult = canvasImageInterop.Get()->GetD2DImage( + device: this.canvasDevice.Get(), + deviceContext: d2D1DeviceContext, + flags: flags, + targetDpi: targetDpi, + realizeDpi: &realizedDpi, + ppImage: d2D1Image.GetAddressOf()); + + // Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception + if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) + { + hresult.Assert(); + } + + // If no realized image is available, unrealize the effect + if (d2D1Image.Get() is null) + { + Unrealize(value, index); + + return false; + } + } + else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE) + { + // If the source is null and this is not explicitly allowed, the invocation is not valid + ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null."); + } + + // Save the managed wrapper and realized image + Sources.Storage[index].SetWrapperAndResource(value, d2D1Image.Get()); + + // TODO: handle DPI compensation + + // Set the actual effect input + this.d2D1Effect.Get()->SetInput(index: (uint)index, input: d2D1Image.Get()); + + return true; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs index 3b1f8569..eb9d81d0 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -1,12 +1,8 @@ -using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Interop; -using ComputeSharp.D2D1.Uwp.Buffers; using ComputeSharp.D2D1.Uwp.Extensions; using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; using TerraFX.Interop.DirectX; -using Windows.Graphics.Effects; -using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS; namespace ComputeSharp.D2D1.Uwp; @@ -93,67 +89,6 @@ 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 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() - }; - } - } - - /// - /// Sets the marshalled value for . - /// - /// The value to set for . - /// The index of the source to get or set. - private void SetSource(IGraphicsEffectSource? value, int index) - { - using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - - lock (this.lockObject) - { - ref SourceReference source = ref Sources.Storage[index]; - - if (this.d2D1Effect.Get() is not null) - { - const WIN2D_GET_D2D_IMAGE_FLAGS sourceFlags = - WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION | - WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS | - WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE; - - // Try to set the source, and unrealize if the operation failed - if (!this.d2D1Effect.Get()->TrySetSource( - canvasDevice: this.canvasDevice.Get(), - deviceContext: null, - flags: sourceFlags, - targetDpi: 0, - value: value, - source: ref source, - index: index)) - { - Unrealize(); - } - } - else - { - source.SetWrapper(value); - } - } - } - /// /// Gets the marshalled value for . ///