Merge pull request #451 from Sergio0694/dev/win2d-effect
Add ComputeSharp.D2D1.Uwp and new PixelShaderEffect<T> type
このコミットが含まれているのは:
77ファイル変更
+4173
-242
検索条件に一致するファイルはありません
@@ -50,8 +50,10 @@ jobs:
|
||||
run: dotnet build src\ComputeSharp.Pix.Package\ComputeSharp.Pix.Package.msbuildproj -c Release
|
||||
- name: Build ComputeSharp.Uwp package
|
||||
run: msbuild src\ComputeSharp.Uwp\ComputeSharp.Uwp.csproj -t:restore,build,pack /p:Configuration=Release
|
||||
- name: Build ComputeSharp.D2D1.Uwp package
|
||||
run: msbuild src\ComputeSharp.D2D1.Uwp\ComputeSharp.D2D1.Uwp.csproj -t:restore,build,pack /p:Configuration=Release
|
||||
|
||||
# Packing needs to be done separately and with no previous build target for this to work correctly
|
||||
# WinUI 3 packaging needs to be done separately and with no previous build target for it to work correctly
|
||||
- name: Build ComputeSharp.WinUI project
|
||||
run: >
|
||||
msbuild src\ComputeSharp.WinUI\ComputeSharp.WinUI.csproj -t:restore /p:Configuration=Release;
|
||||
|
||||
@@ -132,6 +132,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B86A48A7-18B
|
||||
src\Directory.Build.targets = src\Directory.Build.targets
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.D2D1.Uwp", "src\ComputeSharp.D2D1.Uwp\ComputeSharp.D2D1.Uwp.csproj", "{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
@@ -372,6 +374,14 @@ Global
|
||||
{C12D7ACE-98ED-4813-8118-6667C34F484F}.Release|ARM64.Build.0 = Release|ARM64
|
||||
{C12D7ACE-98ED-4813-8118-6667C34F484F}.Release|x64.ActiveCfg = Release|x64
|
||||
{C12D7ACE-98ED-4813-8118-6667C34F484F}.Release|x64.Build.0 = Release|x64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Debug|ARM64.ActiveCfg = Debug|ARM64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Debug|ARM64.Build.0 = Debug|ARM64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Debug|x64.Build.0 = Debug|x64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Release|ARM64.ActiveCfg = Release|ARM64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Release|ARM64.Build.0 = Release|ARM64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Release|x64.ActiveCfg = Release|x64
|
||||
{E60A3B1E-FC5D-41AC-ACF0-C2D75B6590D2}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -40,6 +40,13 @@
|
||||
See: https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1060.
|
||||
-->
|
||||
<NoWarn>$(NoWarn);CA1060</NoWarn>
|
||||
|
||||
<!--
|
||||
Suppress ref safety warnings in unsafe contexts (see https://github.com/dotnet/csharplang/issues/6476).
|
||||
This is used eg. to replace Unsafe.SizeOf<T>() calls with just sizeof(T). The warning is not necessary
|
||||
since in order to use these APIs the caller already has to be in an unsafe context.
|
||||
-->
|
||||
<NoWarn>$(NoWarn);CS8500</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Centralized location for all generated artifacts -->
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<InternalsVisibleTo Include="ComputeSharp.Uwp" />
|
||||
<InternalsVisibleTo Include="ComputeSharp.WinUI" />
|
||||
<InternalsVisibleTo Include="ComputeSharp.D2D1" />
|
||||
<InternalsVisibleTo Include="ComputeSharp.D2D1.Uwp" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- .NET Standard 2.0 polyfill packages -->
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Buffers\BitOperations.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics.CodeAnalysis\DoesNotReturnAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics.CodeAnalysis\DisallowNullAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics.CodeAnalysis\NotNullAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Math.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace System.Diagnostics.CodeAnalysis;
|
||||
|
||||
/// <summary>
|
||||
/// Specifies that null is disallowed as an input even if the corresponding type allows it.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
|
||||
internal sealed class DisallowNullAttribute : Attribute
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/> objects.
|
||||
/// </summary>
|
||||
[Guid("A27F0B5D-EC2C-4D4F-948F-0AA1E95E33E6")]
|
||||
[NativeTypeName("class ICanvasDevice : IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasDevice
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[1])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[2])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The activation factory for <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>.
|
||||
/// </summary>
|
||||
[Guid("695C440D-04B3-4EDD-BFD9-63E51E9F7202")]
|
||||
[NativeTypeName("class ICanvasFactoryNative : public IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasFactoryNative
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[1])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[2])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates an <c>IInspectable</c> Win2D wrapper for a given native D2D resource.
|
||||
/// </summary>
|
||||
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
|
||||
/// <param name="resource">The input native resource to create a wrapper for.</param>
|
||||
/// <param name="dpi">The realization DPIs for <paramref name="resource"/></param>
|
||||
/// <param name="wrapper">The resulting wrapper for <paramref name="resource"/>.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(6)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public HRESULT GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi, [NativeTypeName("IInspectable**")] void** wrapper)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasFactoryNative*, ICanvasDevice*, IUnknown*, float, void**, int>)this.lpVtbl[6])(
|
||||
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
device,
|
||||
resource,
|
||||
dpi,
|
||||
wrapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using TerraFX.Interop;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// An interop Win2D interface for all images and effects that can be drawn.
|
||||
/// </summary>
|
||||
[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")]
|
||||
[NativeTypeName("class ICanvasImageInterop : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
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="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT"/> 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="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT"/>,
|
||||
/// <see cref="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION"/>
|
||||
/// or <see cref="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION"/>
|
||||
/// </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,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasImageInterop*, ICanvasDevice*, ID2D1DeviceContext*, WIN2D_GET_D2D_IMAGE_FLAGS, 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>
|
||||
[ComImport]
|
||||
[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface Interface
|
||||
{
|
||||
/// <inheritdoc cref="ICanvasImageInterop.GetDevice"/>
|
||||
[PreserveSig]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
int GetDevice(ICanvasDevice** device);
|
||||
|
||||
/// <inheritdoc cref="ICanvasImageInterop.GetD2DImage"/>
|
||||
[PreserveSig]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
int GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see cref="global::Microsoft.Graphics.Canvas.ICanvasResourceCreator"/> objects.
|
||||
/// </summary>
|
||||
[Guid("8F6D8AA8-492F-4BC6-B3D0-E7F5EAE84B11")]
|
||||
[NativeTypeName("class ICanvasResourceCreator : IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasResourceCreator
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[1])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[2])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// An interop wrapper type for Win2D objects (see <see href="https://microsoft.github.io/Win2D/WinUI3/html/Interop.htm"/>).
|
||||
/// </summary>
|
||||
[Guid("5F10688D-EA55-4D55-A3B0-4DDB55C0C20A")]
|
||||
[NativeTypeName("class ICanvasResourceWrapperNative : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct ICanvasResourceWrapperNative
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[1])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[2])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface provided by various Win2D objects that is able to retrieve the wrapped resource.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT GetNativeResource(ICanvasDevice* device, float dpi, [NativeTypeName("REFIID")] Guid* iid, void** resource)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ICanvasResourceWrapperNative*, ICanvasDevice*, float, Guid*, void**, int>)this.lpVtbl[3])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), device, dpi, iid, resource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
internal enum WIN2D_GET_D2D_IMAGE_FLAGS : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// No options specified.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_NONE = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Ignore the <c>targetDpi</c> parameter, read DPI from <c>deviceContext</c> instead.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Ignore the <c>targetDpi</c> parameter, always insert DPI compensation.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Ignore the <c>targetDpi</c> parameter, never insert DPI compensation.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION = 4,
|
||||
|
||||
/// <summary>
|
||||
/// Do the bare minimum to get back an <c>ID2D1Image</c>, no validation or recursive realization.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION = 8,
|
||||
|
||||
/// <summary>
|
||||
/// Allow partially configured effect graphs where some inputs are <see langword="null"/>.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS = 16,
|
||||
|
||||
/// <summary>
|
||||
/// If an input is invalid, unrealize the effect and set the output image to <see langword="null"/>.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE = 32,
|
||||
|
||||
/// <summary>
|
||||
/// Ignored.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_FORCE_DWORD = 0xffffffff
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// Native APIs from <c>Microsoft.Graphics.Canvas</c> (ie. Win2D).
|
||||
/// </summary>
|
||||
internal static unsafe class Win2D
|
||||
{
|
||||
/// <summary>
|
||||
/// Calculates the bounds for a given <see cref="ICanvasImageInterop"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="resourceCreator">The <see cref="ICanvasResourceCreator"/> object in use.</param>
|
||||
/// <param name="image">The input <see cref="ICanvasImageInterop"/> instance to compute the bounds for.</param>
|
||||
/// <param name="transform">The transform to use (optional).</param>
|
||||
/// <param name="rect">The resulting bounds.</param>
|
||||
/// <returns>The <see cref="TerraFX.Interop.Windows.HRESULT"/> for the operation.</returns>
|
||||
[DllImport("Microsoft.Graphics.Canvas.dll", PreserveSig = true, ExactSpelling = true)]
|
||||
public static extern int GetBoundsForICanvasImageInterop(
|
||||
ICanvasResourceCreator* resourceCreator,
|
||||
ICanvasImageInterop* image,
|
||||
[NativeTypeName("Windows::Foundation::Numerics::Matrix3x2 const*")] Matrix3x2* transform,
|
||||
Rect* rect);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Uwp.Extensions;
|
||||
using ComputeSharp.D2D1.Uwp.Helpers;
|
||||
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 : IDisposable
|
||||
{
|
||||
/// <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.d2D1ImageSource.Dispose();
|
||||
|
||||
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)
|
||||
{
|
||||
this.graphicsEffectSourceWrapper = ResourceManager.GetOrCreate(
|
||||
device: canvasDevice,
|
||||
resource: (IUnknown*)d2D1Image,
|
||||
dpi: 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.graphicsEffectSourceWrapper = null;
|
||||
}
|
||||
|
||||
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
|
||||
}
|
||||
|
||||
return this.graphicsEffectSourceWrapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the underlying <see cref="ID2D1Image"/> resource, if needed.
|
||||
/// This is used after a potential re-realization of a lazily bound effect.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Image">The input <see cref="ID2D1Image"/> object.</param>
|
||||
/// <returns>Whether the resource has changed or not.</returns>
|
||||
public bool UpdateResource(ID2D1Image* d2D1Image)
|
||||
{
|
||||
if (d2D1Image->IsSameInstance(this.d2D1ImageSource.Get()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.d2D1ImageSource.Dispose();
|
||||
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.d2D1ImageSource.Dispose();
|
||||
this.d2D1EffectDpiCompensation.Dispose();
|
||||
this.graphicsEffectSourceWrapper = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// Shared helpers for all types implementing <see cref="IFixedCountList{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
internal static class FixedCountList<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies the entire <see cref="IFixedCountList{T}"/> instance to a target array range.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <param name="array">The array to copy elements to.</param>
|
||||
/// <param name="index">The initial index within <paramref name="array"/>.</param>
|
||||
public static void CopyTo(IFixedCountList<T> list, T[] array, int index)
|
||||
{
|
||||
if (array is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(array), "The input array cannot be null.");
|
||||
}
|
||||
|
||||
Span<T> span = array.AsSpan(index);
|
||||
|
||||
if (list.Indices.Length > span.Length)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentException(nameof(array), "The destination array range is smaller than the number of source items.");
|
||||
}
|
||||
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
span[i] = list[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the entire <see cref="IFixedCountList{T}"/> instance to a target array range.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <param name="array">The array to copy elements to.</param>
|
||||
/// <param name="index">The initial index within <paramref name="array"/>.</param>
|
||||
public static void CopyTo(IFixedCountList<T> list, Array array, int index)
|
||||
{
|
||||
if (array is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(array), "The input array cannot be null.");
|
||||
}
|
||||
|
||||
if ((uint)index >= array.Length)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The starting array index is out of range.");
|
||||
}
|
||||
|
||||
int remainingLength = array.Length - index;
|
||||
|
||||
if (list.Indices.Length > remainingLength)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentException(nameof(array), "The destination array range is smaller than the number of source items.");
|
||||
}
|
||||
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
array.SetValue(list[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="IEnumerable{T}"/> for an input <see cref="IFixedCountList{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <returns>An <see cref="IEnumerable{T}"/> instance for <paramref name="list"/>.</returns>
|
||||
public static IEnumerator<T> GetEnumerator(IFixedCountList<T> list)
|
||||
{
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
yield return list[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches for the specified object and returns the zero-based index of the first occurrence
|
||||
/// within the range of elements in a given <see cref="IFixedCountList{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <param name="value">
|
||||
/// The object to locate in <paramref name="list"/>. The value can be <see langword="null"/> for reference types.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The zero-based index of the first occurrence of item within <paramref name="list"/>, otherwise -1.
|
||||
/// </returns>
|
||||
public static int IndexOf(IFixedCountList<T> list, T value)
|
||||
{
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(list[i], value))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for a list with a fixed collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
interface IFixedCountList<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
ImmutableArray<int> Indices { get; }
|
||||
|
||||
/// <inheritdoc cref="IList{T}.this"/>
|
||||
T this[int index] { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<Project Sdk="MSBuild.Sdk.Extras">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>uap10.0.17763</TargetFrameworks>
|
||||
<TargetPlatformBaseVersion>10.0</TargetPlatformBaseVersion>
|
||||
<TargetPlatformRevision>19041</TargetPlatformRevision>
|
||||
<TargetPlatformMinRevision>17763</TargetPlatformMinRevision>
|
||||
<TargetPlatformVersion>$(TargetPlatformBaseVersion).$(TargetPlatformRevision).0</TargetPlatformVersion>
|
||||
<TargetPlatformMinVersion>$(TargetPlatformBaseVersion).$(TargetPlatformMinRevision).0</TargetPlatformMinVersion>
|
||||
<DebugType>Portable</DebugType>
|
||||
<GenerateLibraryLayout>true</GenerateLibraryLayout>
|
||||
<IsPackable>true</IsPackable>
|
||||
<IsPublishable>true</IsPublishable>
|
||||
<ExtrasEnableDefaultXamlItems>true</ExtrasEnableDefaultXamlItems>
|
||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
|
||||
<RootNamespace>ComputeSharp.D2D1.Uwp</RootNamespace>
|
||||
<Platforms>x64;ARM64</Platforms>
|
||||
<RuntimeIdentifiers>win10-x64;win-arm64</RuntimeIdentifiers>
|
||||
<NoWarn>$(NoWarn);NETSDK1023</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<PackageId>ComputeSharp.D2D1.Uwp</PackageId>
|
||||
<Title>ComputeSharp.D2D1.Uwp</Title>
|
||||
<Description>A UWP library with APIs to leverage D2D1 functionality with D2D1 pixel shaders powered by ComputeSharp.D2D1</Description>
|
||||
<PackageTags>$(PackageTags) uwp winui</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\ComputeSharp\Graphics\Interop\IReferenceTrackedObject.cs" Link="Interop\IReferenceTrackedObject.cs" />
|
||||
<Compile Include="..\ComputeSharp\Graphics\Interop\ReferenceTracker.cs" Link="Interop\ReferenceTracker.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\ComputeSharp.Core.Package\icon.png" Pack="true" PackagePath="\icon.png" Visible="False" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.14" />
|
||||
<PackageReference Include="Win2D.uwp" Version="1.27.0-preview1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ComputeSharp.D2D1\ComputeSharp.D2D1.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<Project>
|
||||
<Import Project="..\..\Directory.Build.props" />
|
||||
</Project>
|
||||
@@ -0,0 +1,59 @@
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Win32 = TerraFX.Interop.Windows.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="ICanvasDevice"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class ICanvasDeviceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the underlying <see cref="ID2D1Device1"/> object from an <see cref="ICanvasDevice"/> object.
|
||||
/// </summary>
|
||||
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object.</param>
|
||||
/// <param name="d2D1Device1">The underlying <see cref="ID2D1Device1"/> object for <paramref name="canvasDevice"/>.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
public static HRESULT GetD2DDevice(this ref ICanvasDevice canvasDevice, ID2D1Device1** d2D1Device1)
|
||||
{
|
||||
using ComPtr<ICanvasResourceWrapperNative> canvasDeviceResourceWrapperNative = default;
|
||||
|
||||
// Get the ICanvasResourceWrapperNative wrapper around the input device
|
||||
canvasDevice.QueryInterface(
|
||||
riid: Win32.__uuidof<ICanvasResourceWrapperNative>(),
|
||||
ppvObject: canvasDeviceResourceWrapperNative.GetVoidAddressOf()).Assert();
|
||||
|
||||
// Get the underlying ID2D1Device1 instance in use
|
||||
HRESULT hresult = canvasDeviceResourceWrapperNative.Get()->GetNativeResource(
|
||||
device: null,
|
||||
dpi: 0,
|
||||
iid: Win32.__uuidof<ID2D1Device1>(),
|
||||
resource: (void**)d2D1Device1);
|
||||
|
||||
return hresult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ID2D1DeviceContext"/> from an input <see cref="ICanvasDevice"/>.
|
||||
/// </summary>
|
||||
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object.</param>
|
||||
/// <param name="d2D1DeviceContext">The resulting <see cref="ID2D1DeviceContext"/> object.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
public static HRESULT CreateD2DDeviceContext(this ref ICanvasDevice canvasDevice, ID2D1DeviceContext** d2D1DeviceContext)
|
||||
{
|
||||
using ComPtr<ID2D1Device1> d2D1Device1 = default;
|
||||
|
||||
// Get the underlying ID2D1Device1 object
|
||||
canvasDevice.GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
|
||||
|
||||
// Create the new device context
|
||||
HRESULT hresult = d2D1Device1.Get()->CreateDeviceContext(
|
||||
options: D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
|
||||
deviceContext: d2D1DeviceContext);
|
||||
|
||||
return hresult;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
|
||||
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="ID2D1Effect"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class ID2D1EffectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the constant buffer of type <typeparamref name="T"/> from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader being used.</typeparam>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The constant buffer of type <typeparamref name="T"/> for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static T GetConstantBuffer<T>(this ref ID2D1Effect d2D1Effect)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
int constantBufferSize = D2D1PixelShader.GetConstantBufferSize<T>();
|
||||
byte[] buffer = ArrayPool<byte>.Shared.Rent(constantBufferSize);
|
||||
|
||||
fixed (byte* p = buffer)
|
||||
{
|
||||
// Get the raw constant buffer from the effect
|
||||
d2D1Effect.GetValue(
|
||||
index: D2D1PixelShaderEffectProperty.ConstantBuffer,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB,
|
||||
data: p,
|
||||
dataSize: (uint)constantBufferSize).Assert();
|
||||
}
|
||||
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.InitializeFromDispatchData(buffer.AsSpan(0, constantBufferSize));
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1TransformMapper{T}"/> instance from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader being used.</typeparam>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The <see cref="D2D1TransformMapper{T}"/> instance for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static D2D1TransformMapper<T>? GetTransformMapper<T>(this ref ID2D1Effect d2D1Effect)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
using ComPtr<ID2D1TransformMapper> d2D1TransformMapper = default;
|
||||
|
||||
// Get the ID2D1TransformMapper object from the effect
|
||||
d2D1Effect.GetValue(
|
||||
index: D2D1PixelShaderEffectProperty.TransformMapper,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN,
|
||||
data: (byte*)d2D1TransformMapper.GetAddressOf(),
|
||||
dataSize: (uint)sizeof(void*)).Assert();
|
||||
|
||||
// If there is no transform, just return null (this is the default state)
|
||||
if (d2D1TransformMapper.Get() is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Otherwise, check if it's an internal CCW and retrieve the managed wrapper
|
||||
using ComPtr<ID2D1TransformMapperInternal> d2D1TransformMapperInternal = default;
|
||||
|
||||
d2D1TransformMapper.CopyTo(d2D1TransformMapperInternal.GetAddressOf()).Assert();
|
||||
|
||||
IntPtr handlePtr;
|
||||
|
||||
// Get the underlying GCHandle value
|
||||
d2D1TransformMapperInternal.Get()->GetManagedWrapperHandle((void**)&handlePtr).Assert();
|
||||
|
||||
// Retrieve the managed wrapper from the GCHandle
|
||||
return (D2D1TransformMapper<T>)GCHandle.FromIntPtr(handlePtr).Target!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1ResourceTextureManager"/> instance from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="source">The current cached source instance.</param>
|
||||
/// <param name="index">The index of the resource texture manager to retrieve.</param>
|
||||
/// <returns>The <see cref="D2D1ResourceTextureManager"/> instance for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static D2D1ResourceTextureManager? GetResourceTextureManager(
|
||||
this ref ID2D1Effect d2D1Effect,
|
||||
D2D1ResourceTextureManager? source,
|
||||
int index)
|
||||
{
|
||||
using ComPtr<ID2D1ResourceTextureManager> d2D1ResourceTextureManager = default;
|
||||
|
||||
// Get the ID2D1ResourceTextureManager object from the effect
|
||||
d2D1Effect.GetValue(
|
||||
index: (uint)(D2D1PixelShaderEffectProperty.ResourceTextureManager0 + index),
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN,
|
||||
data: (byte*)d2D1ResourceTextureManager.GetAddressOf(),
|
||||
dataSize: (uint)sizeof(void*)).Assert();
|
||||
|
||||
// If there is no resource texture manager, just return null (this is the default state)
|
||||
if (d2D1ResourceTextureManager.Get() is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Otherwise, the source must not be null and wrapping the same CCW
|
||||
if (source is not null)
|
||||
{
|
||||
using ComPtr<ID2D1ResourceTextureManager> d2D1ResourceTextureManagerSource = default;
|
||||
|
||||
source.GetD2D1ResourceTextureManager(d2D1ResourceTextureManagerSource.GetAddressOf());
|
||||
|
||||
// Check if the instances match, and return the managed wrapper we already have
|
||||
if (d2D1ResourceTextureManager.Get()->IsSameInstance(d2D1ResourceTextureManagerSource.Get()))
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
// There was no way to retrieve the correct managed wrapper
|
||||
ThrowHelper.ThrowInvalidOperationException("Cannot retrieve a D2D1ResourceTextureManager instance wrapping a native object that was set externally.");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> value for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static bool GetCachedProperty(this ref ID2D1Effect d2D1Effect)
|
||||
{
|
||||
int cacheOutput;
|
||||
|
||||
d2D1Effect.GetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL,
|
||||
data: (byte*)&cacheOutput,
|
||||
dataSize: sizeof(int)).Assert();
|
||||
|
||||
return cacheOutput != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="value">The property value to set.</param>
|
||||
public static void SetCachedProperty(this ref ID2D1Effect d2D1Effect, bool value)
|
||||
{
|
||||
int cacheOutput = value ? 1 : 0;
|
||||
|
||||
d2D1Effect.SetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL,
|
||||
data: (byte*)&cacheOutput,
|
||||
dataSize: sizeof(int)).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> value for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static D2D1_BUFFER_PRECISION GetPrecisionProperty(this ref ID2D1Effect d2D1Effect)
|
||||
{
|
||||
D2D1_BUFFER_PRECISION d2D1BufferPrecision;
|
||||
|
||||
d2D1Effect.GetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2D1BufferPrecision,
|
||||
dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert();
|
||||
|
||||
return d2D1BufferPrecision;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="value">The property value to set.</param>
|
||||
public static void SetPrecisionProperty(this ref ID2D1Effect d2D1Effect, D2D1_BUFFER_PRECISION value)
|
||||
{
|
||||
d2D1Effect.SetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&value,
|
||||
dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="ID2D1DeviceContext"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class ID2D1DeviceContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether a given <see cref="ID2D1DeviceContext"/> has an <see cref="ID2D1CommandList"/> as render target.
|
||||
/// </summary>
|
||||
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance to check.</param>
|
||||
/// <returns>Whether the target of <paramref name="deviceContext"/> is an <see cref="ID2D1CommandList"/>.</returns>
|
||||
public static bool HasCommandListTarget(this ref ID2D1DeviceContext deviceContext)
|
||||
{
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
// First get the target and check that it is not null
|
||||
deviceContext.GetTarget(d2D1Image.GetAddressOf());
|
||||
|
||||
if (d2D1Image.Get() is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1CommandList> d2D1CommandList = default;
|
||||
|
||||
// If there is a target, check if it is an ID2D1CommandList
|
||||
HRESULT hresult = d2D1Image.CopyTo(d2D1CommandList.GetAddressOf());
|
||||
|
||||
// Special case E_NOINTERFACE: this should return false and it's not an error
|
||||
if (hresult == E.E_NOINTERFACE)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Throw for any other failure
|
||||
hresult.Assert();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the DPI value from a target <see cref="ID2D1DeviceContext"/> object.
|
||||
/// </summary>
|
||||
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance.</param>
|
||||
/// <returns>The DPIs from <paramref name="deviceContext"/>.</returns>
|
||||
public static float GetDpi(this ref ID2D1DeviceContext deviceContext)
|
||||
{
|
||||
float dpiX;
|
||||
float dpiY;
|
||||
|
||||
deviceContext.GetDpi(&dpiX, &dpiY);
|
||||
|
||||
return dpiX;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Win32 = TerraFX.Interop.Windows.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for working with the <see cref="IUnknown"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class IUnknownExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether two <see cref="IUnknown"/> values have the same underlying COM object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the first value to check.</typeparam>
|
||||
/// <typeparam name="U">The type of the second value to check.</typeparam>
|
||||
/// <param name="left">The first <see cref="IUnknown"/> object to compare.</param>
|
||||
/// <param name="right">The second <see cref="IUnknown"/> object to compare.</param>
|
||||
/// <returns>Whether <paramref name="left"/> and <paramref name="right"/> point to the same COM object.</returns>
|
||||
public static bool IsSameInstance<T, U>(this ref T left, U* right)
|
||||
where T : unmanaged // IUnknown
|
||||
where U : unmanaged // IUnknown
|
||||
{
|
||||
if (Unsafe.AreSame(ref left, ref *(T*)right))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Unsafe.IsNullRef(ref left) || right is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using ComPtr<IUnknown> leftUnknown = default;
|
||||
using ComPtr<IUnknown> rightUnknown = default;
|
||||
|
||||
((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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the source <see cref="IUnknown"/> object onto a target <see cref="ComPtr{T}"/> destination.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object being copied.</typeparam>
|
||||
/// <param name="source">The source <see cref="IUnknown"/> object.</param>
|
||||
/// <param name="destination">The destination <see cref="ComPtr{T}"/> location.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation (always <see cref="S.S_OK"/>).</returns>
|
||||
public static HRESULT CopyTo<T>(this ref T source, ref ComPtr<T> destination)
|
||||
where T : unmanaged // IUnknown
|
||||
{
|
||||
// Note: this explicitly needs a temporary copy to avoid issues in case source and destination were
|
||||
// pointers to the same object (which can be the case even if the two pointers are not identical).
|
||||
// To double check, a full check through an IUnknown* cast would be needed, but to avoid the extra
|
||||
// QueryInterface call on both objects, we can just always do one AddRef call which avoids issues in
|
||||
// all cases as well. The situation we need to avoid is that without a temporary copy, releasing the
|
||||
// target object might actually destroy the object, causing the following AddRef call to AV.
|
||||
using ComPtr<T> temporary = new((T*)Unsafe.AsPointer(ref source));
|
||||
|
||||
return temporary.CopyTo(ref destination);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using TerraFX.Interop.Windows;
|
||||
using TerraFX.Interop.WinRT;
|
||||
using Windows.Graphics.Effects;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A helper type to replicate Win2D's <c>ResourceManager</c> type.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type provides an entry point to use the Win2D <see cref="ICanvasFactoryNative"/> activation factory.
|
||||
/// </remarks>
|
||||
internal static class ResourceManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or creates an <see cref="IGraphicsEffectSource"/> instance for a native resource.
|
||||
/// </summary>
|
||||
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
|
||||
/// <param name="resource">The input native resource to create a wrapper for.</param>
|
||||
/// <param name="dpi">The realization DPIs for <paramref name="resource"/></param>
|
||||
/// <returns>The resulting <see cref="IGraphicsEffectSource"/> wrapper instance.</returns>
|
||||
/// <exception cref="Exception">Thrown if the managed wrapper could not be retrieved.</exception>
|
||||
public static unsafe IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
|
||||
{
|
||||
const string activatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice";
|
||||
|
||||
fixed (char* pActivatableClassId = activatableClassId)
|
||||
{
|
||||
HSTRING_HEADER hStringHeaderActivatableClassId;
|
||||
HSTRING hStringActivatableClass;
|
||||
|
||||
// Create a fast-pass HSTRING for "Microsoft.Graphics.Canvas.CanvasDevice"
|
||||
WinRT.WindowsCreateStringReference(
|
||||
sourceString: (ushort*)pActivatableClassId,
|
||||
length: (uint)activatableClassId.Length,
|
||||
hstringHeader: &hStringHeaderActivatableClassId,
|
||||
@string: &hStringActivatableClass).Assert();
|
||||
|
||||
Guid canvasFactoryNativeId = typeof(ICanvasFactoryNative).GUID;
|
||||
|
||||
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
|
||||
|
||||
// Get the activation factory for CanvasDevice
|
||||
WinRT.RoGetActivationFactory(
|
||||
activatableClassId: hStringActivatableClass,
|
||||
iid: &canvasFactoryNativeId,
|
||||
factory: canvasFactoryNative.GetVoidAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IUnknown> wrapperUnknown = default;
|
||||
|
||||
// Get or create a WinRT wrapper for the resource
|
||||
canvasFactoryNative.Get()->GetOrCreate(
|
||||
device: device,
|
||||
resource: resource,
|
||||
dpi: dpi,
|
||||
wrapper: wrapperUnknown.GetVoidAddressOf()).Assert();
|
||||
|
||||
// Get the runtime-provided RCW for the resulting WinRT wrapper
|
||||
object wrapper = Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get());
|
||||
|
||||
return (IGraphicsEffectSource)wrapper;
|
||||
}
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Loaders;
|
||||
|
||||
/// <summary>
|
||||
/// A resource texture index array loader for D2D1 pixel shaders.
|
||||
/// </summary>
|
||||
internal struct D2D1ImmutableArrayResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The array of indices for resource textures for the current effect.
|
||||
/// </summary>
|
||||
private ImmutableArray<int> indices;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resulting array of indices for resource textures for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>An array of indices for resource textures for the current effect.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImmutableArray<int> GetResultingIndices()
|
||||
{
|
||||
return this.indices;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
|
||||
{
|
||||
if (data.IsEmpty)
|
||||
{
|
||||
this.indices = ImmutableArray<int>.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadOnlySpan<D2D1ResourceTextureDescription> descriptions = MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data);
|
||||
|
||||
int[] indices = new int[descriptions.Length];
|
||||
|
||||
for (int i = 0; i < descriptions.Length; i++)
|
||||
{
|
||||
indices[i] = descriptions[i].Index;
|
||||
}
|
||||
|
||||
this.indices = *(ImmutableArray<int>*)&indices;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Loaders;
|
||||
|
||||
/// <summary>
|
||||
/// A resource texture index bitmask loader for D2D1 pixel shaders.
|
||||
/// </summary>
|
||||
internal struct D2D1IndexBitmaskResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
private int indexBitmask;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resulting bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
/// <returns>A bitmask of indices for resource textures in the target shader.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int GetResultingIndexBitmask()
|
||||
{
|
||||
return this.indexBitmask;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
|
||||
{
|
||||
int indexBitmask = 0;
|
||||
|
||||
foreach (ref readonly D2D1ResourceTextureDescription description in MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data))
|
||||
{
|
||||
indexBitmask |= 1 << description.Index;
|
||||
}
|
||||
|
||||
this.indexBitmask = indexBitmask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.Effects;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
void ICanvasEffect.InvalidateSourceRectangle(ICanvasResourceCreatorWithDpi resourceCreator, uint sourceIndex, Rect invalidRectangle)
|
||||
{
|
||||
throw new NotSupportedException("ICanvasEffect.InvalidateSourceRectangle is not currently supported.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
Rect[] ICanvasEffect.GetInvalidRectangles(ICanvasResourceCreatorWithDpi resourceCreator)
|
||||
{
|
||||
throw new NotSupportedException("ICanvasEffect.GetInvalidRectangles is not currently supported.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
Rect ICanvasEffect.GetRequiredSourceRectangle(
|
||||
ICanvasResourceCreatorWithDpi resourceCreator,
|
||||
Rect outputRectangle,
|
||||
ICanvasEffect sourceEffect,
|
||||
uint sourceIndex,
|
||||
Rect sourceBounds)
|
||||
{
|
||||
throw new NotSupportedException("ICanvasEffect.GetRequiredSourceRectangle is not currently supported.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
Rect[] ICanvasEffect.GetRequiredSourceRectangles(
|
||||
ICanvasResourceCreatorWithDpi resourceCreator,
|
||||
Rect outputRectangle,
|
||||
ICanvasEffect[] sourceEffects,
|
||||
uint[] sourceIndices,
|
||||
Rect[] sourceBounds)
|
||||
{
|
||||
throw new NotSupportedException("ICanvasEffect.GetRequiredSourceRectangles is not currently supported.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Foundation;
|
||||
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator)
|
||||
{
|
||||
return GetBounds(resourceCreator, null);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform)
|
||||
{
|
||||
return GetBounds(resourceCreator, &transform);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ICanvasImage.GetBounds(ICanvasResourceCreator, Matrix3x2)"/>
|
||||
private Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2* transform)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<IUnknown> canvasResourceCreatorUnknown = default;
|
||||
using ComPtr<IUnknown> canvasImageInteropUnknown = default;
|
||||
|
||||
// Get the IUnknown* references for the ICanvasResourceCreator and ICanvasImageInterop objects
|
||||
canvasResourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator));
|
||||
canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this));
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator> canvasResourceCreator = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Get the ICanvasResourceCreator* and ICanvasImageInterop* pointers
|
||||
canvasResourceCreatorUnknown.CopyTo(canvasResourceCreator.GetAddressOf()).Assert();
|
||||
canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
Rect bounds;
|
||||
|
||||
// Forward the actual logic to Win2D to compute the image bounds (it needs the internal context)
|
||||
Win2D.GetBoundsForICanvasImageInterop(
|
||||
resourceCreator: canvasResourceCreator.Get(),
|
||||
image: canvasImageInterop.Get(),
|
||||
transform: transform,
|
||||
rect: &bounds).Assert();
|
||||
|
||||
return bounds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Interop.Effects;
|
||||
using ComputeSharp.D2D1.Uwp.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Graphics.Effects;
|
||||
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken);
|
||||
|
||||
// Check for disposal
|
||||
if (!leaseTaken)
|
||||
{
|
||||
return RO.RO_E_CLOSED;
|
||||
}
|
||||
|
||||
// Ensure we can take a lock without blocking. We can't access the realization device with no
|
||||
// lock, as that could lead to memory leaks or corruption if GetD2DImage was to manipulate that
|
||||
// field concurrently. We also cannot lock directly, because that would deadlock if someone was
|
||||
// to (incorrectly) try to use the same effect from two different threads. To protect against
|
||||
// that, we try to lock, and just return E_NOT_VALID_STATE if another thread holds the lock. If
|
||||
// this call had GetD2DImage in the stack, that would work fine because locks are recursive.
|
||||
if (!Monitor.TryEnter(this.lockObject))
|
||||
{
|
||||
return E.E_NOT_VALID_STATE;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Copy the device over to the target
|
||||
return this.canvasDevice.CopyTo(device);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(this.lockObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int ICanvasImageInterop.Interface.GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken);
|
||||
|
||||
// Check for disposal
|
||||
if (!leaseTaken)
|
||||
{
|
||||
return RO.RO_E_CLOSED;
|
||||
}
|
||||
|
||||
// Check for graph cycles
|
||||
if (Interlocked.CompareExchange(ref this.isInsideGetD2DImage, 1, 0) == 1)
|
||||
{
|
||||
return D2DERR.D2DERR_CYCLIC_GRAPH;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Lock after checking for graph cycles (other public APIs also lock on this object)
|
||||
lock (this.lockObject)
|
||||
{
|
||||
// Process the ReadDpiFromDeviceContext flag (same logic as CanvasEffect::GetD2DImage)
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
// If ReadDpiFromDeviceContext is used, the context cannot be null
|
||||
if (deviceContext is null)
|
||||
{
|
||||
return E.E_POINTER;
|
||||
}
|
||||
|
||||
if (deviceContext->HasCommandListTarget())
|
||||
{
|
||||
// Command lists are DPI dependent, so DPI compensation effects are always needed
|
||||
flags |= WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the effect is not being drawn onto a command list, the DPIs can be read from the device context
|
||||
targetDpi = deviceContext->GetDpi();
|
||||
}
|
||||
|
||||
// ReadDpiFromDeviceContext has been processed, so it can be removed now
|
||||
flags &= ~WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT;
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1Device1> d2D1Device1 = default;
|
||||
|
||||
// Get the underlying ID2D1Device1 instance in use
|
||||
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
|
||||
|
||||
// Check that the input D2D device is the same instance as the current realization device, if any.
|
||||
// If not and the effect has already been realized, unrealize it and store the new device to use.
|
||||
if (!d2D1Device1.Get()->IsSameInstance(this.d2D1RealizationDevice.Get()))
|
||||
{
|
||||
Unrealize();
|
||||
|
||||
// Store the input realization device and canvas device for future uses
|
||||
d2D1Device1.CopyTo(ref this.d2D1RealizationDevice).Assert();
|
||||
device->CopyTo(ref this.canvasDevice).Assert();
|
||||
}
|
||||
|
||||
// Create the effect, if it doesn't exist already
|
||||
if (this.d2D1Effect.Get() is null)
|
||||
{
|
||||
if (!Realize(flags, targetDpi, deviceContext))
|
||||
{
|
||||
*ppImage = null;
|
||||
|
||||
return E.E_FAIL;
|
||||
}
|
||||
}
|
||||
else if (!flags.HasFlag(WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION))
|
||||
{
|
||||
// Recurse through the effect graph and ensure all nodes are initialized
|
||||
RefreshInputs(flags, targetDpi, deviceContext);
|
||||
}
|
||||
|
||||
// The realized DPIs are always 0 for pixel shader effects
|
||||
if (realizeDpi is not null)
|
||||
{
|
||||
*realizeDpi = 0;
|
||||
}
|
||||
|
||||
this.d2D1Effect.CopyTo(ppImage).Assert();
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
*ppImage = null;
|
||||
|
||||
return e.HResult;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure that reset this flag so that this method can be called again
|
||||
this.isInsideGetD2DImage = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unrealizes the currently realized <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="source">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 Unrealize(IGraphicsEffectSource source, int index)
|
||||
{
|
||||
// Unrealize the effect and indicate to the logic to skip the source at the current index.
|
||||
// That is, there is no need to marshal it back to the cache, as we're about to do that here.
|
||||
Unrealize(index);
|
||||
|
||||
// Once the effect is unrealized, also update the cached source managed wrapper
|
||||
Sources.Storage[index].SetWrapper(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unrealizes the currently realized <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
private void Unrealize(int? sourceIndexToSkip = null)
|
||||
{
|
||||
// If there is no ID2D1Effect object, there is nothing left to do
|
||||
if (this.d2D1Effect.Get() is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache the last update values for the constant buffer, cache output, buffer precision properties, etc.
|
||||
// Note: the resource texture managers are deliberately not read back, as the only supported way to set
|
||||
// those is directly from the current effect instance. As such, they'd all either be the same instances
|
||||
// that are already stored locally, or trying to retrieve them would just throw an exception.
|
||||
this.constantBuffer = this.d2D1Effect.Get()->GetConstantBuffer<T>();
|
||||
this.transformMapper = this.d2D1Effect.Get()->GetTransformMapper<T>();
|
||||
this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty();
|
||||
this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty();
|
||||
|
||||
// Loop over all effect inputs and update the cache back as well
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
// If this index has been explicitly requested to be skipped, just dispose the source reference.
|
||||
// This is the case when the effect is being unrealized, and callers will set this value on their own.
|
||||
if (i == sourceIndexToSkip)
|
||||
{
|
||||
Sources.Storage[i].Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just get the input, which will automatically update the cached values. The
|
||||
// returned IGraphicsEffectSource managed wrapper is not used here and is safe to ignore.
|
||||
_ = GetD2DInput(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Finally release the effect as well
|
||||
this.d2D1Effect.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Realizes the underlying <see cref="ID2D1Effect"/> for this object.
|
||||
/// </summary>
|
||||
/// <param name="flags">The current flags in use.</param>
|
||||
/// <param name="targetDpi">The target DPI in use.</param>
|
||||
/// <param name="deviceContext">The <see cref="ID2D1DeviceContext"/> instance in use.</param>
|
||||
/// <returns>Whether the effect was realized correctly.</returns>
|
||||
private bool Realize(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
|
||||
{
|
||||
using ComPtr<ID2D1Factory> d2D1Factory = default;
|
||||
|
||||
// Get the underlying ID2D1Factory from the input context, so we can register and create the effect
|
||||
deviceContext->GetFactory(d2D1Factory.GetAddressOf());
|
||||
|
||||
using ComPtr<ID2D1Factory1> d2D1Factory1 = default;
|
||||
|
||||
// D2D1PixelShaderEffect APIs specifically need an ID2D1Factory1 (as ID2D1Factory1::RegisterEffectFromString is used)
|
||||
d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert();
|
||||
|
||||
fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect)
|
||||
{
|
||||
Guid effectId = PixelShaderEffect.For<T>.Instance.Id;
|
||||
|
||||
// Try to create an instance of the effect in use and store it in the current object
|
||||
HRESULT hresult = deviceContext->CreateEffect(effectId: &effectId, effect: d2D1Effect);
|
||||
|
||||
// Check if creation failed due to the effect not being registered. In that case, register
|
||||
// it and then try again. This is much faster than check whether the effect is registered
|
||||
// manually every time, by enumerating all registered effects for the target device context.
|
||||
// An unregistered effect can also sometimes return E_NOTFOUND, although it's not documented.
|
||||
if (hresult == D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED ||
|
||||
hresult == E.E_NOTFOUND)
|
||||
{
|
||||
// Register the effect with the factory (pass the same D2D1 draw transform mapper factory that was used before)
|
||||
D2D1PixelShaderEffect.RegisterForD2D1Factory1<T>(d2D1Factory1.Get(), out _);
|
||||
|
||||
// Try to create the effect again
|
||||
hresult = deviceContext->CreateEffect(effectId: &effectId, effect: d2D1Effect);
|
||||
}
|
||||
|
||||
// Rethrow any other errors
|
||||
hresult.Assert();
|
||||
}
|
||||
|
||||
// Set the constant buffer for the effect
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.d2D1Effect.Get(), in this.constantBuffer);
|
||||
|
||||
// If there is a transform mapper, set it in the effect
|
||||
if (this.transformMapper is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(this.d2D1Effect.Get(), this.transformMapper);
|
||||
}
|
||||
|
||||
// If cache input has been set, forward that to the new effect
|
||||
if (this.cacheOutput)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetCachedProperty(true);
|
||||
}
|
||||
|
||||
// If the buffer precision isn't unknown, forward that as well
|
||||
if (this.d2D1BufferPrecision != D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
|
||||
}
|
||||
|
||||
// Forward all available sources (the effect is being created now, so no need to validate the underlying resources)
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
if (!SetD2DInput(
|
||||
index: i,
|
||||
value: Sources.Storage[i].GetWrapper(),
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
d2D1DeviceContext: deviceContext))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Set all available resource texture managers (only set those that are not null, as they're all null by default anyway).
|
||||
// The loop only goes over the indices of valid slots for resource textures for the current effect, and ignores the others.
|
||||
foreach (int index in ResourceTextureManagerCollection.Indices)
|
||||
{
|
||||
if (ResourceTextureManagers.Storage[index] is D2D1ResourceTextureManager resourceTextureManager)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, index);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the draw state for the currently realized effect.
|
||||
/// </summary>
|
||||
/// <param name="flags">The current flags in use.</param>
|
||||
/// <param name="targetDpi">The target DPI in use.</param>
|
||||
/// <param name="deviceContext">The <see cref="ID2D1DeviceContext"/> instance in use.</param>
|
||||
private void RefreshInputs(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
|
||||
{
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
// Retrieve the managed wrapper for the current source
|
||||
IGraphicsEffectSource? source = GetD2DInput(i);
|
||||
|
||||
if (source is null)
|
||||
{
|
||||
// If the source is null and that is not valid here (eg. when drawing), just throw
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
ThrowHelper.ThrowInvalidOperationException("The effect has one or more sources set to null, and null effect inputs are not valid at this time.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using ComPtr<IUnknown> canvasImageInteropUnknown = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Get the underlying IUnknown object for the current source
|
||||
canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(source));
|
||||
|
||||
// Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't)
|
||||
canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
float realizedDpi;
|
||||
|
||||
// Invoke GetD2DImage on the underlying object (either a built-in effect or a custom one)
|
||||
HRESULT hresult = canvasImageInterop.Get()->GetD2DImage(
|
||||
device: this.canvasDevice.Get(),
|
||||
deviceContext: deviceContext,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
realizeDpi: &realizedDpi,
|
||||
ppImage: d2D1Image.GetAddressOf());
|
||||
|
||||
// To match the behavior of ICanvasImageInternal::GetD2DImage in case of failure, check if the flags being used did have the flag
|
||||
// WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE set. If not, and the call failed, then we explicitly throw from the returned HRESULT.
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
Marshal.ThrowExceptionForHR(hresult);
|
||||
}
|
||||
|
||||
bool isDifferentResource = Sources.Storage[i].UpdateResource(d2D1Image.Get());
|
||||
|
||||
bool isDifferentDpi = ApplyDpiCompensation(
|
||||
index: i,
|
||||
d2D1Image: ref *&d2D1Image,
|
||||
inputDpi: realizedDpi,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
d2D1DeviceContext: deviceContext);
|
||||
|
||||
// If the source or the DPI setting has changed, also update the D2D effect graph
|
||||
if (isDifferentResource || isDifferentDpi)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetInput(index: (uint)i, input: d2D1Image.Get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Uwp.Buffers;
|
||||
using ComputeSharp.D2D1.Uwp.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Graphics.Effects;
|
||||
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
|
||||
using static TerraFX.Interop.DirectX.D2D1_BORDER_MODE;
|
||||
using static TerraFX.Interop.DirectX.D2D1_DPICOMPENSATION_INTERPOLATION_MODE;
|
||||
using static TerraFX.Interop.DirectX.D2D1_DPICOMPENSATION_PROP;
|
||||
using static TerraFX.Interop.DirectX.D2D1_PROPERTY_TYPE;
|
||||
using Win32 = TerraFX.Interop.Windows.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
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)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => GetD2DInput(index),
|
||||
_ => Sources.Storage[index].GetWrapper()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 WIN2D_GET_D2D_IMAGE_FLAGS flags =
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION |
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS |
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE;
|
||||
|
||||
// The WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE flag is passed, so we just let the effect
|
||||
// unrealize in case of failure and take no other action (including throwing) if that happens.
|
||||
_ = SetD2DInput(index, value, flags, targetDpi: 0.0f, d2D1DeviceContext: null);
|
||||
}
|
||||
else
|
||||
{
|
||||
source.SetWrapper(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <remarks>This method assumes a lock is taken and the effect is already realized.</remarks>
|
||||
/// <inheritdoc cref="GetSource"/>
|
||||
private IGraphicsEffectSource? GetD2DInput(int index)
|
||||
{
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
// Get the ID2D1Image input from the effect
|
||||
this.d2D1Effect.Get()->GetInput(
|
||||
index: (uint)index,
|
||||
input: d2D1Image.GetAddressOf());
|
||||
|
||||
ref SourceReference sourceReference = ref Sources.Storage[index];
|
||||
|
||||
// Check whether the input had a DPI compensation effect added to it, and skip it if so
|
||||
if (sourceReference.HasDpiCompensationEffect)
|
||||
{
|
||||
// Since a realized effect is the only source of authority for the effect state, we also
|
||||
// need to check that the actual source is in fact the same that was previous set by the
|
||||
// managed wrapper. If it is, we get the input and return that (ie. the underlying image).
|
||||
// If not, we just return it directly and reset the locally stored DPI compensation effect.
|
||||
if (d2D1Image.Get()->IsSameInstance(sourceReference.GetDpiCompensationEffect()))
|
||||
{
|
||||
sourceReference.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf());
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceReference.SetDpiCompensationEffect(null);
|
||||
}
|
||||
}
|
||||
|
||||
// Get or create a wrapper for the input image
|
||||
return sourceReference.GetOrCreateWrapper(this.canvasDevice.Get(), d2D1Image.Get());
|
||||
}
|
||||
|
||||
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
|
||||
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
|
||||
/// <param name="flags">The <see cref="WIN2D_GET_D2D_IMAGE_FLAGS"/> flags for <paramref name="value"/>.</param>
|
||||
/// <param name="targetDpi">The target DPI for the realized image, if present.</param>
|
||||
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> object to use, if present.</param>
|
||||
/// <returns>Whether the input was set successfully or not.</returns>
|
||||
/// <remarks>This method assumes a lock is taken and the effect is already realized.</remarks>
|
||||
/// <inheritdoc cref="SetSource"/>
|
||||
private bool SetD2DInput(
|
||||
int index,
|
||||
IGraphicsEffectSource? value,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
ID2D1DeviceContext* d2D1DeviceContext)
|
||||
{
|
||||
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))
|
||||
{
|
||||
// Only potentially handle E_NOINTERFACE, always throw in other cases
|
||||
if (hresult != E.E_NOINTERFACE)
|
||||
{
|
||||
Marshal.ThrowExceptionForHR(hresult);
|
||||
}
|
||||
|
||||
// If unrealization isn't requested for failures, just throw
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is not valid (it must implement ICanvasImageInterop).");
|
||||
}
|
||||
|
||||
Unrealize(value, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
using ComPtr<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 && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get()))
|
||||
{
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentException(nameof(value), "The effect source is realized on a different canvas device.");
|
||||
}
|
||||
|
||||
Unrealize(value, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to realize the image from the input source (this will recurse through the effect graph)
|
||||
hresult = canvasImageInterop.Get()->GetD2DImage(
|
||||
device: this.canvasDevice.Get(),
|
||||
deviceContext: d2D1DeviceContext,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
realizeDpi: &realizedDpi,
|
||||
ppImage: d2D1Image.GetAddressOf());
|
||||
|
||||
// Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
hresult.Assert();
|
||||
}
|
||||
|
||||
// If no realized image is available, unrealize the effect
|
||||
if (d2D1Image.Get() is null)
|
||||
{
|
||||
Unrealize(value, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
// If the source is null and this is not explicitly allowed, the invocation is not valid
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(value), "The effect source cannot be null.");
|
||||
}
|
||||
|
||||
// Save the managed wrapper and realized image
|
||||
Sources.Storage[index].SetWrapperAndResource(value, d2D1Image.Get());
|
||||
|
||||
// Handle DPI compensation (the return is ignored here as whether DPI compensation was applied doesn't matter)
|
||||
_ = ApplyDpiCompensation(
|
||||
index: index,
|
||||
d2D1Image: ref *&d2D1Image,
|
||||
inputDpi: realizedDpi,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
d2D1DeviceContext: d2D1DeviceContext);
|
||||
|
||||
// Set the actual effect input
|
||||
this.d2D1Effect.Get()->SetInput(index: (uint)index, input: d2D1Image.Get());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a DPI compensation effect onto a realized image, if needed.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the source being processed.</param>
|
||||
/// <param name="d2D1Image">A reference to the realized source (this might be replaced if DPI compensation is added).</param>
|
||||
/// <param name="inputDpi">The input DPU for <paramref name="d2D1Image"/>.</param>
|
||||
/// <param name="flags">The <see cref="WIN2D_GET_D2D_IMAGE_FLAGS"/> flags for <paramref name="d2D1Image"/>.</param>
|
||||
/// <param name="targetDpi">The target DPI for the realized image, if present.</param>
|
||||
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> object to use, if present.</param>
|
||||
/// <returns>Whether the DPI configuration has changed.</returns>
|
||||
private bool ApplyDpiCompensation(
|
||||
int index,
|
||||
ref ComPtr<ID2D1Image> d2D1Image,
|
||||
float inputDpi,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
ID2D1DeviceContext* d2D1DeviceContext)
|
||||
{
|
||||
ref SourceReference sourceReference = ref Sources.Storage[index];
|
||||
|
||||
bool isDpiCompensationNeeded;
|
||||
|
||||
// Detemine whether DPI compensation is needed (direct port of Win2D's CanvasEffect::ApplyDpiCompensation)
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
// In minimal mode, we don't yet know the target DPI. For instance this occurs when setting an
|
||||
// effect as the source of an image brush. We'll fix up later when the brush is drawn to a device
|
||||
// context, at which point the real DPI is known. Since we don't yet know whether to include
|
||||
// compensation, we just preserve the existing state. This avoids repeated recreation during
|
||||
// set-draw-set-draw sequences.
|
||||
isDpiCompensationNeeded = sourceReference.HasDpiCompensationEffect && (inputDpi != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool neverCompensate = (flags & WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE;
|
||||
bool alwaysCompensate = (flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE;
|
||||
|
||||
// We need DPI compensation if:
|
||||
// - The input has a fixed DPI
|
||||
// - We were not told never to include it
|
||||
// - Either we were told to always include it, or the input DPI is different from the target
|
||||
isDpiCompensationNeeded = (inputDpi != 0) && !neverCompensate && (alwaysCompensate || (inputDpi != targetDpi));
|
||||
|
||||
// Early out if we are already in the right state
|
||||
if (isDpiCompensationNeeded == sourceReference.HasDpiCompensationEffect)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the determined DPI compensation configuration
|
||||
if (isDpiCompensationNeeded)
|
||||
{
|
||||
using ComPtr<ID2D1Effect> d2D1EffectDpiCompensation = default;
|
||||
|
||||
// Create an ID2D1Effect for the DPI compensation, if there isn't one already
|
||||
if (!sourceReference.HasDpiCompensationEffect)
|
||||
{
|
||||
using ComPtr<ID2D1DeviceContext> d2D1DeviceContextEffective = default;
|
||||
|
||||
// We need to create a DPI compensation effect, so a device context must be available.
|
||||
// If there is no input device context, just create a new one from the realization device.
|
||||
if (d2D1DeviceContext is null)
|
||||
{
|
||||
this.canvasDevice.Get()->CreateD2DDeviceContext(d2D1DeviceContextEffective.GetAddressOf()).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just use the input device context
|
||||
*&d2D1DeviceContextEffective = new ComPtr<ID2D1DeviceContext>(d2D1DeviceContext);
|
||||
}
|
||||
|
||||
// Create the DPI compensation effect
|
||||
d2D1DeviceContextEffective.Get()->CreateEffect(
|
||||
effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1DpiCompensation)),
|
||||
effect: d2D1EffectDpiCompensation.GetAddressOf()).Assert();
|
||||
|
||||
D2D1_BORDER_MODE d2D1BorderMode = D2D1_BORDER_MODE_HARD;
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE d2D1DpiCompensationInterpolationMode = D2D1_DPICOMPENSATION_INTERPOLATION_MODE_LINEAR;
|
||||
|
||||
// Set the border mode to hard
|
||||
d2D1EffectDpiCompensation.Get()->SetValue(
|
||||
index: (uint)D2D1_DPICOMPENSATION_PROP_BORDER_MODE,
|
||||
type: D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2D1BorderMode,
|
||||
dataSize: sizeof(D2D1_BORDER_MODE)).Assert();
|
||||
|
||||
// Set the interpolation mode to linear
|
||||
d2D1EffectDpiCompensation.Get()->SetValue(
|
||||
index: (uint)D2D1_DPICOMPENSATION_PROP_INTERPOLATION_MODE,
|
||||
type: D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2D1DpiCompensationInterpolationMode,
|
||||
dataSize: sizeof(D2D1_DPICOMPENSATION_INTERPOLATION_MODE)).Assert();
|
||||
|
||||
// Store the DPI effect for later use
|
||||
sourceReference.SetDpiCompensationEffect(d2D1EffectDpiCompensation.Get());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Just reuse the existing DPI effect otherwise
|
||||
d2D1EffectDpiCompensation.Attach(new ComPtr<ID2D1Effect>(sourceReference.GetDpiCompensationEffect()).Get());
|
||||
}
|
||||
|
||||
// Set the input image as source for the DPI compensation effect
|
||||
sourceReference.GetDpiCompensationEffect()->SetInput(index: (uint)index, input: d2D1Image.Get());
|
||||
|
||||
D2D_VECTOR_2F d2DVectorDpi;
|
||||
d2DVectorDpi.x = inputDpi;
|
||||
d2DVectorDpi.y = inputDpi;
|
||||
|
||||
// Set the DPI setting
|
||||
sourceReference.GetDpiCompensationEffect()->SetValue(
|
||||
index: (uint)D2D1_DPICOMPENSATION_PROP_INPUT_DPI,
|
||||
type: D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2DVectorDpi,
|
||||
dataSize: (uint)sizeof(D2D_VECTOR_2F)).Assert();
|
||||
|
||||
// Substitute the input image with the DPI compensation effect
|
||||
d2D1EffectDpiCompensation.CopyTo(ref d2D1Image).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ensure the existing effect is released, if present
|
||||
sourceReference.SetDpiCompensationEffect(null);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Uwp.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using TerraFX.Interop.DirectX;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="CacheOutput"/>.</returns>
|
||||
private T GetConstantBuffer()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetConstantBuffer<T>(),
|
||||
_ => this.constantBuffer
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="ConstantBuffer"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="ConstantBuffer"/>.</param>
|
||||
private void SetConstantBuffer(in T value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
// If the effect is realized, set the property on the underlying ID2D1Effect object
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.d2D1Effect.Get(), in value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just store the value locally
|
||||
this.constantBuffer = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="TransformMapper"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="TransformMapper"/>.</returns>
|
||||
private D2D1TransformMapper<T>? GetTransformMapper()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetTransformMapper<T>(),
|
||||
_ => this.transformMapper
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="TransformMapper"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="TransformMapper"/>.</param>
|
||||
private void SetTransformMapper(D2D1TransformMapper<T> value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(this.d2D1Effect.Get(), value);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.transformMapper = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="ResourceTextureManagers"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
|
||||
/// <returns>The marshalled value for <see cref="ResourceTextureManagers"/>.</returns>
|
||||
private D2D1ResourceTextureManager? GetResourceTextureManager(int index)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
D2D1ResourceTextureManager? resourceTextureManager = ResourceTextureManagers.Storage[index];
|
||||
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetResourceTextureManager(resourceTextureManager, index),
|
||||
_ => resourceTextureManager
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="ResourceTextureManagers"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="ResourceTextureManagers"/>.</param>
|
||||
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
|
||||
private void SetResourceTextureManager(D2D1ResourceTextureManager value, int index)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), value, index);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResourceTextureManagers.Storage[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="CacheOutput"/>.</returns>
|
||||
private bool GetCacheOutput()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetCachedProperty(),
|
||||
_ => this.cacheOutput
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="CacheOutput"/>.</param>
|
||||
private void SetCacheOutput(bool value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetCachedProperty(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.cacheOutput = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="BufferPrecision"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="BufferPrecision"/>.</returns>
|
||||
private CanvasBufferPrecision? GetBufferPrecision()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
D2D1_BUFFER_PRECISION d2D1BufferPrecision = this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetPrecisionProperty(),
|
||||
_ => this.d2D1BufferPrecision
|
||||
};
|
||||
|
||||
// Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN
|
||||
return this.d2D1BufferPrecision switch
|
||||
{
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM => CanvasBufferPrecision.Precision8UIntNormalized,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB => CanvasBufferPrecision.Precision8UIntNormalizedSrgb,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM => CanvasBufferPrecision.Precision16UIntNormalized,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT => CanvasBufferPrecision.Precision16Float,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT => CanvasBufferPrecision.Precision32Float,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="BufferPrecision"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="BufferPrecision"/>.</param>
|
||||
private void SetBufferPrecision(CanvasBufferPrecision? value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
// Map back from CanvasBufferPrecision? to D2D1_BUFFER_PRECISION (null results in D2D1_BUFFER_PRECISION_UNKNOWN)
|
||||
this.d2D1BufferPrecision = value switch
|
||||
{
|
||||
CanvasBufferPrecision.Precision8UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM,
|
||||
CanvasBufferPrecision.Precision8UIntNormalizedSrgb => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB,
|
||||
CanvasBufferPrecision.Precision16UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM,
|
||||
CanvasBufferPrecision.Precision16Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT,
|
||||
CanvasBufferPrecision.Precision32Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT,
|
||||
_ => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN
|
||||
};
|
||||
|
||||
// If the effect is realized, set the property on the underlying ID2D1Effect object too
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+268
@@ -0,0 +1,268 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Shaders.Loaders;
|
||||
using ComputeSharp.D2D1.Uwp.Buffers;
|
||||
using ComputeSharp.D2D1.Uwp.Collections;
|
||||
|
||||
#pragma warning disable CS0618, CS8767
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the collection of <see cref="D2D1ResourceTextureManager"/> objects in a <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
public sealed class ResourceTextureManagerCollection : IList<D2D1ResourceTextureManager?>, IReadOnlyList<D2D1ResourceTextureManager?>, IList, IFixedCountList<D2D1ResourceTextureManager?>
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask of valid indices for resource texture managers in the current shader type.
|
||||
/// </summary>
|
||||
private static readonly int IndexBitmask = GetIndexBitmask();
|
||||
|
||||
/// <summary>
|
||||
/// The fixed buffer of <see cref="D2D1ResourceTextureManager"/> instances.
|
||||
/// </summary>
|
||||
private ResourceTextureManagerBuffer fixedBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ResourceTextureManagerCollection"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="owner">The owning <see cref="PixelShaderEffect{T}"/> instance.</param>
|
||||
internal ResourceTextureManagerCollection(PixelShaderEffect<T> owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count => Indices.Length;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="D2D1ResourceTextureManager"/> object at a specified index.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
|
||||
/// <returns>The <see cref="D2D1ResourceTextureManager"/> object at the specified index.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if the value is set to <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
|
||||
[DisallowNull]
|
||||
public D2D1ResourceTextureManager? this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
ValidateIndex(index);
|
||||
|
||||
return Owner.GetResourceTextureManager(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(value), "Input resource texture managers cannot be null.");
|
||||
}
|
||||
|
||||
ValidateIndex(index);
|
||||
|
||||
Owner.SetResourceTextureManager(value, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
object? IList.this[int index]
|
||||
{
|
||||
get => this[index];
|
||||
set => this[index] = (D2D1ResourceTextureManager?)value!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid resource texture indices for the current effect.
|
||||
/// </summary>
|
||||
internal static ImmutableArray<int> Indices { get; } = GetIndices();
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
internal PixelShaderEffect<T> Owner { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="ResourceTextureManagerBuffer"/> value containing the available <see cref="D2D1ResourceTextureManager"/>-s.
|
||||
/// </summary>
|
||||
internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
ImmutableArray<int> IFixedCountList<D2D1ResourceTextureManager?>.Indices => Indices;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<D2D1ResourceTextureManager?>.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsFixedSize => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection.IsSynchronized => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for ResourceTextureManagerCollection.");
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<D2D1ResourceTextureManager?>.Add(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
throw new NotSupportedException("ICollection<T>.Add is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.Add(object value)
|
||||
{
|
||||
throw new NotSupportedException("IList.Add is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<D2D1ResourceTextureManager?>.Clear()
|
||||
{
|
||||
throw new NotSupportedException("ICollection<T>.Clear is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Clear()
|
||||
{
|
||||
throw new NotSupportedException("IList.Clear is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<D2D1ResourceTextureManager?>.Contains(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, item) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.Contains(object value)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, (D2D1ResourceTextureManager?)value) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<D2D1ResourceTextureManager?>.CopyTo(D2D1ResourceTextureManager?[] array, int arrayIndex)
|
||||
{
|
||||
FixedCountList<D2D1ResourceTextureManager?>.CopyTo(this, array, arrayIndex);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
{
|
||||
FixedCountList<D2D1ResourceTextureManager?>.CopyTo(this, array, index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator<D2D1ResourceTextureManager?> IEnumerable<D2D1ResourceTextureManager?>.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList<D2D1ResourceTextureManager?>.IndexOf(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, item);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.IndexOf(object value)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, (D2D1ResourceTextureManager?)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<D2D1ResourceTextureManager?>.Insert(int index, D2D1ResourceTextureManager? item)
|
||||
{
|
||||
throw new NotSupportedException("IList<T>.Insert is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Insert(int index, object value)
|
||||
{
|
||||
throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<D2D1ResourceTextureManager?>.Remove(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
throw new NotSupportedException("ICollection<T>.Remove is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Remove(object value)
|
||||
{
|
||||
throw new NotSupportedException("IList.Remove is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<D2D1ResourceTextureManager?>.RemoveAt(int index)
|
||||
{
|
||||
throw new NotSupportedException("IList<T>.RemoveAt is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.RemoveAt(int index)
|
||||
{
|
||||
throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the input index for <see cref="this[int]"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index to validate.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is out of range.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ValidateIndex(int index)
|
||||
{
|
||||
if ((uint)index >= 16 || (IndexBitmask & (1 << index)) == 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid resource texture manager index for the current effect.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
/// <returns>The bitmask of indices for resource textures in the target shader.</returns>
|
||||
private static int GetIndexBitmask()
|
||||
{
|
||||
D2D1IndexBitmaskResourceTextureDescriptionsLoader bitmaskLoader = default;
|
||||
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.LoadResourceTextureDescriptions(ref bitmaskLoader);
|
||||
|
||||
return bitmaskLoader.GetResultingIndexBitmask();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>The collection of valid indices for the current effect.</returns>
|
||||
private static ImmutableArray<int> GetIndices()
|
||||
{
|
||||
D2D1ImmutableArrayResourceTextureDescriptionsLoader indicesLoader = default;
|
||||
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.LoadResourceTextureDescriptions(ref indicesLoader);
|
||||
|
||||
return indicesLoader.GetResultingIndices();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Uwp.Buffers;
|
||||
using ComputeSharp.D2D1.Uwp.Collections;
|
||||
using Windows.Graphics.Effects;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the collection of <see cref="IGraphicsEffectSource"/> sources in a <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
public sealed class SourceCollection : IList<IGraphicsEffectSource?>, IReadOnlyList<IGraphicsEffectSource?>, IList, IFixedCountList<IGraphicsEffectSource?>
|
||||
{
|
||||
/// <summary>
|
||||
/// The collection of valid resource texture indices for the current effect.
|
||||
/// </summary>
|
||||
private static readonly ImmutableArray<int> Indices = GetIndices();
|
||||
|
||||
/// <summary>
|
||||
/// The fixed buffer of <see cref="IGraphicsEffectSource"/> instances.
|
||||
/// </summary>
|
||||
private GraphicsEffectSourceBuffer fixedBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="SourceCollection"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="owner">The owning <see cref="PixelShaderEffect{T}"/> instance.</param>
|
||||
internal SourceCollection(PixelShaderEffect<T> owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => D2D1PixelShader.GetInputCount<T>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IGraphicsEffectSource? this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
ValidateIndex(index);
|
||||
|
||||
return Owner.GetSource(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
ValidateIndex(index);
|
||||
|
||||
Owner.SetSource(value, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
object? IList.this[int index]
|
||||
{
|
||||
get => this[index];
|
||||
set => this[index] = (IGraphicsEffectSource?)value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
internal PixelShaderEffect<T> Owner { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="GraphicsEffectSourceBuffer"/> value containing the available <see cref="IGraphicsEffectSource"/>-s.
|
||||
/// </summary>
|
||||
internal ref GraphicsEffectSourceBuffer Storage => ref this.fixedBuffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
ImmutableArray<int> IFixedCountList<IGraphicsEffectSource?>.Indices => Indices;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<IGraphicsEffectSource?>.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsFixedSize => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection.IsSynchronized => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for SourceCollection.");
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<IGraphicsEffectSource?>.Add(IGraphicsEffectSource? item)
|
||||
{
|
||||
throw new NotSupportedException("ICollection<T>.Add is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.Add(object value)
|
||||
{
|
||||
throw new NotSupportedException("IList.Add is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<IGraphicsEffectSource?>.Clear()
|
||||
{
|
||||
throw new NotSupportedException("ICollection<T>.Clear is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Clear()
|
||||
{
|
||||
throw new NotSupportedException("IList.Clear is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<IGraphicsEffectSource?>.Contains(IGraphicsEffectSource? item)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, item) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.Contains(object value)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, (IGraphicsEffectSource?)value) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<IGraphicsEffectSource?>.CopyTo(IGraphicsEffectSource?[] array, int arrayIndex)
|
||||
{
|
||||
FixedCountList<IGraphicsEffectSource?>.CopyTo(this, array, arrayIndex);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
{
|
||||
FixedCountList<IGraphicsEffectSource?>.CopyTo(this, array, index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator<IGraphicsEffectSource?> IEnumerable<IGraphicsEffectSource?>.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList<IGraphicsEffectSource?>.IndexOf(IGraphicsEffectSource? item)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, item);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.IndexOf(object value)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, (IGraphicsEffectSource?)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<IGraphicsEffectSource?>.Insert(int index, IGraphicsEffectSource? item)
|
||||
{
|
||||
throw new NotSupportedException("IList<T>.Insert is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Insert(int index, object value)
|
||||
{
|
||||
throw new NotSupportedException("IList.Insert is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<IGraphicsEffectSource?>.Remove(IGraphicsEffectSource? item)
|
||||
{
|
||||
throw new NotSupportedException("ICollection<T>.Remove is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Remove(object value)
|
||||
{
|
||||
throw new NotSupportedException("IList.Remove is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<IGraphicsEffectSource?>.RemoveAt(int index)
|
||||
{
|
||||
throw new NotSupportedException("IList<T>.RemoveAt is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.RemoveAt(int index)
|
||||
{
|
||||
throw new NotSupportedException("IList.RemoveAt is not supported for SourceCollection.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the input index for <see cref="this[int]"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index to validate.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is out of range.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ValidateIndex(int index)
|
||||
{
|
||||
if ((uint)index >= D2D1PixelShader.GetInputCount<T>())
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid source index for the current effect.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>The collection of valid indices for the current effect.</returns>
|
||||
private static unsafe ImmutableArray<int> GetIndices()
|
||||
{
|
||||
int inputCount = D2D1PixelShader.GetInputCount<T>();
|
||||
|
||||
if (inputCount == 0)
|
||||
{
|
||||
return ImmutableArray<int>.Empty;
|
||||
}
|
||||
|
||||
int[] indices = new int[inputCount];
|
||||
|
||||
for (int i = 0; i < indices.Length; i++)
|
||||
{
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
return *(ImmutableArray<int>*)&indices;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the current <typeparamref name="T"/> value in use.
|
||||
/// </summary>
|
||||
public T ConstantBuffer
|
||||
{
|
||||
get => GetConstantBuffer();
|
||||
set => SetConstantBuffer(in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="D2D1TransformMapper{T}"/> instance to use for the effect, if any.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown if the value is set to <see langword="null"/>.</exception>
|
||||
[DisallowNull]
|
||||
public D2D1TransformMapper<T>? TransformMapper
|
||||
{
|
||||
get => GetTransformMapper();
|
||||
set => SetTransformMapper(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool CacheOutput
|
||||
{
|
||||
get => GetCacheOutput();
|
||||
set => SetCacheOutput(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public CanvasBufferPrecision? BufferPrecision
|
||||
{
|
||||
get => GetBufferPrecision();
|
||||
set => SetBufferPrecision(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string? Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="SourceCollection"/> object associated with the current instance.
|
||||
/// </summary>
|
||||
public SourceCollection Sources { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ResourceTextureManagerCollection"/> object associated with the current instance.
|
||||
/// </summary>
|
||||
public ResourceTextureManagerCollection ResourceTextureManagers { get; }
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using ComputeSharp.Interop;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources and performs other cleanup operations.
|
||||
/// </summary>
|
||||
~PixelShaderEffect()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IReferenceTrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker IReferenceTrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IReferenceTrackedObject.DangerousOnDispose()
|
||||
{
|
||||
this.canvasDevice.Dispose();
|
||||
this.d2D1RealizationDevice.Dispose();
|
||||
this.d2D1Effect.Dispose();
|
||||
|
||||
// Also release all native resources for the used sources.
|
||||
// The transform mapper and resource texture managers don't
|
||||
// need to be disposed, as those could be shared between effects.
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
Sources.Storage[i].Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.Interop;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.Effects;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <summary>
|
||||
/// A custom <see cref="ICanvasEffect"/> implementation powered by a supplied shader type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader to use to render frames.</typeparam>
|
||||
public sealed partial class PixelShaderEffect<T> : IReferenceTrackedObject, ICanvasEffect, ICanvasImageInterop.Interface
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ReferenceTracker"/> value for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// Lock object used to synchronize calls to <see cref="ICanvasImageInterop"/> APIs.
|
||||
/// </summary>
|
||||
private readonly object lockObject = new();
|
||||
|
||||
/// <summary>
|
||||
/// Flag to track whether a given call is recursively invoked by <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>, to avoid graph cycles.
|
||||
/// </summary>
|
||||
private volatile int isInsideGetD2DImage;
|
||||
|
||||
/// <summary>
|
||||
/// The current device the effect is realized on, if one is available (this is the underlying COM object for <see cref="CanvasDevice"/>).
|
||||
/// </summary>
|
||||
private ComPtr<ICanvasDevice> canvasDevice;
|
||||
|
||||
/// <summary>
|
||||
/// The underlying D2D1 device the effect is realized on, if one is available (this is the D2D device backing <see cref="canvasDevice"/>).
|
||||
/// </summary>
|
||||
private ComPtr<ID2D1Device1> d2D1RealizationDevice;
|
||||
|
||||
/// <summary>
|
||||
/// The current realized <see cref="ID2D1Effect"/>, if one is available.
|
||||
/// </summary>
|
||||
private ComPtr<ID2D1Effect> d2D1Effect;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="ConstantBuffer"/>.
|
||||
/// </summary>
|
||||
private T constantBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="TransformMapper"/>.
|
||||
/// </summary>
|
||||
private D2D1TransformMapper<T>? transformMapper;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
private bool cacheOutput;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="BufferPrecision"/>.
|
||||
/// </summary>
|
||||
private D2D1_BUFFER_PRECISION d2D1BufferPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
public PixelShaderEffect()
|
||||
{
|
||||
this.referenceTracker = new ReferenceTracker(this);
|
||||
|
||||
Sources = new SourceCollection(this);
|
||||
ResourceTextureManagers = new ResourceTextureManagerCollection(this);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,10 @@
|
||||
<SupportedOSVersion>windows6.1</SupportedOSVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ComputeSharp.D2D1.Uwp" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ComputeSharp.Core\ComputeSharp.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.Interop;
|
||||
|
||||
/// <summary>
|
||||
@@ -7,6 +9,7 @@ namespace ComputeSharp.D2D1.Interop;
|
||||
/// <para>This type exposes the values that can be set via <c>ID2D1RenderInfo::SetInputDescription</c>.</para>
|
||||
/// <para>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1effectauthor/nf-d2d1effectauthor-id2d1renderinfo-setinputdescription"/>.</para>
|
||||
/// </remarks>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct D2D1InputDescription
|
||||
{
|
||||
/// <summary>
|
||||
@@ -30,19 +33,6 @@ public readonly struct D2D1InputDescription
|
||||
/// </summary>
|
||||
private readonly int levelOfDetailCount;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1InputDescription"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the resource the description belongs to.</param>
|
||||
/// <param name="filter">The type of filter to apply to the input texture.</param>
|
||||
/// <param name="levelOfDetailCount">The mip level to retrieve from the upstream transform, if specified.</param>
|
||||
internal D2D1InputDescription(int index, D2D1Filter filter, int levelOfDetailCount)
|
||||
{
|
||||
this.index = index;
|
||||
this.filter = filter;
|
||||
this.levelOfDetailCount = levelOfDetailCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the resource the description belongs to.
|
||||
/// </summary>
|
||||
|
||||
@@ -426,11 +426,11 @@ public static class D2D1PixelShader
|
||||
/// Sets the constant buffer from an input D2D1 pixel shader, by calling <c>ID2D1DrawInfo::SetPixelShaderConstantBuffer</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to set the constant buffer for.</typeparam>
|
||||
/// <param name="shader">The input D2D1 pixel shader to set the contant buffer for.</param>
|
||||
/// <param name="d2D1DrawInfo">A pointer to the <c>ID2D1DrawInfo</c> instance to use.</param>
|
||||
/// <param name="shader">The input D2D1 pixel shader to set the contant buffer for.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="d2D1DrawInfo"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1effectauthor/nf-d2d1effectauthor-id2d1drawinfo-setpixelshaderconstantbuffer"/>.</remarks>
|
||||
public static unsafe void SetConstantBufferForD2D1DrawInfo<T>(in T shader, void* d2D1DrawInfo)
|
||||
public static unsafe void SetConstantBufferForD2D1DrawInfo<T>(void* d2D1DrawInfo, in T shader)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
if (d2D1DrawInfo is null)
|
||||
|
||||
@@ -497,11 +497,11 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
/// Sets the constant buffer from an input D2D1 effect, by calling <c>ID2D1Effect::SetValue</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to set the constant buffer for.</typeparam>
|
||||
/// <param name="shader">The input D2D1 pixel shader to set the contant buffer for.</param>
|
||||
/// <param name="d2D1Effect">A pointer to the <c>ID2D1Effect</c> instance to use.</param>
|
||||
/// <param name="shader">The input D2D1 pixel shader to set the contant buffer for.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="d2D1Effect"/> is <see langword="null"/>.</exception>
|
||||
/// <remarks>For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1properties-setvalue(uint32_d2d1_property_type_constbyte_uint32)"/>.</remarks>
|
||||
public static void SetConstantBufferForD2D1Effect<T>(in T shader, void* d2D1Effect)
|
||||
public static void SetConstantBufferForD2D1Effect<T>(void* d2D1Effect, in T shader)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
if (d2D1Effect is null)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// A type containing info on a description for a given D2D1 pixel shader resource resource.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct D2D1ResourceTextureDescription
|
||||
{
|
||||
/// <summary>
|
||||
@@ -16,17 +19,6 @@ public readonly struct D2D1ResourceTextureDescription
|
||||
/// </summary>
|
||||
private readonly int dimensions;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1ResourceTextureDescription"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the resource texture the description belongs to.</param>
|
||||
/// <param name="dimensions">The number of dimensions of the resource texture the description belongs to.</param>
|
||||
internal D2D1ResourceTextureDescription(int index, int dimensions)
|
||||
{
|
||||
this.index = index;
|
||||
this.dimensions = dimensions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the index of the resource texture the description belongs to.
|
||||
/// </summary>
|
||||
|
||||
+165
-102
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
#if NET6_0_OR_GREATER
|
||||
using System.Runtime.InteropServices;
|
||||
#endif
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Interop.Effects;
|
||||
using TerraFX.Interop.Windows;
|
||||
@@ -16,128 +14,193 @@ namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
|
||||
/// <inheritdoc/>
|
||||
unsafe partial struct D2D1TransformMapperImpl
|
||||
{
|
||||
/// <inheritdoc cref="QueryInterface(Guid*, void**)"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static int QueryInterface(D2D1TransformMapperImpl* @this, Guid* riid, void** ppvObject)
|
||||
/// <summary>
|
||||
/// The implementation for <see cref="ID2D1TransformMapper"/>.
|
||||
/// </summary>
|
||||
private static class ID2D1TransformMapperMethods
|
||||
{
|
||||
return @this->QueryInterface(riid, ppvObject);
|
||||
}
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInputRectsToOutputRect"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
public delegate int MapInputRectsToOutputRectDelegate(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext,
|
||||
RECT* inputRects,
|
||||
RECT* inputOpaqueSubRects,
|
||||
uint inputRectCount,
|
||||
RECT* outputRect,
|
||||
RECT* outputOpaqueSubRect);
|
||||
|
||||
/// <inheritdoc cref="AddRef()"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint AddRef(D2D1TransformMapperImpl* @this)
|
||||
{
|
||||
return @this->AddRef();
|
||||
}
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapOutputRectToInputRects"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
public delegate int MapOutputRectToInputRectsDelegate(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
RECT* outputRect,
|
||||
RECT* inputRects,
|
||||
uint inputRectsCount);
|
||||
|
||||
/// <inheritdoc cref="Release()"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static uint Release(D2D1TransformMapperImpl* @this)
|
||||
{
|
||||
return @this->Release();
|
||||
}
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInvalidRect"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
public delegate int MapInvalidRectDelegate(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
uint inputIndex,
|
||||
RECT invalidInputRect,
|
||||
RECT* invalidOutputRect);
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInputRectsToOutputRect"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static int MapInputRectsToOutputRect(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext,
|
||||
RECT* inputRects,
|
||||
RECT* inputOpaqueSubRects,
|
||||
uint inputRectCount,
|
||||
RECT* outputRect,
|
||||
RECT* outputOpaqueSubRect)
|
||||
{
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount);
|
||||
Span<Rectangle> opaqueInputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount);
|
||||
/// <summary>
|
||||
/// A cached <see cref="QueryInterfaceDelegate"/> instance wrapping <see cref="QueryInterface"/>.
|
||||
/// </summary>
|
||||
public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
|
||||
|
||||
for (int i = 0; i < (int)inputRectCount; i++)
|
||||
/// <summary>
|
||||
/// A cached <see cref="AddRefDelegate"/> instance wrapping <see cref="AddRef"/>.
|
||||
/// </summary>
|
||||
public static readonly AddRefDelegate AddRefWrapper = AddRef;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release"/>.
|
||||
/// </summary>
|
||||
public static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="MapInputRectsToOutputRectDelegate"/> instance wrapping <see cref="MapInputRectsToOutputRect"/>.
|
||||
/// </summary>
|
||||
public static readonly MapInputRectsToOutputRectDelegate MapInputRectsToOutputRectWrapper = MapInputRectsToOutputRect;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="MapOutputRectToInputRectsDelegate"/> instance wrapping <see cref="MapOutputRectToInputRects"/>.
|
||||
/// </summary>
|
||||
public static readonly MapOutputRectToInputRectsDelegate MapOutputRectToInputRectsWrapper = MapOutputRectToInputRects;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="MapInvalidRectDelegate"/> instance wrapping <see cref="MapInvalidRect"/>.
|
||||
/// </summary>
|
||||
public static readonly MapInvalidRectDelegate MapInvalidRectWrapper = MapInvalidRect;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="D2D1TransformMapperImpl.QueryInterface"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int QueryInterface(D2D1TransformMapperImpl* @this, Guid* riid, void** ppvObject)
|
||||
{
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle();
|
||||
return @this->QueryInterface(riid, ppvObject);
|
||||
}
|
||||
|
||||
Rectangle output;
|
||||
Rectangle opaqueOutput;
|
||||
|
||||
// Invoke MapInputsToOutput and handle exceptions so they don't cross the ABI boundary
|
||||
try
|
||||
/// <inheritdoc cref="D2D1TransformMapperImpl.AddRef"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static uint AddRef(D2D1TransformMapperImpl* @this)
|
||||
{
|
||||
Unsafe.As<ID2D1TransformMapperInterop>(@this->transformMapperHandle.Target!).MapInputsToOutput(
|
||||
d2D1DrawInfoUpdateContext,
|
||||
inputs,
|
||||
opaqueInputs,
|
||||
out output,
|
||||
out opaqueOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
return @this->AddRef();
|
||||
}
|
||||
|
||||
*outputRect = output.ToRECT();
|
||||
*outputOpaqueSubRect = opaqueOutput.ToRECT();
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapOutputRectToInputRects"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static int MapOutputRectToInputRects(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
RECT* outputRect,
|
||||
RECT* inputRects,
|
||||
uint inputRectsCount)
|
||||
{
|
||||
Rectangle output = outputRect->ToRectangle();
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectsCount);
|
||||
|
||||
for (int i = 0; i < (int)inputRectsCount; i++)
|
||||
/// <inheritdoc cref="D2D1TransformMapperImpl.Release"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static uint Release(D2D1TransformMapperImpl* @this)
|
||||
{
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
return @this->Release();
|
||||
}
|
||||
|
||||
// Handle exceptions, as mentioned above
|
||||
try
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInputRectsToOutputRect"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int MapInputRectsToOutputRect(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext,
|
||||
RECT* inputRects,
|
||||
RECT* inputOpaqueSubRects,
|
||||
uint inputRectCount,
|
||||
RECT* outputRect,
|
||||
RECT* outputOpaqueSubRect)
|
||||
{
|
||||
Unsafe.As<ID2D1TransformMapperInterop>(@this->transformMapperHandle.Target!).MapOutputToInputs(in output, inputs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount);
|
||||
Span<Rectangle> opaqueInputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount);
|
||||
|
||||
for (int i = 0; i < (int)inputRectCount; i++)
|
||||
{
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle();
|
||||
}
|
||||
|
||||
Rectangle output;
|
||||
Rectangle opaqueOutput;
|
||||
|
||||
// Invoke MapInputsToOutput and handle exceptions so they don't cross the ABI boundary
|
||||
try
|
||||
{
|
||||
Unsafe.As<ID2D1TransformMapperInterop>(@this->transformMapperHandle.Target!).MapInputsToOutput(
|
||||
d2D1DrawInfoUpdateContext,
|
||||
inputs,
|
||||
opaqueInputs,
|
||||
out output,
|
||||
out opaqueOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
|
||||
*outputRect = output.ToRECT();
|
||||
*outputOpaqueSubRect = opaqueOutput.ToRECT();
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)inputRectsCount; i++)
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapOutputRectToInputRects"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int MapOutputRectToInputRects(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
RECT* outputRect,
|
||||
RECT* inputRects,
|
||||
uint inputRectsCount)
|
||||
{
|
||||
inputRects[i] = inputs[i].ToRECT();
|
||||
Rectangle output = outputRect->ToRectangle();
|
||||
Span<Rectangle> inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectsCount);
|
||||
|
||||
for (int i = 0; i < (int)inputRectsCount; i++)
|
||||
{
|
||||
inputs[i] = inputRects[i].ToRectangle();
|
||||
}
|
||||
|
||||
// Handle exceptions, as mentioned above
|
||||
try
|
||||
{
|
||||
Unsafe.As<ID2D1TransformMapperInterop>(@this->transformMapperHandle.Target!).MapOutputToInputs(in output, inputs);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
|
||||
for (int i = 0; i < (int)inputRectsCount; i++)
|
||||
{
|
||||
inputRects[i] = inputs[i].ToRECT();
|
||||
}
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInvalidRect"/>
|
||||
[UnmanagedCallersOnly]
|
||||
private static int MapInvalidRect(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
uint inputIndex,
|
||||
RECT invalidInputRect,
|
||||
RECT* invalidOutputRect)
|
||||
{
|
||||
Rectangle invalidInput = invalidInputRect.ToRectangle();
|
||||
Rectangle invalidOutput;
|
||||
|
||||
// Handle exceptions once again
|
||||
try
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInvalidRect"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int MapInvalidRect(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
uint inputIndex,
|
||||
RECT invalidInputRect,
|
||||
RECT* invalidOutputRect)
|
||||
{
|
||||
Unsafe.As<ID2D1TransformMapperInterop>(@this->transformMapperHandle.Target!).MapInvalidOutput((int)inputIndex, invalidInput, out invalidOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
Rectangle invalidInput = invalidInputRect.ToRectangle();
|
||||
Rectangle invalidOutput;
|
||||
|
||||
*invalidOutputRect = invalidOutput.ToRECT();
|
||||
// Handle exceptions once again
|
||||
try
|
||||
{
|
||||
Unsafe.As<ID2D1TransformMapperInterop>(@this->transformMapperHandle.Target!).MapInvalidOutput((int)inputIndex, invalidInput, out invalidOutput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.HResult;
|
||||
}
|
||||
|
||||
return S.S_OK;
|
||||
*invalidOutputRect = invalidOutput.ToRECT();
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
#if !NET6_0_OR_GREATER
|
||||
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial struct D2D1TransformMapperImpl
|
||||
{
|
||||
/// <summary>
|
||||
/// The implementation for <see cref="ID2D1TransformMapperInternal"/>.
|
||||
/// </summary>
|
||||
private static class ID2D1TransformMapperInternalMethods
|
||||
{
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="ID2D1TransformMapperInternal.GetManagedWrapperHandle"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
public delegate int GetManagedWrapperHandleDelegate(D2D1TransformMapperImpl* @this, void** ppvHandle);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="QueryInterfaceDelegate"/> instance wrapping <see cref="QueryInterface"/>.
|
||||
/// </summary>
|
||||
public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="AddRefDelegate"/> instance wrapping <see cref="AddRef"/>.
|
||||
/// </summary>
|
||||
public static readonly AddRefDelegate AddRefWrapper = AddRef;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release"/>.
|
||||
/// </summary>
|
||||
public static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="GetManagedWrapperHandleDelegate"/> instance wrapping <see cref="GetManagedWrapperHandle"/>.
|
||||
/// </summary>
|
||||
public static readonly GetManagedWrapperHandleDelegate GetManagedWrapperHandleWrapper = GetManagedWrapperHandle;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="D2D1TransformMapperImpl.QueryInterface"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int QueryInterface(D2D1TransformMapperImpl* @this, Guid* riid, void** ppvObject)
|
||||
{
|
||||
@this = (D2D1TransformMapperImpl*)&((void**)@this)[-1];
|
||||
|
||||
return @this->QueryInterface(riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="D2D1TransformMapperImpl.AddRef"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static uint AddRef(D2D1TransformMapperImpl* @this)
|
||||
{
|
||||
@this = (D2D1TransformMapperImpl*)&((void**)@this)[-1];
|
||||
|
||||
return @this->AddRef();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="D2D1TransformMapperImpl.Release"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static uint Release(D2D1TransformMapperImpl* @this)
|
||||
{
|
||||
@this = (D2D1TransformMapperImpl*)&((void**)@this)[-1];
|
||||
|
||||
return @this->Release();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapperInternal.GetManagedWrapperHandle"/>
|
||||
[UnmanagedCallersOnly]
|
||||
public static int GetManagedWrapperHandle(D2D1TransformMapperImpl* @this, void** ppvHandle)
|
||||
{
|
||||
@this = (D2D1TransformMapperImpl*)&((void**)@this)[-1];
|
||||
|
||||
if (ppvHandle is null)
|
||||
{
|
||||
return E.E_POINTER;
|
||||
}
|
||||
|
||||
*ppvHandle = (void*)GCHandle.ToIntPtr(@this->transformMapperHandle);
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
+58
-79
@@ -30,101 +30,68 @@ internal unsafe partial struct D2D1TransformMapperImpl
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
private delegate uint ReleaseDelegate(D2D1TransformMapperImpl* @this);
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInputRectsToOutputRect"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
private delegate int MapInputRectsToOutputRectDelegate(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext,
|
||||
RECT* inputRects,
|
||||
RECT* inputOpaqueSubRects,
|
||||
uint inputRectCount,
|
||||
RECT* outputRect,
|
||||
RECT* outputOpaqueSubRect);
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapOutputRectToInputRects"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
private delegate int MapOutputRectToInputRectsDelegate(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
RECT* outputRect,
|
||||
RECT* inputRects,
|
||||
uint inputRectsCount);
|
||||
|
||||
/// <inheritdoc cref="ID2D1TransformMapper.MapInvalidRect"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
private delegate int MapInvalidRectDelegate(
|
||||
D2D1TransformMapperImpl* @this,
|
||||
uint inputIndex,
|
||||
RECT invalidInputRect,
|
||||
RECT* invalidOutputRect);
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="QueryInterfaceDelegate"/> instance wrapping <see cref="QueryInterface(Guid*, void**)"/>.
|
||||
/// </summary>
|
||||
private static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="AddRefDelegate"/> instance wrapping <see cref="AddRef()"/>.
|
||||
/// </summary>
|
||||
private static readonly AddRefDelegate AddRefWrapper = AddRef;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="ReleaseDelegate"/> instance wrapping <see cref="Release()"/>.
|
||||
/// </summary>
|
||||
private static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="MapInputRectsToOutputRectDelegate"/> instance wrapping <see cref="MapInputRectsToOutputRect"/>.
|
||||
/// </summary>
|
||||
private static readonly MapInputRectsToOutputRectDelegate MapInputRectsToOutputRectWrapper = MapInputRectsToOutputRect;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="MapOutputRectToInputRectsDelegate"/> instance wrapping <see cref="MapOutputRectToInputRects"/>.
|
||||
/// </summary>
|
||||
private static readonly MapOutputRectToInputRectsDelegate MapOutputRectToInputRectsWrapper = MapOutputRectToInputRects;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="MapInvalidRectDelegate"/> instance wrapping <see cref="MapInvalidRect"/>.
|
||||
/// </summary>
|
||||
private static readonly MapInvalidRectDelegate MapInvalidRectWrapper = MapInvalidRect;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// The shared method table pointer for all <see cref="ID2D1TransformMapper"/> instances.
|
||||
/// The shared vtable pointer for <see cref="D2D1TransformMapperImpl"/> instance, for <see cref="ID2D1TransformMapper"/>.
|
||||
/// </summary>
|
||||
private static readonly void** Vtbl = InitVtbl();
|
||||
private static readonly void** VtblForID2D1TransformMapper = InitVtblForID2D1TransformMapperAndID2D1TransformMapperInternal();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the custom method table pointer for <see cref="ID2D1TransformMapper"/>.
|
||||
/// The shared vtable pointer for <see cref="D2D1TransformMapperImpl"/> instance, for <see cref="ID2D1TransformMapperInternal"/>.
|
||||
/// </summary>
|
||||
/// <returns>The method table pointer for <see cref="ID2D1TransformMapper"/>.</returns>
|
||||
private static void** InitVtbl()
|
||||
{
|
||||
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1TransformMapperImpl), sizeof(void*) * 6);
|
||||
private static readonly void** VtblForID2D1TransformMapperInternal = &VtblForID2D1TransformMapper[6];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the combined vtable for <see cref="ID2D1TransformMapper"/> and <see cref="ID2D1TransformMapperInternal"/>.
|
||||
/// </summary>
|
||||
/// <returns>The combined vtable for <see cref="ID2D1TransformMapper"/> and <see cref="ID2D1TransformMapperInternal"/>.</returns>
|
||||
private static void** InitVtblForID2D1TransformMapperAndID2D1TransformMapperInternal()
|
||||
{
|
||||
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1TransformMapperImpl), sizeof(void*) * 10);
|
||||
|
||||
// ID2D1ResourceTextureManager
|
||||
#if NET6_0_OR_GREATER
|
||||
lpVtbl[0] = (delegate* unmanaged<D2D1TransformMapperImpl*, Guid*, void**, int>)&QueryInterface;
|
||||
lpVtbl[1] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint>)&AddRef;
|
||||
lpVtbl[2] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint>)&Release;
|
||||
lpVtbl[3] = (delegate* unmanaged<D2D1TransformMapperImpl*, ID2D1DrawInfoUpdateContext*, RECT*, RECT*, uint, RECT*, RECT*, int>)&MapInputRectsToOutputRect;
|
||||
lpVtbl[4] = (delegate* unmanaged<D2D1TransformMapperImpl*, RECT*, RECT*, uint, int>)&MapOutputRectToInputRects;
|
||||
lpVtbl[5] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint, RECT, RECT*, int>)&MapInvalidRect;
|
||||
lpVtbl[0] = (delegate* unmanaged<D2D1TransformMapperImpl*, Guid*, void**, int>)&ID2D1TransformMapperMethods.QueryInterface;
|
||||
lpVtbl[1] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint>)&ID2D1TransformMapperMethods.AddRef;
|
||||
lpVtbl[2] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint>)&ID2D1TransformMapperMethods.Release;
|
||||
lpVtbl[3] = (delegate* unmanaged<D2D1TransformMapperImpl*, ID2D1DrawInfoUpdateContext*, RECT*, RECT*, uint, RECT*, RECT*, int>)&ID2D1TransformMapperMethods.MapInputRectsToOutputRect;
|
||||
lpVtbl[4] = (delegate* unmanaged<D2D1TransformMapperImpl*, RECT*, RECT*, uint, int>)&ID2D1TransformMapperMethods.MapOutputRectToInputRects;
|
||||
lpVtbl[5] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint, RECT, RECT*, int>)&ID2D1TransformMapperMethods.MapInvalidRect;
|
||||
#else
|
||||
lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(QueryInterfaceWrapper);
|
||||
lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(AddRefWrapper);
|
||||
lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ReleaseWrapper);
|
||||
lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(MapInputRectsToOutputRectWrapper);
|
||||
lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(MapOutputRectToInputRectsWrapper);
|
||||
lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(MapInvalidRectWrapper);
|
||||
lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperMethods.QueryInterfaceWrapper);
|
||||
lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperMethods.AddRefWrapper);
|
||||
lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperMethods.ReleaseWrapper);
|
||||
lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperMethods.MapInputRectsToOutputRectWrapper);
|
||||
lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperMethods.MapOutputRectToInputRectsWrapper);
|
||||
lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperMethods.MapInvalidRectWrapper);
|
||||
#endif
|
||||
|
||||
// ID2D1TransformMapperInternal
|
||||
#if NET6_0_OR_GREATER
|
||||
lpVtbl[6 + 0] = (delegate* unmanaged<D2D1TransformMapperImpl*, Guid*, void**, int>)&ID2D1TransformMapperInternalMethods.QueryInterface;
|
||||
lpVtbl[6 + 1] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint>)&ID2D1TransformMapperInternalMethods.AddRef;
|
||||
lpVtbl[6 + 2] = (delegate* unmanaged<D2D1TransformMapperImpl*, uint>)&ID2D1TransformMapperInternalMethods.Release;
|
||||
lpVtbl[6 + 3] = (delegate* unmanaged<D2D1TransformMapperImpl*, void**, int>)&ID2D1TransformMapperInternalMethods.GetManagedWrapperHandle;
|
||||
#else
|
||||
lpVtbl[6 + 0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperInternalMethods.QueryInterfaceWrapper);
|
||||
lpVtbl[6 + 1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperInternalMethods.AddRefWrapper);
|
||||
lpVtbl[6 + 2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperInternalMethods.ReleaseWrapper);
|
||||
lpVtbl[6 + 3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1TransformMapperInternalMethods.GetManagedWrapperHandleWrapper);
|
||||
#endif
|
||||
|
||||
return lpVtbl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The vtable pointer for the current instance.
|
||||
/// The vtable pointer for the current instance, for <see cref="ID2D1TransformMapper"/>.
|
||||
/// </summary>
|
||||
private void** lpVtbl;
|
||||
private void** lpVtblForID2D1TransformMapper;
|
||||
|
||||
/// <summary>
|
||||
/// The vtable pointer for the current instance, for <see cref="ID2D1TransformMapperInternal"/>.
|
||||
/// </summary>
|
||||
private void** lpVtblForID2D1TransformMapperInternal;
|
||||
|
||||
/// <summary>
|
||||
/// The current reference count for the object (from <see cref="IUnknown"/>).
|
||||
@@ -152,7 +119,8 @@ internal unsafe partial struct D2D1TransformMapperImpl
|
||||
{
|
||||
D2D1TransformMapperImpl* @this = (D2D1TransformMapperImpl*)NativeMemory.Alloc((nuint)sizeof(D2D1TransformMapperImpl));
|
||||
|
||||
@this->lpVtbl = Vtbl;
|
||||
@this->lpVtblForID2D1TransformMapper = VtblForID2D1TransformMapper;
|
||||
@this->lpVtblForID2D1TransformMapperInternal = VtblForID2D1TransformMapperInternal;
|
||||
@this->referenceCount = 1;
|
||||
@this->transformMapperHandle = GCHandle.Alloc(transformMapper);
|
||||
@this->spinLock = new SpinLock(enableThreadOwnerTracking: false);
|
||||
@@ -203,6 +171,7 @@ internal unsafe partial struct D2D1TransformMapperImpl
|
||||
return E.E_POINTER;
|
||||
}
|
||||
|
||||
// ID2D1TransformMapper
|
||||
if (riid->Equals(Windows.__uuidof<IUnknown>()) ||
|
||||
riid->Equals(ID2D1TransformMapper.Guid))
|
||||
{
|
||||
@@ -213,6 +182,16 @@ internal unsafe partial struct D2D1TransformMapperImpl
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
// ID2D1TransformMapperInternal
|
||||
if (riid->Equals(ID2D1TransformMapperInternal.Guid))
|
||||
{
|
||||
this.referenceCount++;
|
||||
|
||||
*ppvObject = (void**)Unsafe.AsPointer(ref this) + 1;
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
|
||||
*ppvObject = null;
|
||||
|
||||
return E.E_NOINTERFACE;
|
||||
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
#pragma warning disable CS0649
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
|
||||
|
||||
/// <summary>
|
||||
/// The internal transform mapper manager interface.
|
||||
/// </summary>
|
||||
[Guid("C5D8FC65-FB86-4C2D-9EF5-95AEC639C952")]
|
||||
internal unsafe struct ID2D1TransformMapperInternal
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="System.Guid"/> for <see cref="ID2D1TransformMapper"/> (<c>C5D8FC65-FB86-4C2D-9EF5-95AEC639C952</c>).
|
||||
/// </summary>
|
||||
public static ref readonly Guid Guid
|
||||
{
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0x65, 0xFC, 0xD8, 0xC5,
|
||||
0x86, 0xFB,
|
||||
0x2D, 0x4C,
|
||||
0x9E, 0xF5,
|
||||
0x95,
|
||||
0xAE,
|
||||
0xC6,
|
||||
0x39,
|
||||
0xC9,
|
||||
0x52
|
||||
};
|
||||
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The vtable for the current instance.
|
||||
/// </summary>
|
||||
private readonly void** lpVtbl;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="GCHandle"/> value for the managed wrapper associated with this CCW.
|
||||
/// </summary>
|
||||
/// <param name="ppvHandle">The <see cref="GCHandle"/> value for the managed wrapper associated with this CCW.</param>
|
||||
/// <returns>An <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public HRESULT GetManagedWrapperHandle(void** ppvHandle)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1TransformMapperInternal*, void**, int>)this.lpVtbl[3])(
|
||||
(ID2D1TransformMapperInternal*)Unsafe.AsPointer(ref this),
|
||||
ppvHandle);
|
||||
}
|
||||
}
|
||||
+1
@@ -35,6 +35,7 @@ internal abstract class D2D1TransformMapper<T, TParameters> : D2D1TransformMappe
|
||||
Unsafe.SkipInit(out output);
|
||||
|
||||
output.ToD2D1Infinite();
|
||||
|
||||
opaqueOutput = Rectangle.Empty;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -95,10 +95,10 @@ internal struct Rectangle64
|
||||
/// <param name="rectangle">The input <see cref="Rectangle64"/> instance to compute the union with.</param>
|
||||
public void Union(Rectangle64 rectangle)
|
||||
{
|
||||
long left = Math.Min(this.Left, rectangle.Left);
|
||||
long top = Math.Min(this.Top, rectangle.Top);
|
||||
long right = Math.Max(this.Right, rectangle.Right);
|
||||
long bottom = Math.Max(this.Bottom, rectangle.Bottom);
|
||||
long left = Math.Min(Left, rectangle.Left);
|
||||
long top = Math.Min(Top, rectangle.Top);
|
||||
long right = Math.Max(Right, rectangle.Right);
|
||||
long bottom = Math.Max(Bottom, rectangle.Bottom);
|
||||
|
||||
this.x = left;
|
||||
this.y = top;
|
||||
@@ -114,8 +114,8 @@ internal struct Rectangle64
|
||||
{
|
||||
long left = Math.Max(this.x, rectangle.x);
|
||||
long top = Math.Max(this.y, rectangle.y);
|
||||
long right = Math.Min(this.Right, rectangle.Right);
|
||||
long bottom = Math.Min(this.Bottom, rectangle.Bottom);
|
||||
long right = Math.Min(Right, rectangle.Right);
|
||||
long bottom = Math.Min(Bottom, rectangle.Bottom);
|
||||
|
||||
this.x = left;
|
||||
this.y = top;
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: ComVisible(false)]
|
||||
@@ -215,7 +215,7 @@ internal struct ReferenceTracker : IDisposable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A reference tracking lease to extend the lifetime of a given <see cref="ReferenceTrackedObject"/> instance while in a given scope.
|
||||
/// A reference tracking lease to extend the lifetime of a given <see cref="IReferenceTrackedObject"/> instance while in a given scope.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type must always be used in a <see langword="using"/> statement and disposed properly. Not doing
|
||||
@@ -224,7 +224,7 @@ internal struct ReferenceTracker : IDisposable
|
||||
public struct Lease : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ReferenceTrackedObject"/> instance being wrapped, if any.
|
||||
/// The <see cref="IReferenceTrackedObject"/> instance being wrapped, if any.
|
||||
/// </summary>
|
||||
private IReferenceTrackedObject? trackedObject;
|
||||
|
||||
|
||||
@@ -2,20 +2,18 @@
|
||||
<Project>
|
||||
<Import Project="..\Directory.Build.targets" />
|
||||
|
||||
<!-- Enable trimming support on .NET 6 -->
|
||||
<Target Name="ConfigureAssemblyTrimmingIfNeeded"
|
||||
BeforeTargets="PrepareForBuild">
|
||||
|
||||
<!-- Enable trimming support on .NET 6 -->
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
|
||||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
|
||||
<IsTrimmable>true</IsTrimmable>
|
||||
</PropertyGroup>
|
||||
</Target>
|
||||
|
||||
<!-- Emit the [SupportedOSVersion] attribute if needed -->
|
||||
<Target Name="EmitSupportedOSVersionAttributeForTargetOS"
|
||||
BeforeTargets="PrepareForBuild">
|
||||
|
||||
<!-- Emit the [SupportedOSVersion] attribute if needed -->
|
||||
<ItemGroup Condition="'$(SupportedOSVersion)' != ''">
|
||||
<AssemblyAttribute Include="System.Runtime.Versioning.SupportedOSPlatformAttribute">
|
||||
<_Parameter1>$(SupportedOSVersion)</_Parameter1>
|
||||
@@ -23,9 +21,20 @@
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
<!-- Emit the [ComVisible(false)] attribute for UWP targets -->
|
||||
<Target Name="EmitComVisibleAttributeForUwpProjects"
|
||||
BeforeTargets="PrepareForBuild">
|
||||
<ItemGroup Condition="$(TargetFramework.StartsWith('uap10.0'))">
|
||||
<AssemblyAttribute Include="System.Runtime.InteropServices.ComVisibleAttribute">
|
||||
<_Parameter1>false</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
<!--
|
||||
The following target has been ported from TerraFX.Interop.Windows.
|
||||
See: https://github.com/terrafx/terrafx.interop.windows.
|
||||
This generates the [module: SkipLocalsInit] attribute for all projects.
|
||||
-->
|
||||
<PropertyGroup>
|
||||
<GeneratedSkipLocalsInitFile Condition="'$(GeneratedSkipLocalsInitFile)' == ''">$(IntermediateOutputPath)$(MSBuildProjectName).SkipLocalsInit.g.cs</GeneratedSkipLocalsInitFile>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
[Flags]
|
||||
internal enum D2D1_DEVICE_CONTEXT_OPTIONS : uint
|
||||
{
|
||||
D2D1_DEVICE_CONTEXT_OPTIONS_NONE = 0,
|
||||
D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS = 1,
|
||||
D2D1_DEVICE_CONTEXT_OPTIONS_FORCE_DWORD = 0xffffffff,
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
internal enum D2D1_PROPERTY : uint
|
||||
{
|
||||
D2D1_PROPERTY_CLSID = 0x80000000,
|
||||
D2D1_PROPERTY_DISPLAYNAME = 0x80000001,
|
||||
D2D1_PROPERTY_AUTHOR = 0x80000002,
|
||||
D2D1_PROPERTY_CATEGORY = 0x80000003,
|
||||
D2D1_PROPERTY_DESCRIPTION = 0x80000004,
|
||||
D2D1_PROPERTY_INPUTS = 0x80000005,
|
||||
D2D1_PROPERTY_CACHED = 0x80000006,
|
||||
D2D1_PROPERTY_PRECISION = 0x80000007,
|
||||
D2D1_PROPERTY_MIN_INPUTS = 0x80000008,
|
||||
D2D1_PROPERTY_MAX_INPUTS = 0x80000009,
|
||||
D2D1_PROPERTY_FORCE_DWORD = 0xffffffff
|
||||
}
|
||||
@@ -7,6 +7,8 @@ namespace TerraFX.Interop.DirectX;
|
||||
|
||||
internal enum D2D1_PROPERTY_TYPE : uint
|
||||
{
|
||||
D2D1_PROPERTY_TYPE_UNKNOWN = 0,
|
||||
D2D1_PROPERTY_TYPE_BOOL = 2,
|
||||
D2D1_PROPERTY_TYPE_BLOB = 9,
|
||||
D2D1_PROPERTY_TYPE_IUNKNOWN = 10
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
[Guid("B4F34A19-2383-4D76-94F6-EC343657C3DC")]
|
||||
[NativeTypeName("struct ID2D1CommandList : ID2D1Image")]
|
||||
[NativeInheritance("ID2D1Image")]
|
||||
internal unsafe partial struct ID2D1CommandList
|
||||
{
|
||||
public void** lpVtbl;
|
||||
}
|
||||
@@ -19,10 +19,31 @@ internal unsafe partial struct ID2D1DeviceContext
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public void GetFactory(ID2D1Factory** factory)
|
||||
{
|
||||
((delegate* unmanaged[Stdcall]<ID2D1DeviceContext*, ID2D1Factory**, void>)(lpVtbl[3]))((ID2D1DeviceContext*)Unsafe.AsPointer(ref this), factory);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(52)]
|
||||
public void GetDpi(float* dpiX, float* dpiY)
|
||||
{
|
||||
((delegate* unmanaged[Stdcall]<ID2D1DeviceContext*, float*, float*, void>)(lpVtbl[52]))((ID2D1DeviceContext*)Unsafe.AsPointer(ref this), dpiX, dpiY);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(63)]
|
||||
public HRESULT CreateEffect([NativeTypeName("const IID &")] Guid* effectId, ID2D1Effect** effect)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1DeviceContext*, Guid*, ID2D1Effect**, int>)(lpVtbl[63]))((ID2D1DeviceContext*)Unsafe.AsPointer(ref this), effectId, effect);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(75)]
|
||||
public void GetTarget(ID2D1Image** image)
|
||||
{
|
||||
((delegate* unmanaged[Stdcall]<ID2D1DeviceContext*, ID2D1Image**, void>)(lpVtbl[75]))((ID2D1DeviceContext*)Unsafe.AsPointer(ref this), image);
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,25 @@ internal unsafe partial struct ID2D1Effect
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1Effect*, uint, D2D1_PROPERTY_TYPE, byte*, uint, int>)(lpVtbl[9]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, type, data, dataSize);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(11)]
|
||||
public HRESULT GetValue([NativeTypeName("UINT32")] uint index, D2D1_PROPERTY_TYPE type, byte* data, [NativeTypeName("UINT32")] uint dataSize)
|
||||
{
|
||||
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)
|
||||
{
|
||||
((delegate* unmanaged[Stdcall]<ID2D1Effect*, uint, ID2D1Image**, void>)(lpVtbl[16]))((ID2D1Effect*)Unsafe.AsPointer(ref this), index, input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1_2.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
[Guid("D21768E1-23A4-4823-A14B-7C3EBA85D658")]
|
||||
[NativeTypeName("struct ID2D1Device1 : ID2D1Device")]
|
||||
[NativeInheritance("ID2D1Device")]
|
||||
internal unsafe partial struct ID2D1Device1
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1Device1*, Guid*, void**, int>)(lpVtbl[0]))((ID2D1Device1*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1Device1*, uint>)(lpVtbl[1]))((ID2D1Device1*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1Device1*, uint>)(lpVtbl[2]))((ID2D1Device1*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public void GetFactory(ID2D1Factory** factory)
|
||||
{
|
||||
((delegate* unmanaged[Stdcall]<ID2D1Device1*, ID2D1Factory**, void>)(lpVtbl[3]))((ID2D1Device1*)Unsafe.AsPointer(ref this), factory);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(4)]
|
||||
public HRESULT CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS options, ID2D1DeviceContext** deviceContext)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1Device1*, D2D1_DEVICE_CONTEXT_OPTIONS, ID2D1DeviceContext**, int>)(lpVtbl[4]))((ID2D1Device1*)Unsafe.AsPointer(ref this), options, deviceContext);
|
||||
}
|
||||
}
|
||||
@@ -34,4 +34,28 @@ internal static partial class CLSID
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
[NativeTypeName("const GUID")]
|
||||
public static ref readonly Guid CLSID_D2D1DpiCompensation
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data = new byte[] {
|
||||
0xC7, 0xC5, 0x26, 0x6C,
|
||||
0xE0, 0x34,
|
||||
0xFC, 0x46,
|
||||
0x9C,
|
||||
0xFD,
|
||||
0xE5,
|
||||
0x82,
|
||||
0x37,
|
||||
0x06,
|
||||
0xE2,
|
||||
0x28
|
||||
};
|
||||
|
||||
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1effects.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
internal enum D2D1_BORDER_MODE : uint
|
||||
{
|
||||
D2D1_BORDER_MODE_SOFT = 0,
|
||||
D2D1_BORDER_MODE_HARD = 1,
|
||||
D2D1_BORDER_MODE_FORCE_DWORD = 0xffffffff,
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1effects.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
internal enum D2D1_DPICOMPENSATION_INTERPOLATION_MODE : uint
|
||||
{
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_NEAREST_NEIGHBOR = 0,
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_LINEAR = 1,
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_CUBIC = 2,
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_MULTI_SAMPLE_LINEAR = 3,
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_ANISOTROPIC = 4,
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_HIGH_QUALITY_CUBIC = 5,
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE_FORCE_DWORD = 0xffffffff,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/d2d1effects.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
internal enum D2D1_DPICOMPENSATION_PROP : uint
|
||||
{
|
||||
D2D1_DPICOMPENSATION_PROP_INTERPOLATION_MODE = 0,
|
||||
D2D1_DPICOMPENSATION_PROP_BORDER_MODE = 1,
|
||||
D2D1_DPICOMPENSATION_PROP_INPUT_DPI = 2,
|
||||
D2D1_DPICOMPENSATION_PROP_FORCE_DWORD = 0xffffffff,
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/dcommon.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.DirectX;
|
||||
|
||||
internal partial struct D2D_VECTOR_2F
|
||||
{
|
||||
public float x;
|
||||
public float y;
|
||||
}
|
||||
@@ -27,16 +27,23 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1TransformGraph.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1TransformNode.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\CLSID.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\D2D1_BORDER_MODE.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\D2D1_DPICOMPENSATION_PROP.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1\D2D1_EXTEND_MODE.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1\ID2D1Factory.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1\ID2D1Image.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_CHANNEL_DEPTH.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_BUFFER_PRECISION.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_DEVICE_CONTEXT_OPTIONS.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_PROPERTY.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\D2D1_PROPERTY_TYPE.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1CommandList.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1DeviceContext.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1Effect.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1Factory1.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_1\ID2D1Multithread.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1_2\ID2D1Device1.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcommon\D3D_INCLUDE_TYPE.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcommon\D3D_SHADER_MACRO.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3d11shader\D3D11_SHADER_DESC.cs" />
|
||||
@@ -50,7 +57,10 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\DirectX\um\d3dcommon\D3D_TESSELLATOR_PARTITIONING.cs" Link="DirectX\um\d3dcommon\D3D_TESSELLATOR_PARTITIONING.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\DirectX\um\d3dcommon\D3D_TESSELLATOR_DOMAIN.cs" Link="DirectX\um\d3dcommon\D3D_TESSELLATOR_DOMAIN.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3d11shader\ID3D11ShaderReflection.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\dcommon\D2D_VECTOR_2F.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\other\helper-types\HSTRING.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\uuids\Windows.Manual.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)WinRT\um\windows.graphics.directx.direct3d11.interop\IDirect3DDxgiInterfaceAccess.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\ComPtr`1.cs" Link="ComPtr`1.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1DrawInfo.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\Windows\other\helper-types\HRESULT.cs" Link="Windows\other\helper-types\HRESULT.cs" />
|
||||
@@ -65,5 +75,8 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\E.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\RO.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\winerror\Windows.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)WinRT\winrt\hstring\HSTRING_HEADER.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)WinRT\winrt\roapi\WinRT.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)WinRT\winrt\winstring\WinRT.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from um/windows.graphics.directx.direct3d11.interop.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace TerraFX.Interop.WinRT;
|
||||
|
||||
[Guid("A9B3D012-3DF2-4EE3-B8D1-8695F457D3C1")]
|
||||
[NativeTypeName("struct IDirect3DDxgiInterfaceAccess : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe partial struct IDirect3DDxgiInterfaceAccess
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<IDirect3DDxgiInterfaceAccess*, Guid*, void**, int>)(lpVtbl[0]))((IDirect3DDxgiInterfaceAccess*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<IDirect3DDxgiInterfaceAccess*, uint>)(lpVtbl[1]))((IDirect3DDxgiInterfaceAccess*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<IDirect3DDxgiInterfaceAccess*, uint>)(lpVtbl[2]))((IDirect3DDxgiInterfaceAccess*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT GetInterface([NativeTypeName("const IID &")] Guid* iid, void** p)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<IDirect3DDxgiInterfaceAccess*, Guid*, void**, int>)(lpVtbl[3]))((IDirect3DDxgiInterfaceAccess*)Unsafe.AsPointer(ref this), iid, p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from winrt/hstring.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
namespace TerraFX.Interop.WinRT;
|
||||
|
||||
internal partial struct HSTRING_HEADER
|
||||
{
|
||||
[NativeTypeName("union (anonymous union at C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/winrt/hstring.h:80:5)")]
|
||||
public _Reserved_e__Union Reserved;
|
||||
|
||||
public unsafe partial struct _Reserved_e__Union
|
||||
{
|
||||
internal fixed byte Reserved1[24];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from winrt/roapi.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace TerraFX.Interop.WinRT;
|
||||
|
||||
internal static unsafe partial class WinRT
|
||||
{
|
||||
[DllImport("combase", ExactSpelling = true)]
|
||||
public static extern HRESULT RoGetActivationFactory(HSTRING activatableClassId, [NativeTypeName("const IID &")] Guid* iid, void** factory);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from winrt/winstring.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace TerraFX.Interop.WinRT;
|
||||
|
||||
internal static unsafe partial class WinRT
|
||||
{
|
||||
[DllImport("combase", ExactSpelling = true)]
|
||||
public static extern HRESULT WindowsCreateStringReference([NativeTypeName("PCWSTR")] ushort* sourceString, [NativeTypeName("UINT32")] uint length, HSTRING_HEADER* hstringHeader, HSTRING* @string);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
namespace TerraFX.Interop.WinRT;
|
||||
|
||||
internal readonly unsafe partial struct HSTRING
|
||||
{
|
||||
public readonly void* Value;
|
||||
|
||||
public HSTRING(void* value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,10 @@ internal static partial class D2DERR
|
||||
{
|
||||
[NativeTypeName("#define D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES _HRESULT_TYPEDEF_(0x88990026L)")]
|
||||
public const int D2DERR_INSUFFICIENT_DEVICE_CAPABILITIES = unchecked((int)(0x88990026));
|
||||
|
||||
[NativeTypeName("#define D2DERR_CYCLIC_GRAPH _HRESULT_TYPEDEF_(0x88990020L)")]
|
||||
public const int D2DERR_CYCLIC_GRAPH = unchecked((int)(0x88990020));
|
||||
|
||||
[NativeTypeName("#define D2DERR_EFFECT_IS_NOT_REGISTERED _HRESULT_TYPEDEF_(0x88990028L)")]
|
||||
public const int D2DERR_EFFECT_IS_NOT_REGISTERED = unchecked((int)(0x88990028));
|
||||
}
|
||||
@@ -7,6 +7,9 @@ namespace TerraFX.Interop.Windows;
|
||||
|
||||
internal static class E
|
||||
{
|
||||
[NativeTypeName("#define E_NOTFOUND HRESULT_FROM_WIN32(ERROR_NOT_FOUND)")]
|
||||
public const int E_NOTFOUND = -2147023728;
|
||||
|
||||
[NativeTypeName("#define E_NOT_VALID_STATE HRESULT_FROM_WIN32(ERROR_INVALID_STATE)")]
|
||||
public const int E_NOT_VALID_STATE = -2147019873;
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public partial class D2D1PixelShaderEffectTests
|
||||
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
|
||||
public unsafe void SetConstantBufferForD2D1Effect_NullD2D1Effect()
|
||||
{
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect<InvertEffect>(default, null);
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect<InvertEffect>(null, default);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
||||
@@ -630,6 +630,6 @@ public partial class D2D1PixelShaderTests
|
||||
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
|
||||
public unsafe void SetConstantBufferForD2D1DrawInfo_NullD2D1DrawInfo()
|
||||
{
|
||||
D2D1PixelShader.SetConstantBufferForD2D1DrawInfo<InvertEffect>(default, null);
|
||||
D2D1PixelShader.SetConstantBufferForD2D1DrawInfo<InvertEffect>(null, default);
|
||||
}
|
||||
}
|
||||
@@ -198,7 +198,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<IndexFrom2DResourceTextureShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default(IndexFrom2DResourceTextureShader), d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), default(IndexFrom2DResourceTextureShader));
|
||||
|
||||
using ComPtr<IUnknown> resourceTextureManager = default;
|
||||
|
||||
@@ -257,7 +257,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<IndexFrom2DResourceTextureShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default(IndexFrom2DResourceTextureShader), d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), default(IndexFrom2DResourceTextureShader));
|
||||
|
||||
using ComPtr<IUnknown> resourceTextureManager = default;
|
||||
|
||||
@@ -321,7 +321,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<IndexFrom2DResourceTextureShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default(IndexFrom2DResourceTextureShader), d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), default(IndexFrom2DResourceTextureShader));
|
||||
|
||||
string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
|
||||
string expectedPath = Path.Combine(assemblyPath, "Assets", "Landscape.png");
|
||||
@@ -498,7 +498,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
IndexFrom3DResourceTextureShader shader = new(expected.Height / 4);
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
using ComPtr<IUnknown> resourceTextureManager = default;
|
||||
|
||||
@@ -575,7 +575,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
CopyFromResourceTexture1DShader shader = new(width);
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
byte[] texture = new byte[width];
|
||||
|
||||
@@ -663,7 +663,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
CopyFromResourceTexture2DShader shader = new(width, height);
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
byte[] texture = new byte[width * height];
|
||||
|
||||
@@ -766,7 +766,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
CopyFromResourceTexture3DShader shader = new(width, height, depth);
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
byte[] texture = new byte[width * height * depth];
|
||||
|
||||
@@ -860,7 +860,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<NullResourceTextureShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default(NullResourceTextureShader), d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), default(NullResourceTextureShader));
|
||||
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), 128, 128);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ using Win32.Graphics.Direct2D;
|
||||
|
||||
namespace ComputeSharp.D2D1.Tests;
|
||||
|
||||
using HRESULT = HResult;
|
||||
using D2D1_MAPPED_RECT = MappedRect;
|
||||
using Rectangle = System.Drawing.Rectangle;
|
||||
|
||||
@@ -28,16 +29,20 @@ public partial class D2D1TransformMapperTests
|
||||
|
||||
using ComPtr<IUnknown> unknown = default;
|
||||
using ComPtr<IUnknown> transformMapper2 = default;
|
||||
using ComPtr<IUnknown> transformMapperInternal = default;
|
||||
|
||||
Guid uuidOfIUnknown = typeof(IUnknown).GUID;
|
||||
Guid uuidOfTransformMapper = new("02E6D48D-B892-4FBC-AA54-119203BAB802");
|
||||
Guid uuidOfTransformMapperInternal = new("C5D8FC65-FB86-4C2D-9EF5-95AEC639C952");
|
||||
|
||||
// The object implements IUnknown and the transform mapper interface
|
||||
// The object implements IUnknown and the transform mapper interfaces
|
||||
Assert.AreEqual(CustomQueryInterfaceResult.Handled, ((ICustomQueryInterface)transformMapper).GetInterface(ref uuidOfIUnknown, out *(IntPtr*)unknown.GetAddressOf()));
|
||||
Assert.AreEqual(CustomQueryInterfaceResult.Handled, ((ICustomQueryInterface)transformMapper).GetInterface(ref uuidOfTransformMapper, out *(IntPtr*)transformMapper2.GetAddressOf()));
|
||||
Assert.AreEqual(CustomQueryInterfaceResult.Handled, ((ICustomQueryInterface)transformMapper).GetInterface(ref uuidOfTransformMapperInternal, out *(IntPtr*)transformMapperInternal.GetAddressOf()));
|
||||
|
||||
Assert.IsTrue(unknown.Get() is not null);
|
||||
Assert.IsTrue(transformMapper2.Get() is not null);
|
||||
Assert.IsTrue(transformMapperInternal.Get() is not null);
|
||||
|
||||
using ComPtr<IUnknown> garbage = default;
|
||||
|
||||
@@ -96,6 +101,41 @@ public partial class D2D1TransformMapperTests
|
||||
Assert.AreEqual(2u, transformMapper2.Get()->Release());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void VerifyManagedWrapperRetrieval()
|
||||
{
|
||||
D2D1TransformMapper<HelloWorld> transformMapper = D2D1TransformMapperFactory<HelloWorld>.Inflate(4);
|
||||
|
||||
using ComPtr<IUnknown> unknown = default;
|
||||
using ComPtr<IUnknown> transformMapperInternal = default;
|
||||
|
||||
Guid uuidOfIUnknown = typeof(IUnknown).GUID;
|
||||
Guid uuidOfTransformMapperInternal = new("C5D8FC65-FB86-4C2D-9EF5-95AEC639C952");
|
||||
|
||||
Assert.AreEqual(CustomQueryInterfaceResult.Handled, ((ICustomQueryInterface)transformMapper).GetInterface(ref uuidOfIUnknown, out *(IntPtr*)unknown.GetAddressOf()));
|
||||
|
||||
Assert.IsTrue(unknown.Get() is not null);
|
||||
|
||||
HRESULT hresult = unknown.CopyTo(&uuidOfTransformMapperInternal, (void**)transformMapperInternal.GetAddressOf());
|
||||
|
||||
Assert.AreEqual(hresult, HRESULT.Ok);
|
||||
Assert.IsTrue(transformMapperInternal.Get() is not null);
|
||||
|
||||
IntPtr handlePtr;
|
||||
|
||||
// Invoke GetManagedWrapperHandle
|
||||
hresult = ((delegate* unmanaged[Stdcall]<IUnknown*, void**, int>)(*(void***)transformMapperInternal.Get())[3])(
|
||||
transformMapperInternal.Get(),
|
||||
(void**)&handlePtr);
|
||||
|
||||
Assert.AreEqual(hresult, HRESULT.Ok);
|
||||
|
||||
GCHandle handle = GCHandle.FromIntPtr(handlePtr);
|
||||
|
||||
Assert.IsNotNull(handle.Target);
|
||||
Assert.AreSame(handle.Target, transformMapper);
|
||||
}
|
||||
|
||||
[D2DInputCount(0)]
|
||||
private partial struct DummyShader : ID2D1PixelShader
|
||||
{
|
||||
@@ -118,7 +158,7 @@ public partial class D2D1TransformMapperTests
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<ShaderWithDispatchArea>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new ShaderWithDispatchArea(111, 222), d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), new ShaderWithDispatchArea(111, 222));
|
||||
|
||||
DispatchAreaTransformMapper transformMapper = new() { ExpectedWidth = 111, ExpectedHeight = 222 };
|
||||
|
||||
|
||||
@@ -425,9 +425,9 @@ public sealed partial class BokehBlurEffect
|
||||
// Set the constant buffers
|
||||
for (int i = 0; i < numberOfComponents; i++)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new VerticalConvolution.Shader((int)kernelSize), verticalConvolutionEffectsForReals[i].Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new VerticalConvolution.Shader((int)kernelSize), verticalConvolutionEffectsForImaginaries[i].Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(new HorizontalConvolutionAndAccumulatePartials.Shader((int)kernelSize, this.kernelParameters[i].Z, this.kernelParameters[i].W), horizontalConvolutionEffects[i].Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(verticalConvolutionEffectsForReals[i].Get(), new VerticalConvolution.Shader((int)kernelSize));
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(verticalConvolutionEffectsForImaginaries[i].Get(), new VerticalConvolution.Shader((int)kernelSize));
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(horizontalConvolutionEffects[i].Get(), new HorizontalConvolutionAndAccumulatePartials.Shader((int)kernelSize, this.kernelParameters[i].Z, this.kernelParameters[i].W));
|
||||
}
|
||||
|
||||
// Build the effect graph:
|
||||
|
||||
@@ -124,7 +124,7 @@ internal static class D2D1TestRunner
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<T>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
if (transformMapper is not null)
|
||||
{
|
||||
@@ -173,7 +173,7 @@ internal static class D2D1TestRunner
|
||||
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<T>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(d2D1Effect.Get(), in shader);
|
||||
|
||||
if (transformMapper is not null)
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする