Merge pull request #512 from Sergio0694/dev/canvas-effect-factory

Add support for ICanvasFactoryNative.RegisterEffectFactory in PixelShaderEffect<T>
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-04-23 05:48:17 -07:00
コミット d61b37a6db
11ファイル変更
+257 -10

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

+4 -1
ファイルの表示
@@ -341,4 +341,7 @@ ASALocalRun/
samples/**/city_*.jpg
# XML docs files
src/**/ComputeSharp*.xml
src/**/ComputeSharp*.xml
# Preserve .rd.xml files
!src/**/ComputeSharp*.rd.xml
+82 -2
ファイルの表示
@@ -4,7 +4,11 @@ using System.Runtime.InteropServices;
using TerraFX.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using TerraFX.Interop.WinRT;
#if !WINDOWS_UWP
using WinRT;
using WinRT.Interop;
#endif
using IInspectable = TerraFX.Interop.WinRT.IInspectable;
#pragma warning disable CS0649, IDE1006
@@ -47,7 +51,7 @@ internal unsafe struct ICanvasEffectFactoryNative
}
/// <summary>
/// Creates a new inspectable wrapper for an inpute D2D effect previosly registered through <see cref="ICanvasFactoryNative.RegisterEffectFactory"/>.
/// Creates a new inspectable wrapper for an input D2D effect previosly registered through <see cref="ICanvasFactoryNative.RegisterEffectFactory"/>.
/// </summary>
/// <param name="device">The input canvas device.</param>
/// <param name="resource">The input native effect to create a wrapper for.</param>
@@ -69,4 +73,80 @@ internal unsafe struct ICanvasEffectFactoryNative
dpi,
wrapper);
}
/// <summary>
/// The managed implementation of <see cref="ICanvasEffectFactoryNative"/>.
/// </summary>
[Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
#if WINDOWS_UWP
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
#else
[WindowsRuntimeType]
[WindowsRuntimeHelperType(typeof(Interface))]
#endif
public interface Interface
{
/// <inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper"/>
#if WINDOWS_UWP
[PreserveSig]
#endif
[return: NativeTypeName("HRESULT")]
int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper);
#if !WINDOWS_UWP
/// <summary>
/// The vtable type for <see cref="Interface"/>.
/// </summary>
[Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
public struct Vftbl
{
/// <summary>
/// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention).
/// </summary>
public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl();
/// <summary>
/// Builds the custom method table pointer for <see cref="Interface"/>.
/// </summary>
/// <returns>The method table pointer for <see cref="Interface"/>.</returns>
private static IntPtr InitVtbl()
{
Vftbl* lpVtbl = (Vftbl*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(Vftbl));
lpVtbl->IUnknownVftbl = IUnknownVftbl.AbiToProjectionVftbl;
lpVtbl->CreateWrapper = &CreateWrapperFromAbi;
return (IntPtr)lpVtbl;
}
/// <summary>
/// The IUnknown vtable.
/// </summary>
private IUnknownVftbl IUnknownVftbl;
/// <summary>
/// Function pointer for <see cref="CreateWrapperFromAbi"/>.
/// </summary>
private delegate* unmanaged[Stdcall]<IntPtr, ICanvasDevice*, ID2D1Effect*, float, IInspectable**, int> CreateWrapper;
/// <inheritdoc cref="Interface.CreateWrapper"/>
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[return: NativeTypeName("HRESULT")]
private static int CreateWrapperFromAbi(IntPtr thisPtr, ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
{
try
{
return ComWrappersSupport.FindObject<Interface>(thisPtr).CreateWrapper(device, resource, dpi, wrapper);
}
catch (Exception e)
{
ExceptionHelpers.SetErrorInfo(e);
return Marshal.GetHRForException(e);
}
}
}
#endif
}
}
+5 -3
ファイルの表示
@@ -128,8 +128,9 @@ internal unsafe struct ICanvasFactoryNative
[VtblIndex(9)]
public HRESULT RegisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId, ICanvasEffectFactoryNative* factory)
{
return ((delegate* unmanaged[Stdcall]<ICanvasFactoryNative*, ICanvasEffectFactoryNative*, int>)this.lpVtbl[9])(
return ((delegate* unmanaged[Stdcall]<ICanvasFactoryNative*, Guid*, ICanvasEffectFactoryNative*, int>)this.lpVtbl[9])(
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
effectId,
factory);
}
@@ -145,8 +146,9 @@ internal unsafe struct ICanvasFactoryNative
[VtblIndex(10)]
public HRESULT UnregisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId)
{
return ((delegate* unmanaged[Stdcall]<ICanvasFactoryNative*, int>)this.lpVtbl[10])(
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this));
return ((delegate* unmanaged[Stdcall]<ICanvasFactoryNative*, Guid*, int>)this.lpVtbl[10])(
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
effectId);
}
#if !WINDOWS_UWP
+3 -3
ファイルの表示
@@ -145,17 +145,17 @@ internal unsafe struct ICanvasImageInterop
/// <summary>
/// The IUnknown vtable.
/// </summary>
internal IUnknownVftbl IUnknownVftbl;
private IUnknownVftbl IUnknownVftbl;
/// <summary>
/// Function pointer for <see cref="GetDeviceFromAbi"/>.
/// </summary>
internal delegate* unmanaged[Stdcall]<IntPtr, ICanvasDevice**, WIN2D_GET_DEVICE_ASSOCIATION_TYPE*, int> GetDevice;
private delegate* unmanaged[Stdcall]<IntPtr, ICanvasDevice**, WIN2D_GET_DEVICE_ASSOCIATION_TYPE*, int> GetDevice;
/// <summary>
/// Function pointer for <see cref="GetD2DImageFromAbi"/>.
/// </summary>
internal delegate* unmanaged[Stdcall]<IntPtr, ICanvasDevice*, ID2D1DeviceContext*, WIN2D_GET_D2D_IMAGE_FLAGS, float, float*, ID2D1Image**, int> GetD2DImage;
private delegate* unmanaged[Stdcall]<IntPtr, ICanvasDevice*, ID2D1DeviceContext*, WIN2D_GET_D2D_IMAGE_FLAGS, float, float*, ID2D1Image**, int> GetD2DImage;
/// <inheritdoc cref="Interface.GetDevice"/>
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
+1
ファイルの表示
@@ -38,6 +38,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ResourceManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasImage.cs" />
+19
ファイルの表示
@@ -90,6 +90,25 @@ internal static unsafe class ResourceManager
canvasFactoryNative.Get()->UnregisterWrapper(resource).Assert();
}
/// <summary>
/// Registers a factory of wrappers for custom effect with a given effect id.
/// </summary>
/// <param name="effectId">The id of the effects to register a factory for.</param>
/// <param name="factory">The input factory to create wrappers of native effects.</param>
public static void RegisterEffectFactory(Guid effectId, ICanvasEffectFactoryNative.Interface factory)
{
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
GetActivationFactory(canvasFactoryNative.GetAddressOf());
using ComPtr<ICanvasEffectFactoryNative> canvasEffectFactoryNative = default;
RcwMarshaller.GetNativeInterface(factory, canvasEffectFactoryNative.GetAddressOf()).Assert();
// Register the factory for the current effect from the activation factory
canvasFactoryNative.Get()->RegisterEffectFactory(&effectId, canvasEffectFactoryNative.Get()).Assert();
}
/// <summary>
/// Gets the <see cref="ICanvasFactoryNative"/> activation factory.
/// </summary>
+112
ファイルの表示
@@ -0,0 +1,112 @@
using System;
using System.Runtime.CompilerServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Interop.Effects;
#if WINDOWS_UWP
using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.D2D1.Uwp.Helpers;
#else
using ComputeSharp.D2D1.WinUI.Extensions;
using ComputeSharp.D2D1.WinUI.Helpers;
#endif
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using TerraFX.Interop.WinRT;
#if WINDOWS_UWP
namespace ComputeSharp.D2D1.Uwp;
#else
namespace ComputeSharp.D2D1.WinUI;
#endif
/// <inheritdoc/>
unsafe partial class PixelShaderEffect<T>
{
/// <summary>
/// A manager type to handle the effect factory registration logic for <see cref="ICanvasEffectFactoryNative"/>.
/// </summary>
private sealed class EffectFactoryManager
{
/// <summary>
/// Lock object used to synchronize the registration logic.
/// </summary>
private readonly object lockObject = new();
/// <summary>
/// Indicates whether an effect factory for <see cref="PixelShaderEffect{T}"/> has been registered.
/// </summary>
private volatile bool isEffectFactoryRegistered;
/// <summary>
/// Creates a new <see cref="EffectFactoryManager"/> instance.
/// </summary>
private EffectFactoryManager()
{
}
/// <summary>
/// Gets the shared <see cref="EffectFactoryManager"/> instance.
/// </summary>
public static EffectFactoryManager Instance { get; } = new();
/// <summary>
/// Ensures an effect factory is registered in Win2D for the current effect type, or registers one otherwise.
/// </summary>
public void EnsureEffectFactoryIsRegistered()
{
if (this.isEffectFactoryRegistered)
{
return;
}
EnsureEffectFactoryIsRegisteredWithLock();
}
/// <summary>
/// Same as <see cref="EnsureEffectFactoryIsRegistered"/>, but with a slower path using a lock.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private void EnsureEffectFactoryIsRegisteredWithLock()
{
lock (this.lockObject)
{
if (!this.isEffectFactoryRegistered)
{
ResourceManager.RegisterEffectFactory(
effectId: PixelShaderEffect.For<T>.Instance.Id,
factory: new EffectFactory());
this.isEffectFactoryRegistered = true;
}
}
}
}
/// <summary>
/// A managed implementation of <see cref="ICanvasEffectFactoryNative"/> for <see cref="PixelShaderEffect{T}"/>.
/// </summary>
private sealed class EffectFactory : ICanvasEffectFactoryNative.Interface
{
/// <inheritdoc/>
int ICanvasEffectFactoryNative.Interface.CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
{
PixelShaderEffect<T> @this = new();
using (ComPtr<ID2D1Device1> d2D1Device1 = default)
{
// Get the underlying ID2D1Device1 instance for the input device
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
// Initialize the effect state
device->CopyTo(ref @this.canvasDevice).Assert();
d2D1Device1.CopyTo(ref @this.d2D1RealizationDevice).Assert();
resource->CopyTo(ref @this.d2D1Effect).Assert();
}
RcwMarshaller.GetNativeObject(@this, wrapper);
return S.S_OK;
}
}
}
+3
ファイルの表示
@@ -347,6 +347,9 @@ unsafe partial class PixelShaderEffect<T>
// Register the new realized effect
ResourceManager.RegisterWrapper((IUnknown*)this.d2D1Effect.Get(), this);
// Register the effect factory, if necessary
EffectFactoryManager.Instance.EnsureEffectFactoryIsRegistered();
return true;
}
+9
ファイルの表示
@@ -15,6 +15,15 @@
<NoWarn>$(NoWarn);NETSDK1023</NoWarn>
</PropertyGroup>
<!--
Bundle the .rd.xml directives as embedded resource so they're picked up automatically.
See: https://devblogs.microsoft.com/dotnet/net-native-deep-dive-making-your-library-great/.
-->
<ItemGroup>
<None Remove="Properties\ComputeSharp.D2D1.Uwp.rd.xml" />
<EmbeddedResource Include="Properties\ComputeSharp.D2D1.Uwp.rd.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Win2D.uwp" Version="1.27.0-preview3" />
</ItemGroup>
+18
ファイルの表示
@@ -0,0 +1,18 @@
<!--
Runtime directives for ComputeSharp.D2D1.Uwp.
Runtime Directives are documented at https://go.microsoft.com/fwlink/?LinkID=391919.
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="ComputeSharp.D2D1.Uwp">
<!--
Ensure that MCG generates CCWs for PixelShaderEffect<T>.EffectFactory types. By default,
this would not be the case, because the type only implements a single [ComImport] interface,
which MCG assumes would only be used for native -> RCW scenarios. As such, without adding this
directive, trying to QueryInterface on a CCW retrieved from an EffectFactory instance would
throw a MissingInteropDataException. With this instead, the object can correctly be marshalled
to native. This is necessary because the CCW is passed to Win2D as an ICanvasEffectFactoryNative.
-->
<Type Name="ComputeSharp.D2D1.Uwp.PixelShaderEffect{T}.EffectFactory" MarshalObject="All"/>
</Library>
</Directives>
+1 -1
ファイルの表示
@@ -154,7 +154,7 @@ internal unsafe partial struct PixelShaderEffect
}
/// <summary>
/// Gets the shader <see cref="For{T}"/> instance.
/// Gets the shared <see cref="For{T}"/> instance.
/// </summary>
public static For<T> Instance { get; } = CreateInstance();