From a91ed7b70d1e02fafd4e7be11003942b965716c6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 3 Dec 2022 03:25:49 +0100 Subject: [PATCH] Implement PixelShaderEffect.SetSource --- .../ICanvasImageInterop.cs | 90 ++++--- .../Buffers/GraphicsEffectSourceBuffer.cs | 117 ++++++++ .../Buffers/ResourceTextureManagerBuffer.cs | 117 ++++++++ .../Buffers/SourceReference.cs | 154 +++++++++++ .../Extensions/ID2D1EffectExtensions.cs | 108 +++++++- ...PixelShaderEffect{T}.Properties.Interop.cs | 44 ++- ...erties.ResourceTextureManagerCollection.cs | 119 +------- ...PixelShaderEffect{T}.Properties.Sources.cs | 253 +----------------- .../DirectX/um/d2d1_1/ID2D1Effect.cs | 7 + 9 files changed, 612 insertions(+), 397 deletions(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs create mode 100644 src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs create mode 100644 src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs index 292d499c..3d6c8e3e 100644 --- a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -18,6 +19,63 @@ internal unsafe struct ICanvasImageInterop { public void** lpVtbl; + /// + /// Gets the device that the effect is currently realized on, if any. + /// + /// The resulting device, if available ((as a marshalled )). + /// The for the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(3)] + public HRESULT GetDevice(ICanvasDevice** device) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device); + } + + /// + /// Gets an from an instance. + /// + /// The input canvas device (as a marshalled ). + /// + /// The device context in use. This value is is optional (but recommended), except when the + /// flag is specified. This is because + /// not all callers of have easy access to a context. It is always + /// possible to get a resource creation context from the device, but the context is only + /// actually necessary if a new effect realization needs to be created, so it is more efficient + /// to have the implementation do this lookup only if/when it turns out to be needed. + /// + /// The flags to use to get the image. + /// + /// The DPI of the target device context. This is used to determine when a D2D1DpiCompensation + /// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values + /// , + /// or + /// + /// + /// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A D2D1DpiCompensation effect + /// will be inserted if and are different (flags permitting). + /// + /// The resulting for the effect. + /// The for the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(4)] + public HRESULT GetD2DImage( + ICanvasDevice* device, + ID2D1DeviceContext* deviceContext, + GetD2DImageFlags flags, + float targetDpi, + float* realizeDpi, + ID2D1Image** ppImage) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[4])( + (ICanvasImageInterop*)Unsafe.AsPointer(ref this), + device, + deviceContext, + flags, + targetDpi, + realizeDpi, + ppImage); + } + /// /// The implementation of . /// @@ -26,40 +84,12 @@ internal unsafe struct ICanvasImageInterop [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface Interface { - /// - /// Gets the device that the effect is currently realized on, if any. - /// - /// The resulting device, if available ((as a marshalled )). - /// The for the operation. + /// [PreserveSig] [return: NativeTypeName("HRESULT")] int GetDevice(ICanvasDevice** device); - /// - /// Gets an from an instance. - /// - /// The input canvas device (as a marshalled ). - /// - /// The device context in use. This value is is optional (but recommended), except when the - /// flag is specified. This is because - /// not all callers of have easy access to a context. It is always - /// possible to get a resource creation context from the device, but the context is only - /// actually necessary if a new effect realization needs to be created, so it is more efficient - /// to have the implementation do this lookup only if/when it turns out to be needed. - /// - /// The flags to use to get the image. - /// - /// The DPI of the target device context. This is used to determine when a D2D1DpiCompensation - /// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values - /// , - /// or - /// - /// - /// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A D2D1DpiCompensation effect - /// will be inserted if and are different (flags permitting). - /// - /// The resulting for the effect. - /// The for the operation. + /// [PreserveSig] [return: NativeTypeName("HRESULT")] int GetD2DImage( diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs new file mode 100644 index 00000000..364e72b4 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/GraphicsEffectSourceBuffer.cs @@ -0,0 +1,117 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1.Helpers; +using Windows.Graphics.Effects; + +namespace ComputeSharp.D2D1.Uwp.Buffers; + +/// +/// A fixed buffer type containing 16 fields. +/// +internal struct GraphicsEffectSourceBuffer +{ + /// + /// The instance at index 0. + /// + public SourceReference Source0; + + /// + /// The instance at index 1. + /// + public SourceReference Source1; + + /// + /// The instance at index 2. + /// + public SourceReference Source2; + + /// + /// The instance at index 3. + /// + public SourceReference Source3; + + /// + /// The instance at index 4. + /// + public SourceReference Source4; + + /// + /// The instance at index 5. + /// + public SourceReference Source5; + + /// + /// The instance at index 6. + /// + public SourceReference Source6; + + /// + /// The instance at index 7. + /// + public SourceReference Source7; + + /// + /// The instance at index 8. + /// + public SourceReference Source8; + + /// + /// The instance at index 9. + /// + public SourceReference Source9; + + /// + /// The instance at index 10. + /// + public SourceReference Source10; + + /// + /// The instance at index 11. + /// + public SourceReference Source11; + + /// + /// The instance at index 12. + /// + public SourceReference Source12; + + /// + /// The instance at index 13. + /// + public SourceReference Source13; + + /// + /// The instance at index 14. + /// + public SourceReference Source14; + + /// + /// The instance at index 15. + /// + public SourceReference Source15; + + /// + /// 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. + /// Thrown if is not a valid index for the current effect. + [UnscopedRef] + public ref SourceReference this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if ((uint)index >= 16) + { + ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); + } + + ref SourceReference r0 = ref Unsafe.As(ref this); + ref SourceReference r1 = ref Unsafe.Add(ref r0, index); + + return ref r1; + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs new file mode 100644 index 00000000..c9ab2eab --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/ResourceTextureManagerBuffer.cs @@ -0,0 +1,117 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1.Helpers; +using ComputeSharp.D2D1.Interop; + +namespace ComputeSharp.D2D1.Uwp.Buffers; + +/// +/// A fixed buffer type containing 16 fields. +/// +internal struct ResourceTextureManagerBuffer +{ + /// + /// The instance at index 0. + /// + public D2D1ResourceTextureManager? ResourceTextureManager0; + + /// + /// The instance at index 1. + /// + public D2D1ResourceTextureManager? ResourceTextureManager1; + + /// + /// The instance at index 2. + /// + public D2D1ResourceTextureManager? ResourceTextureManager2; + + /// + /// The instance at index 3. + /// + public D2D1ResourceTextureManager? ResourceTextureManager3; + + /// + /// The instance at index 4. + /// + public D2D1ResourceTextureManager? ResourceTextureManager4; + + /// + /// The instance at index 5. + /// + public D2D1ResourceTextureManager? ResourceTextureManager5; + + /// + /// The instance at index 6. + /// + public D2D1ResourceTextureManager? ResourceTextureManager6; + + /// + /// The instance at index 7. + /// + public D2D1ResourceTextureManager? ResourceTextureManager7; + + /// + /// The instance at index 8. + /// + public D2D1ResourceTextureManager? ResourceTextureManager8; + + /// + /// The instance at index 9. + /// + public D2D1ResourceTextureManager? ResourceTextureManager9; + + /// + /// The instance at index 10. + /// + public D2D1ResourceTextureManager? ResourceTextureManager10; + + /// + /// The instance at index 11. + /// + public D2D1ResourceTextureManager? ResourceTextureManager11; + + /// + /// The instance at index 12. + /// + public D2D1ResourceTextureManager? ResourceTextureManager12; + + /// + /// The instance at index 13. + /// + public D2D1ResourceTextureManager? ResourceTextureManager13; + + /// + /// The instance at index 14. + /// + public D2D1ResourceTextureManager? ResourceTextureManager14; + + /// + /// The instance at index 15. + /// + public D2D1ResourceTextureManager? ResourceTextureManager15; + + /// + /// 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. + /// Thrown if is not a valid index for the current effect. + [UnscopedRef] + public ref D2D1ResourceTextureManager? this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if ((uint)index >= 16) + { + ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); + } + + ref D2D1ResourceTextureManager? r0 = ref Unsafe.As(ref this); + ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index); + + return ref r1; + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs new file mode 100644 index 00000000..0e106017 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs @@ -0,0 +1,154 @@ +using System; +using System.Runtime.CompilerServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Uwp.Extensions; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using Windows.Graphics.Effects; + +namespace ComputeSharp.D2D1.Uwp.Buffers; + +/// +/// 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.Dispose(); + + 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; + } + + /// + /// Sets 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 void SetWrapper(IGraphicsEffectSource? wrapper) + { + this.graphicsEffectSourceWrapper = wrapper; + } + + /// + /// Sets the wrapper and underlying realized resource for the current source reference. + /// + /// The instance being a managed wrapper for the current effect input. + /// The underlying relized instance for . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetWrapperAndResource(IGraphicsEffectSource? wrapper, ID2D1Image* d2D1Image) + { + this.d2D1ImageSource.Dispose(); + + this.d2D1ImageSource = new ComPtr(d2D1Image); + this.graphicsEffectSourceWrapper = wrapper; + } + + /// + /// 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.Dispose(); + } + + this.d2D1ImageSource = new ComPtr(d2D1Image); + } + + return this.graphicsEffectSourceWrapper; + } +} \ 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 736d3fb0..f1b9fe74 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -8,9 +8,11 @@ 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 Win32 = TerraFX.Interop.Windows.Windows; #pragma warning disable CS0618 @@ -91,18 +93,16 @@ internal static unsafe class ID2D1EffectExtensions /// /// 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 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( + public static IGraphicsEffectSource? GetSource( this ref ID2D1Effect d2D1Effect, ICanvasDevice* canvasDevice, - ref PixelShaderEffect.SourceCollection.SourceReference source, + ref SourceReference source, int index) - where T : unmanaged, ID2D1PixelShader { using ComPtr d2D1Image = default; @@ -132,6 +132,104 @@ internal static unsafe class ID2D1EffectExtensions 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, + GetD2DImageFlags 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 & GetD2DImageFlags.UnrealizeOnFailure) == GetD2DImageFlags.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 & GetD2DImageFlags.UnrealizeOnFailure) == GetD2DImageFlags.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 & GetD2DImageFlags.UnrealizeOnFailure) == GetD2DImageFlags.None) + { + hresult.Assert(); + } + } + else if ((flags & GetD2DImageFlags.AllowNullEffectInputs) == GetD2DImageFlags.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}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs index 5e3b1847..54a6ce52 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -1,4 +1,6 @@ +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; @@ -101,7 +103,7 @@ unsafe partial class PixelShaderEffect lock (this.lockObject) { - ref SourceCollection.SourceReference source = ref Sources.Storage[index]; + ref SourceReference source = ref Sources.Storage[index]; return this.d2D1Effect.Get() switch { @@ -111,6 +113,46 @@ unsafe partial class PixelShaderEffect } } + /// + /// 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 GetD2DImageFlags sourceFlags = + GetD2DImageFlags.MinimalRealization | + GetD2DImageFlags.AllowNullEffectInputs | + GetD2DImageFlags.UnrealizeOnFailure; + + // 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 . /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs index 65922e42..e214a282 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs @@ -1,8 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Uwp.Buffers; namespace ComputeSharp.D2D1.Uwp; @@ -17,7 +16,7 @@ partial class PixelShaderEffect /// /// The fixed buffer of instances. /// - private FixedBuffer fixedBuffer; + private ResourceTextureManagerBuffer fixedBuffer; /// /// Creates a new instance with the specified parameters. @@ -48,118 +47,8 @@ partial class PixelShaderEffect internal PixelShaderEffect Owner { get; } /// - /// Gets a reference to the value containing the available -s. + /// Gets a reference to the value containing the available -s. /// - internal ref FixedBuffer Storage => ref this.fixedBuffer; - - /// - /// A fixed buffer type containing 16 fields. - /// - internal struct FixedBuffer - { - /// - /// The instance at index 0. - /// - public D2D1ResourceTextureManager? ResourceTextureManager0; - - /// - /// The instance at index 1. - /// - public D2D1ResourceTextureManager? ResourceTextureManager1; - - /// - /// The instance at index 2. - /// - public D2D1ResourceTextureManager? ResourceTextureManager2; - - /// - /// The instance at index 3. - /// - public D2D1ResourceTextureManager? ResourceTextureManager3; - - /// - /// The instance at index 4. - /// - public D2D1ResourceTextureManager? ResourceTextureManager4; - - /// - /// The instance at index 5. - /// - public D2D1ResourceTextureManager? ResourceTextureManager5; - - /// - /// The instance at index 6. - /// - public D2D1ResourceTextureManager? ResourceTextureManager6; - - /// - /// The instance at index 7. - /// - public D2D1ResourceTextureManager? ResourceTextureManager7; - - /// - /// The instance at index 8. - /// - public D2D1ResourceTextureManager? ResourceTextureManager8; - - /// - /// The instance at index 9. - /// - public D2D1ResourceTextureManager? ResourceTextureManager9; - - /// - /// The instance at index 10. - /// - public D2D1ResourceTextureManager? ResourceTextureManager10; - - /// - /// The instance at index 11. - /// - public D2D1ResourceTextureManager? ResourceTextureManager11; - - /// - /// The instance at index 12. - /// - public D2D1ResourceTextureManager? ResourceTextureManager12; - - /// - /// The instance at index 13. - /// - public D2D1ResourceTextureManager? ResourceTextureManager13; - - /// - /// The instance at index 14. - /// - public D2D1ResourceTextureManager? ResourceTextureManager14; - - /// - /// The instance at index 15. - /// - public D2D1ResourceTextureManager? ResourceTextureManager15; - - /// - /// 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. - /// Thrown if is not a valid index for the current effect. - [UnscopedRef] - public ref D2D1ResourceTextureManager? this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - if ((uint)index >= 16) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); - } - - ref D2D1ResourceTextureManager? r0 = ref Unsafe.As(ref this); - ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index); - - return ref r1; - } - } - } + internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer; } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs index 938dced2..b9e0afca 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs @@ -1,11 +1,5 @@ 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 ComputeSharp.D2D1.Uwp.Buffers; using Windows.Graphics.Effects; namespace ComputeSharp.D2D1.Uwp; @@ -21,7 +15,7 @@ partial class PixelShaderEffect /// /// The fixed buffer of instances. /// - private FixedBuffer fixedBuffer; + private GraphicsEffectSourceBuffer fixedBuffer; /// /// Creates a new instance with the specified parameters. @@ -38,10 +32,10 @@ partial class PixelShaderEffect /// 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] + public IGraphicsEffectSource? this[int index] { - get => null!; - set { } + get => Owner.GetSource(index); + set => Owner.SetSource(value, index); } /// @@ -50,241 +44,8 @@ partial class PixelShaderEffect internal PixelShaderEffect Owner { get; } /// - /// Gets a reference to the value containing the available -s. + /// Gets a reference to the value containing the available -s. /// - internal ref FixedBuffer Storage => ref this.fixedBuffer; - - /// - /// A fixed buffer type containing 16 fields. - /// - internal struct FixedBuffer - { - /// - /// The instance at index 0. - /// - public SourceReference Source0; - - /// - /// The instance at index 1. - /// - public SourceReference Source1; - - /// - /// The instance at index 2. - /// - public SourceReference Source2; - - /// - /// The instance at index 3. - /// - public SourceReference Source3; - - /// - /// The instance at index 4. - /// - public SourceReference Source4; - - /// - /// The instance at index 5. - /// - public SourceReference Source5; - - /// - /// The instance at index 6. - /// - public SourceReference Source6; - - /// - /// The instance at index 7. - /// - public SourceReference Source7; - - /// - /// The instance at index 8. - /// - public SourceReference Source8; - - /// - /// The instance at index 9. - /// - public SourceReference Source9; - - /// - /// The instance at index 10. - /// - public SourceReference Source10; - - /// - /// The instance at index 11. - /// - public SourceReference Source11; - - /// - /// The instance at index 12. - /// - public SourceReference Source12; - - /// - /// The instance at index 13. - /// - public SourceReference Source13; - - /// - /// The instance at index 14. - /// - public SourceReference Source14; - - /// - /// The instance at index 15. - /// - public SourceReference Source15; - - /// - /// 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. - /// Thrown if is not a valid index for the current effect. - [UnscopedRef] - public ref SourceReference this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - if ((uint)index >= 16) - { - ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range."); - } - - 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; - } - } + internal ref GraphicsEffectSourceBuffer Storage => ref this.fixedBuffer; } } \ 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 43851234..63404b95 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 @@ -33,6 +33,13 @@ internal unsafe partial struct ID2D1Effect return ((delegate* unmanaged[Stdcall])(lpVtbl[11]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, type, data, dataSize); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(14)] + public void SetInput([NativeTypeName("UINT32")] uint index, ID2D1Image* input, [NativeTypeName("BOOL")] int invalidate = 1) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[14]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, input, invalidate); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] [VtblIndex(16)] public void GetInput([NativeTypeName("UINT32")] uint index, ID2D1Image** input)