Implement PixelShaderEffect<T>.SetSource

このコミットが含まれているのは:
Sergio Pedri committed 2022-12-22 10:56:03 -08:00
1 親 6628f4d205
コミット a91ed7b70d
9ファイル変更
+612 -397

検索条件に一致するファイルはありません

+60 -30
ファイルの表示
@@ -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;
/// <summary>
/// Gets the device that the effect is currently realized on, if any.
/// </summary>
/// <param name="device">The resulting device, if available ((as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>)).</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(3)]
public HRESULT GetDevice(ICanvasDevice** device)
{
return ((delegate* unmanaged[Stdcall]<ICanvasImageInterop*, ICanvasDevice**, int>)this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device);
}
/// <summary>
/// Gets an <see cref="ID2D1Image"/> from an <see cref="ICanvasImageInterop.Interface"/> instance.
/// </summary>
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
/// <param name="deviceContext">
/// The device context in use. This value is is optional (but recommended), except when the
/// <see cref="GetD2DImageFlags.ReadDpiFromDeviceContext"/> flag is specified. This is because
/// not all callers of <see cref="GetD2DImage"/> 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.
/// </param>
/// <param name="flags">The flags to use to get the image.</param>
/// <param name="targetDpi">
/// The DPI of the target device context. This is used to determine when a <c>D2D1DpiCompensation</c>
/// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values
/// <see cref="GetD2DImageFlags.ReadDpiFromDeviceContext"/>, <see cref="GetD2DImageFlags.AlwaysInsertDpiCompensation"/>
/// or <see cref="GetD2DImageFlags.NeverInsertDpiCompensation"/>
/// </param>
/// <param name="realizeDpi">
/// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A <c>D2D1DpiCompensation</c> effect
/// will be inserted if <paramref name="targetDpi"/> and <paramref name="realizeDpi"/> are different (flags permitting).
/// </param>
/// <param name="ppImage">The resulting <see cref="ID2D1Image"/> for the effect.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(4)]
public HRESULT GetD2DImage(
ICanvasDevice* device,
ID2D1DeviceContext* deviceContext,
GetD2DImageFlags flags,
float targetDpi,
float* realizeDpi,
ID2D1Image** ppImage)
{
return ((delegate* unmanaged[Stdcall]<ICanvasImageInterop*, ICanvasDevice*, ID2D1DeviceContext*, GetD2DImageFlags, float, float*, ID2D1Image**, int>)this.lpVtbl[4])(
(ICanvasImageInterop*)Unsafe.AsPointer(ref this),
device,
deviceContext,
flags,
targetDpi,
realizeDpi,
ppImage);
}
/// <summary>
/// The <see cref="ComImportAttribute"/> implementation of <see cref="ICanvasImageInterop"/>.
/// </summary>
@@ -26,40 +84,12 @@ internal unsafe struct ICanvasImageInterop
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface Interface
{
/// <summary>
/// Gets the device that the effect is currently realized on, if any.
/// </summary>
/// <param name="device">The resulting device, if available ((as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>)).</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
/// <inheritdoc cref="ICanvasImageInterop.GetDevice"/>
[PreserveSig]
[return: NativeTypeName("HRESULT")]
int GetDevice(ICanvasDevice** device);
/// <summary>
/// Gets an <see cref="ID2D1Image"/> from an <see cref="ICanvasImageInterop.Interface"/> instance.
/// </summary>
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
/// <param name="deviceContext">
/// The device context in use. This value is is optional (but recommended), except when the
/// <see cref="GetD2DImageFlags.ReadDpiFromDeviceContext"/> flag is specified. This is because
/// not all callers of <see cref="GetD2DImage"/> 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.
/// </param>
/// <param name="flags">The flags to use to get the image.</param>
/// <param name="targetDpi">
/// The DPI of the target device context. This is used to determine when a <c>D2D1DpiCompensation</c>
/// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values
/// <see cref="GetD2DImageFlags.ReadDpiFromDeviceContext"/>, <see cref="GetD2DImageFlags.AlwaysInsertDpiCompensation"/>
/// or <see cref="GetD2DImageFlags.NeverInsertDpiCompensation"/>
/// </param>
/// <param name="realizeDpi">
/// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A <c>D2D1DpiCompensation</c> effect
/// will be inserted if <paramref name="targetDpi"/> and <paramref name="realizeDpi"/> are different (flags permitting).
/// </param>
/// <param name="ppImage">The resulting <see cref="ID2D1Image"/> for the effect.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
/// <inheritdoc cref="ICanvasImageInterop.GetD2DImage"/>
[PreserveSig]
[return: NativeTypeName("HRESULT")]
int GetD2DImage(
+117
ファイルの表示
@@ -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;
/// <summary>
/// A fixed buffer type containing 16 <see cref="IGraphicsEffectSource"/> fields.
/// </summary>
internal struct GraphicsEffectSourceBuffer
{
/// <summary>
/// The <see cref="SourceReference"/> instance at index 0.
/// </summary>
public SourceReference Source0;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 1.
/// </summary>
public SourceReference Source1;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 2.
/// </summary>
public SourceReference Source2;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 3.
/// </summary>
public SourceReference Source3;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 4.
/// </summary>
public SourceReference Source4;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 5.
/// </summary>
public SourceReference Source5;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 6.
/// </summary>
public SourceReference Source6;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 7.
/// </summary>
public SourceReference Source7;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 8.
/// </summary>
public SourceReference Source8;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 9.
/// </summary>
public SourceReference Source9;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 10.
/// </summary>
public SourceReference Source10;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 11.
/// </summary>
public SourceReference Source11;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 12.
/// </summary>
public SourceReference Source12;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 13.
/// </summary>
public SourceReference Source13;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 14.
/// </summary>
public SourceReference Source14;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 15.
/// </summary>
public SourceReference Source15;
/// <summary>
/// Gets a reference to the <see cref="SourceReference"/> value at a given index.
/// </summary>
/// <param name="index">The index of the <see cref="SourceReference"/> value to get a reference to.</param>
/// <returns>A reference to the <see cref="SourceReference"/> value at a given index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[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<GraphicsEffectSourceBuffer, SourceReference>(ref this);
ref SourceReference r1 = ref Unsafe.Add(ref r0, index);
return ref r1;
}
}
}
+117
ファイルの表示
@@ -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;
/// <summary>
/// A fixed buffer type containing 16 <see cref="D2D1ResourceTextureManager"/> fields.
/// </summary>
internal struct ResourceTextureManagerBuffer
{
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 0.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager0;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 1.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager1;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 2.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager2;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 3.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager3;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 4.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager4;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 5.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager5;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 6.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager6;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 7.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager7;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 8.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager8;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 9.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager9;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 10.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager10;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 11.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager11;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 12.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager12;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 13.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager13;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 14.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager14;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 15.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager15;
/// <summary>
/// Gets a reference to the <see cref="D2D1ResourceTextureManager"/> value at a given index.
/// </summary>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> value to get a reference to.</param>
/// <returns>A reference to the <see cref="D2D1ResourceTextureManager"/> value at a given index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[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<ResourceTextureManagerBuffer, D2D1ResourceTextureManager?>(ref this);
ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index);
return ref r1;
}
}
}
+154
ファイルの表示
@@ -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;
/// <summary>
/// A wrapper type for an input <see cref="IGraphicsEffectSource"/> and its underlying native D2D images.
/// </summary>
/// <remarks>
/// <para>
/// This type is meant to support these two common operations:
/// <list type="bullet">
/// <item>Look up a D2D resource from a property of some other D2D interface.</item>
/// <item>Get a managed wrapper from a D2D interface.</item>
/// </list>
/// 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.
/// </para>
/// <para>
/// This gives the following improvements:
/// <list type="bullet">
/// <item>Improved performance for the common case of repeated calls using the same D2D instance.</item>
/// <item>Maintaining a reference to the wrapper avoids it being repeatedly released and recreated.</item>
/// </list>
/// </para>
/// <para>
/// 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.
/// </para>
/// <para>
/// For realized effects, <see cref="SourceReference"/> tracks both the <see cref="ID2D1Image"/> resource and its
/// <see cref="IGraphicsEffectSource"/> wrapper (and optional <see cref="ID2D1Effect"/> being the DPI compensation effect,
/// if one is needed. For unrealized effects, the resource is <see langword="null"/> and only the wrapper part is used.
/// </para>
/// </remarks>
internal unsafe struct SourceReference
{
/// <summary>
/// The <see cref="ID2D1Image"/> produced by calling <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>
/// on the input managed wrapper used as source for the effect (ie. <see cref="graphicsEffectSourceWrapper"/>).
/// </summary>
private ComPtr<ID2D1Image> d2D1ImageSource;
/// <summary>
/// The <see cref="ID2D1Effect"/> for DPI compensation wrapping <see cref="d2D1ImageSource"/>, if needed.
/// </summary>
private ComPtr<ID2D1Effect> d2D1EffectDpiCompensation;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
/// </summary>
private IGraphicsEffectSource? graphicsEffectSourceWrapper;
/// <summary>
/// Gets whether or not this source ference has a DPI compensation effect.
/// </summary>
public readonly bool HasDpiCompensationEffect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.d2D1EffectDpiCompensation.Get() is not null;
}
/// <summary>
/// Gets the <see cref="ID2D1Effect"/> object for the DPI compensation effect used, if any.
/// </summary>
/// <returns>The <see cref="ID2D1Effect"/> object for the DPI compensation effect used, if any.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ID2D1Effect* GetDpiCompensationEffect()
{
return this.d2D1EffectDpiCompensation.Get();
}
/// <summary>
/// Sets the DPI compensation effet being used,
/// </summary>
/// <param name="d2D1Effect"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetDpiCompensationEffect(ID2D1Effect* d2D1Effect)
{
this.d2D1EffectDpiCompensation.Dispose();
if (d2D1Effect is not null)
{
this.d2D1EffectDpiCompensation = new ComPtr<ID2D1Effect>(d2D1Effect);
}
}
/// <summary>
/// Gets the <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
/// </summary>
/// <returns>The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly IGraphicsEffectSource? GetWrapper()
{
return this.graphicsEffectSourceWrapper;
}
/// <summary>
/// Sets the <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
/// </summary>
/// <param name="wrapper">The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetWrapper(IGraphicsEffectSource? wrapper)
{
this.graphicsEffectSourceWrapper = wrapper;
}
/// <summary>
/// Sets the wrapper and underlying realized resource for the current source reference.
/// </summary>
/// <param name="wrapper">The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</param>
/// <param name="d2D1Image">The underlying relized <see cref="ID2D1Image"/> instance for <paramref name="wrapper"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetWrapperAndResource(IGraphicsEffectSource? wrapper, ID2D1Image* d2D1Image)
{
this.d2D1ImageSource.Dispose();
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
this.graphicsEffectSourceWrapper = wrapper;
}
/// <summary>
/// Gets or creates an <see cref="IGraphicsEffectSource"/> wrapper for a given <see cref="ID2D1Image"/> object.
/// </summary>
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object the source effect is realized on.</param>
/// <param name="d2D1Image">The input <see cref="ID2D1Image"/> object to get or create a wrapper for.</param>
/// <returns>An <see cref="IGraphicsEffectSource"/> wrapper for a given <see cref="ID2D1Image"/> object.</returns>
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<TWrapper>(device, resource);
throw new NotImplementedException();
}
else
{
this.d2D1ImageSource.Dispose();
}
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
}
return this.graphicsEffectSourceWrapper;
}
}
+103 -5
ファイルの表示
@@ -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
/// <summary>
/// Gets the <see cref="IGraphicsEffectSource"/> source from a given <see cref="ID2D1Effect"/> object, at a specified index.
/// </summary>
/// <typeparam name="T">The type of shader being used.</typeparam>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="canvasDevice">The realization device currently in use.</param>
/// <param name="source">The input <see cref="PixelShaderEffect{T}.SourceCollection.SourceReference"/> object for the target index.</param>
/// <param name="source">The input <see cref="SourceReference"/> object for the target index.</param>
/// <param name="index">The index for the source to retrieve.</param>
/// <returns>The <see cref="IGraphicsEffectSource"/> source from a given <see cref="ID2D1Effect"/> object, at a specified index.</returns>
public static IGraphicsEffectSource? GetSource<T>(
public static IGraphicsEffectSource? GetSource(
this ref ID2D1Effect d2D1Effect,
ICanvasDevice* canvasDevice,
ref PixelShaderEffect<T>.SourceCollection.SourceReference source,
ref SourceReference source,
int index)
where T : unmanaged, ID2D1PixelShader
{
using ComPtr<ID2D1Image> d2D1Image = default;
@@ -132,6 +132,104 @@ internal static unsafe class ID2D1EffectExtensions
return source.GetOrCreateWrapper(null, d2D1Image.Get());
}
/// <summary>
/// Sets the <see cref="IGraphicsEffectSource"/> source for a given <see cref="ID2D1Effect"/> object, at a specified index.
/// </summary>
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
/// <param name="canvasDevice">The realization device currently in use.</param>
/// <param name="deviceContext">The device context currently in use.</param>
/// <param name="flags">The realization flags to use.</param>
/// <param name="targetDpi">The target DPI value for the source.</param>
/// <param name="value">The input <see cref="IGraphicsEffectSource"/> value to set.</param>
/// <param name="source">The target <see cref="SourceReference"/> value, for caching.</param>
/// <param name="index">The index of the source to set.</param>
/// <returns>Whether the operation succeeded (if it didn't the effect should be unrealized).</returns>
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<ID2D1Image> d2D1Image = default;
float realizedDpi = 0;
if (value is not null)
{
using ComPtr<IUnknown> canvasImageUnknown = default;
// Get the underlying IUnknown object for the input source
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value));
using ComPtr<ICanvasImageInterop> 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<ICanvasDevice> 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;
}
/// <summary>
/// Gets the <see cref="D2D1ResourceTextureManager"/> instance from a given <see cref="ID2D1Effect"/> object.
/// </summary>
+43 -1
ファイルの表示
@@ -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<T>
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<T>
}
}
/// <summary>
/// Sets the marshalled value for <see cref="Sources"/>.
/// </summary>
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
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);
}
}
}
/// <summary>
/// Gets the marshalled value for <see cref="ResourceTextureManagers"/>.
/// </summary>
@@ -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<T>
/// <summary>
/// The fixed buffer of <see cref="D2D1ResourceTextureManager"/> instances.
/// </summary>
private FixedBuffer fixedBuffer;
private ResourceTextureManagerBuffer fixedBuffer;
/// <summary>
/// Creates a new <see cref="ResourceTextureManagerCollection"/> instance with the specified parameters.
@@ -48,118 +47,8 @@ partial class PixelShaderEffect<T>
internal PixelShaderEffect<T> Owner { get; }
/// <summary>
/// Gets a reference to the <see cref="FixedBuffer"/> value containing the available <see cref="D2D1ResourceTextureManager"/>-s.
/// Gets a reference to the <see cref="ResourceTextureManagerBuffer"/> value containing the available <see cref="D2D1ResourceTextureManager"/>-s.
/// </summary>
internal ref FixedBuffer Storage => ref this.fixedBuffer;
/// <summary>
/// A fixed buffer type containing 16 <see cref="D2D1ResourceTextureManager"/> fields.
/// </summary>
internal struct FixedBuffer
{
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 0.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager0;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 1.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager1;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 2.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager2;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 3.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager3;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 4.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager4;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 5.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager5;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 6.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager6;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 7.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager7;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 8.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager8;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 9.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager9;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 10.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager10;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 11.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager11;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 12.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager12;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 13.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager13;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 14.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager14;
/// <summary>
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 15.
/// </summary>
public D2D1ResourceTextureManager? ResourceTextureManager15;
/// <summary>
/// Gets a reference to the <see cref="D2D1ResourceTextureManager"/> value at a given index.
/// </summary>
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> value to get a reference to.</param>
/// <returns>A reference to the <see cref="D2D1ResourceTextureManager"/> value at a given index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[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<FixedBuffer, D2D1ResourceTextureManager?>(ref this);
ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index);
return ref r1;
}
}
}
internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer;
}
}
+7 -246
ファイルの表示
@@ -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<T>
/// <summary>
/// The fixed buffer of <see cref="IGraphicsEffectSource"/> instances.
/// </summary>
private FixedBuffer fixedBuffer;
private GraphicsEffectSourceBuffer fixedBuffer;
/// <summary>
/// Creates a new <see cref="SourceCollection"/> instance with the specified parameters.
@@ -38,10 +32,10 @@ partial class PixelShaderEffect<T>
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
/// <returns>The <see cref="IGraphicsEffectSource"/> source at the specified index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
public IGraphicsEffectSource this[int index]
public IGraphicsEffectSource? this[int index]
{
get => null!;
set { }
get => Owner.GetSource(index);
set => Owner.SetSource(value, index);
}
/// <summary>
@@ -50,241 +44,8 @@ partial class PixelShaderEffect<T>
internal PixelShaderEffect<T> Owner { get; }
/// <summary>
/// Gets a reference to the <see cref="FixedBuffer"/> value containing the available <see cref="IGraphicsEffectSource"/>-s.
/// Gets a reference to the <see cref="GraphicsEffectSourceBuffer"/> value containing the available <see cref="IGraphicsEffectSource"/>-s.
/// </summary>
internal ref FixedBuffer Storage => ref this.fixedBuffer;
/// <summary>
/// A fixed buffer type containing 16 <see cref="IGraphicsEffectSource"/> fields.
/// </summary>
internal struct FixedBuffer
{
/// <summary>
/// The <see cref="SourceReference"/> instance at index 0.
/// </summary>
public SourceReference Source0;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 1.
/// </summary>
public SourceReference Source1;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 2.
/// </summary>
public SourceReference Source2;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 3.
/// </summary>
public SourceReference Source3;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 4.
/// </summary>
public SourceReference Source4;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 5.
/// </summary>
public SourceReference Source5;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 6.
/// </summary>
public SourceReference Source6;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 7.
/// </summary>
public SourceReference Source7;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 8.
/// </summary>
public SourceReference Source8;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 9.
/// </summary>
public SourceReference Source9;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 10.
/// </summary>
public SourceReference Source10;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 11.
/// </summary>
public SourceReference Source11;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 12.
/// </summary>
public SourceReference Source12;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 13.
/// </summary>
public SourceReference Source13;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 14.
/// </summary>
public SourceReference Source14;
/// <summary>
/// The <see cref="SourceReference"/> instance at index 15.
/// </summary>
public SourceReference Source15;
/// <summary>
/// Gets a reference to the <see cref="SourceReference"/> value at a given index.
/// </summary>
/// <param name="index">The index of the <see cref="SourceReference"/> value to get a reference to.</param>
/// <returns>A reference to the <see cref="SourceReference"/> value at a given index.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
[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<FixedBuffer, SourceReference>(ref this);
ref SourceReference r1 = ref Unsafe.Add(ref r0, index);
return ref r1;
}
}
}
/// <summary>
/// A wrapper type for an input <see cref="IGraphicsEffectSource"/> and its underlying native D2D images.
/// </summary>
/// <remarks>
/// <para>
/// This type is meant to support these two common operations:
/// <list type="bullet">
/// <item>Look up a D2D resource from a property of some other D2D interface.</item>
/// <item>Get a managed wrapper from a D2D interface.</item>
/// </list>
/// 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.
/// </para>
/// <para>
/// This gives the following improvements:
/// <list type="bullet">
/// <item>Improved performance for the common case of repeated calls using the same D2D instance.</item>
/// <item>Maintaining a reference to the wrapper avoids it being repeatedly released and recreated.</item>
/// </list>
/// </para>
/// <para>
/// 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.
/// </para>
/// <para>
/// For realized effects, <see cref="SourceReference"/> tracks both the <see cref="ID2D1Image"/> resource and its
/// <see cref="IGraphicsEffectSource"/> wrapper (and optional <see cref="ID2D1Effect"/> being the DPI compensation effect,
/// if one is needed. For unrealized effects, the resource is <see langword="null"/> and only the wrapper part is used.
/// </para>
/// </remarks>
internal unsafe struct SourceReference
{
/// <summary>
/// The <see cref="ID2D1Image"/> produced by calling <see cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop.Interface.GetD2DImage"/>
/// on the input managed wrapper used as source for the effect (ie. <see cref="graphicsEffectSourceWrapper"/>).
/// </summary>
private ComPtr<ID2D1Image> d2D1ImageSource;
/// <summary>
/// The <see cref="ID2D1Effect"/> for DPI compensation wrapping <see cref="d2D1ImageSource"/>, if needed.
/// </summary>
private ComPtr<ID2D1Effect> d2D1EffectDpiCompensation;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
/// </summary>
private IGraphicsEffectSource? graphicsEffectSourceWrapper;
/// <summary>
/// Gets whether or not this source ference has a DPI compensation effect.
/// </summary>
public readonly bool HasDpiCompensationEffect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.d2D1EffectDpiCompensation.Get() is not null;
}
/// <summary>
/// Gets the <see cref="ID2D1Effect"/> object for the DPI compensation effect used, if any.
/// </summary>
/// <returns>The <see cref="ID2D1Effect"/> object for the DPI compensation effect used, if any.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ID2D1Effect* GetDpiCompensationEffect()
{
return this.d2D1EffectDpiCompensation.Get();
}
/// <summary>
/// Sets the DPI compensation effet being used,
/// </summary>
/// <param name="d2D1Effect"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetDpiCompensationEffect(ID2D1Effect* d2D1Effect)
{
_ = this.d2D1EffectDpiCompensation.Reset();
if (d2D1Effect is not null)
{
this.d2D1EffectDpiCompensation = new ComPtr<ID2D1Effect>(d2D1Effect);
}
}
/// <summary>
/// Gets the <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
/// </summary>
/// <returns>The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly IGraphicsEffectSource? GetWrapper()
{
return this.graphicsEffectSourceWrapper;
}
/// <summary>
/// Gets or creates an <see cref="IGraphicsEffectSource"/> wrapper for a given <see cref="ID2D1Image"/> object.
/// </summary>
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object the source effect is realized on.</param>
/// <param name="d2D1Image">The input <see cref="ID2D1Image"/> object to get or create a wrapper for.</param>
/// <returns>An <see cref="IGraphicsEffectSource"/> wrapper for a given <see cref="ID2D1Image"/> object.</returns>
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<TWrapper>(device, resource);
throw new NotImplementedException();
}
else
{
_ = this.d2D1ImageSource.Reset();
}
_ = this.d2D1ImageSource.Reset();
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
}
return this.graphicsEffectSourceWrapper;
}
}
internal ref GraphicsEffectSourceBuffer Storage => ref this.fixedBuffer;
}
}
+7
ファイルの表示
@@ -33,6 +33,13 @@ internal unsafe partial struct ID2D1Effect
return ((delegate* unmanaged[Stdcall]<ID2D1Effect*, uint, D2D1_PROPERTY_TYPE, byte*, uint, int>)(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]<ID2D1Effect*, uint, ID2D1Image*, int, void>)(lpVtbl[14]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, input, invalidate);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(16)]
public void GetInput([NativeTypeName("UINT32")] uint index, ID2D1Image** input)