Implement PixelShaderEffect<T>.GetSource(int)

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

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

+1 -1
ファイルの表示
@@ -24,7 +24,7 @@ internal static unsafe class ICanvasDeviceExtensions
// Get the ICanvasResourceWrapperNative wrapper around the input device
canvasDevice.QueryInterface(
riid: Win32.__uuidof<ICanvasResourceWrapperNative>(),
ppvObject: (void**)canvasDeviceResourceWrapperNative.GetAddressOf()).Assert();
ppvObject: canvasDeviceResourceWrapperNative.GetVoidAddressOf()).Assert();
// Get the underlying ID2D1Device1 instance in use
HRESULT hresult = canvasDeviceResourceWrapperNative.Get()->GetNativeResource(
+47 -1
ファイルの表示
@@ -2,6 +2,7 @@ using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Interop;
@@ -9,6 +10,7 @@ using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
#pragma warning disable CS0618
@@ -86,6 +88,50 @@ internal static unsafe class ID2D1EffectExtensions
return (D2D1TransformMapper<T>)GCHandle.FromIntPtr(handlePtr).Target!;
}
/// <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="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>(
this ref ID2D1Effect d2D1Effect,
ICanvasDevice* canvasDevice,
ref PixelShaderEffect<T>.SourceCollection.SourceReference source,
int index)
where T : unmanaged, ID2D1PixelShader
{
using ComPtr<ID2D1Image> 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());
}
/// <summary>
/// Gets the <see cref="D2D1ResourceTextureManager"/> instance from a given <see cref="ID2D1Effect"/> object.
/// </summary>
@@ -121,7 +167,7 @@ internal static unsafe class ID2D1EffectExtensions
source.GetD2D1ResourceTextureManager(d2D1ResourceTextureManagerSource.GetAddressOf());
// Check if the instances match, and return the managed wrapper we already have
if (d2D1ResourceTextureManager.IsSameInstance(in d2D1ResourceTextureManagerSource))
if (d2D1ResourceTextureManager.Get()->IsSameInstance(d2D1ResourceTextureManagerSource.Get()))
{
return source;
}
+2 -2
ファイルの表示
@@ -35,8 +35,8 @@ internal static class IUnknownExtensions
using ComPtr<IUnknown> leftUnknown = default;
using ComPtr<IUnknown> rightUnknown = default;
((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof<T>(), (void**)leftUnknown.GetAddressOf()).Assert();
((IUnknown*)right)->QueryInterface(Win32.__uuidof<T>(), (void**)rightUnknown.GetAddressOf()).Assert();
((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.__uuidof<T>(), leftUnknown.GetVoidAddressOf()).Assert();
((IUnknown*)right)->QueryInterface(Win32.__uuidof<T>(), rightUnknown.GetVoidAddressOf()).Assert();
return leftUnknown.Get() == rightUnknown.Get();
}
+22
ファイルの表示
@@ -3,6 +3,7 @@ using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.Interop;
using Microsoft.Graphics.Canvas;
using TerraFX.Interop.DirectX;
using Windows.Graphics.Effects;
namespace ComputeSharp.D2D1.Uwp;
@@ -89,6 +90,27 @@ unsafe partial class PixelShaderEffect<T>
}
}
/// <summary>
/// Gets the marshalled value for <see cref="Sources"/>.
/// </summary>
/// <param name="index">The index of the <see cref="Sources"/> source to get or set.</param>
/// <returns>The marshalled value for <see cref="Sources"/>.</returns>
private IGraphicsEffectSource? GetSource(int index)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
lock (this.lockObject)
{
ref SourceCollection.SourceReference source = ref Sources.Storage[index];
return this.d2D1Effect.Get() switch
{
not null => this.d2D1Effect.Get()->GetSource(this.canvasDevice.Get(), ref source, index),
_ => source.GetWrapper()
};
}
}
/// <summary>
/// Gets the marshalled value for <see cref="ResourceTextureManagers"/>.
/// </summary>
+165 -38
ファイルの表示
@@ -1,7 +1,11 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Helpers;
using ComputeSharp.D2D1.Uwp.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
namespace ComputeSharp.D2D1.Uwp;
@@ -56,93 +60,93 @@ partial class PixelShaderEffect<T>
internal struct FixedBuffer
{
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 0.
/// The <see cref="SourceReference"/> instance at index 0.
/// </summary>
public IGraphicsEffectSource? Source0;
public SourceReference Source0;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 1.
/// The <see cref="SourceReference"/> instance at index 1.
/// </summary>
public IGraphicsEffectSource? Source1;
public SourceReference Source1;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 2.
/// The <see cref="SourceReference"/> instance at index 2.
/// </summary>
public IGraphicsEffectSource? Source2;
public SourceReference Source2;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 3.
/// The <see cref="SourceReference"/> instance at index 3.
/// </summary>
public IGraphicsEffectSource? Source3;
public SourceReference Source3;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 4.
/// The <see cref="SourceReference"/> instance at index 4.
/// </summary>
public IGraphicsEffectSource? Source4;
public SourceReference Source4;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 5.
/// The <see cref="SourceReference"/> instance at index 5.
/// </summary>
public IGraphicsEffectSource? Source5;
public SourceReference Source5;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 6.
/// The <see cref="SourceReference"/> instance at index 6.
/// </summary>
public IGraphicsEffectSource? Source6;
public SourceReference Source6;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 7.
/// The <see cref="SourceReference"/> instance at index 7.
/// </summary>
public IGraphicsEffectSource? Source7;
public SourceReference Source7;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 8.
/// The <see cref="SourceReference"/> instance at index 8.
/// </summary>
public IGraphicsEffectSource? Source8;
public SourceReference Source8;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 9.
/// The <see cref="SourceReference"/> instance at index 9.
/// </summary>
public IGraphicsEffectSource? Source9;
public SourceReference Source9;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 10.
/// The <see cref="SourceReference"/> instance at index 10.
/// </summary>
public IGraphicsEffectSource? Source10;
public SourceReference Source10;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 11.
/// The <see cref="SourceReference"/> instance at index 11.
/// </summary>
public IGraphicsEffectSource? Source11;
public SourceReference Source11;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 12.
/// The <see cref="SourceReference"/> instance at index 12.
/// </summary>
public IGraphicsEffectSource? Source12;
public SourceReference Source12;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 13.
/// The <see cref="SourceReference"/> instance at index 13.
/// </summary>
public IGraphicsEffectSource? Source13;
public SourceReference Source13;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 14.
/// The <see cref="SourceReference"/> instance at index 14.
/// </summary>
public IGraphicsEffectSource? Source14;
public SourceReference Source14;
/// <summary>
/// The <see cref="IGraphicsEffectSource"/> instance at index 15.
/// The <see cref="SourceReference"/> instance at index 15.
/// </summary>
public IGraphicsEffectSource? Source15;
public SourceReference Source15;
/// <summary>
/// Gets a reference to the <see cref="IGraphicsEffectSource"/> value at a given index.
/// Gets a reference to the <see cref="SourceReference"/> value at a given index.
/// </summary>
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> value to get a reference to.</param>
/// <returns>A reference to the <see cref="IGraphicsEffectSource"/> value at a given index.</returns>
/// <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 IGraphicsEffectSource? this[int index]
public ref SourceReference this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
@@ -152,12 +156,135 @@ partial class PixelShaderEffect<T>
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The index must be in the [0, 15] range.");
}
ref IGraphicsEffectSource? r0 = ref Unsafe.As<FixedBuffer, IGraphicsEffectSource?>(ref this);
ref IGraphicsEffectSource? r1 = ref Unsafe.Add(ref r0, index);
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;
}
}
}
}
+7
ファイルの表示
@@ -32,4 +32,11 @@ 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(16)]
public void GetInput([NativeTypeName("UINT32")] uint index, ID2D1Image** input)
{
((delegate* unmanaged[Stdcall]<ID2D1Effect*, uint, ID2D1Image**, void>)(lpVtbl[16]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, input);
}
}