From 48a368164c0047bffed6f2509f4a88ed50998317 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 31 Oct 2022 23:23:00 +0100 Subject: [PATCH] Add ID2D1TransformMapperManager property and accessors --- .../Effects/PixelShaderEffect.Properties.cs | 128 +++++++++++++++++- .../Interop/Effects/PixelShaderEffect.cs | 13 ++ 2 files changed, 139 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 4f454906..d3128646 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; +using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMapperManagers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER @@ -31,6 +32,16 @@ unsafe partial struct PixelShaderEffect /// A cached instance wrapping . /// private static readonly PropertySetFunctionDelegate SetConstantBufferWrapper = SetConstantBufferImpl; + + /// + /// A cached instance wrapping . + /// + private static readonly PropertyGetFunctionDelegate GetTransformMapperManagerWrapper = GetTransformMapperManagerImpl; + + /// + /// A cached instance wrapping . + /// + private static readonly PropertySetFunctionDelegate SetTransformMapperManagerWrapper = SetTransformMapperManagerImpl; #endif /// @@ -67,6 +78,40 @@ unsafe partial struct PixelShaderEffect #endif } + /// + /// Gets the get accessor for the transform mapper manager. + /// +#if NET6_0_OR_GREATER + public static delegate* unmanaged[Stdcall] GetTransformMapperManager +#else + public static void* GetTransformMapperManager +#endif + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#if NET6_0_OR_GREATER + get => &GetTransformMapperManagerImpl; +#else + get => (void*)Marshal.GetFunctionPointerForDelegate(GetTransformMapperManagerWrapper); +#endif + } + + /// + /// Gets the set accessor for the transform mapper manager. + /// +#if NET6_0_OR_GREATER + public static delegate* unmanaged[Stdcall] SetTransformMapperManager +#else + public static void* SetTransformMapperManager +#endif + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#if NET6_0_OR_GREATER + get => &SetTransformMapperManagerImpl; +#else + get => (void*)Marshal.GetFunctionPointerForDelegate(SetTransformMapperManagerWrapper); +#endif + } + /// [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] private static int GetConstantBufferImpl(IUnknown* effect, byte* data, uint dataSize, uint* actualSize) @@ -119,13 +164,13 @@ unsafe partial struct PixelShaderEffect [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] private static int SetConstantBufferImpl(IUnknown* effect, byte* data, uint dataSize) { + PixelShaderEffect* @this = (PixelShaderEffect*)effect; + if (data is null) { return E.E_POINTER; } - PixelShaderEffect* @this = (PixelShaderEffect*)effect; - if (dataSize != (uint)@this->constantBufferSize) { return E.E_INVALIDARG; @@ -148,6 +193,85 @@ unsafe partial struct PixelShaderEffect return S.S_OK; } + /// + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + private static int GetTransformMapperManagerImpl(IUnknown* effect, byte* data, uint dataSize, uint* actualSize) + { + PixelShaderEffect* @this = (PixelShaderEffect*)effect; + + if (data is null) + { + return E.E_POINTER; + } + + if (dataSize < sizeof(void*)) + { + return E.E_INVALIDARG; + } + + if (@this->d2D1TransformMapperManager is null) + { + *(void**)data = null; + } + else + { + _ = ((IUnknown*)@this->d2D1TransformMapperManager)->QueryInterface(Windows.__uuidof(), (void**)data); + } + + if (actualSize is not null) + { + *actualSize = (uint)sizeof(void*); + } + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + private static int SetTransformMapperManagerImpl(IUnknown* effect, byte* data, uint dataSize) + { + PixelShaderEffect* @this = (PixelShaderEffect*)effect; + + if (data is null) + { + return E.E_POINTER; + } + + void* value = *(void**)data; + + if (value is null) + { + return E.E_POINTER; + } + + if (dataSize != (uint)sizeof(void*)) + { + return E.E_INVALIDARG; + } + + using ComPtr unknown = (IUnknown*)value; + using ComPtr transformMapperManager = default; + + // Check that the input object implements ID2D1TransformMapperManager + int result = unknown.CopyTo(transformMapperManager.GetAddressOf()); + + if (result != S.S_OK) + { + return result; + } + + // If there's already an existing manager, release it + if (@this->d2D1TransformMapperManager is not null) + { + _ = ((IUnknown*)@this->d2D1TransformMapperManager)->Release(); + } + + // Store the transform mapper manager into the effect + @this->d2D1TransformMapperManager = transformMapperManager.Detach(); + + return S.S_OK; + } + /// /// Gets the resource texture manager for a given index. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 136112bc..c4e9fd1c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -3,6 +3,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; +using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMapperManagers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if NET6_0_OR_GREATER @@ -200,6 +201,12 @@ internal unsafe partial struct PixelShaderEffect /// private GCHandle d2D1TransformMapperHandle; + /// + /// The instance to use, if any. + /// + /// If present, this takes precedence over . + private ID2D1TransformMapperManager* d2D1TransformMapperManager; + /// /// The instance currently in use. /// @@ -284,6 +291,7 @@ internal unsafe partial struct PixelShaderEffect @this->resourceTextureDescriptionCount = resourceTextureDescriptionCount; @this->resourceTextureDescriptions = resourceTextureDescriptions; @this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper); + @this->d2D1TransformMapperManager = null; @this->d2D1DrawInfo = null; @this->d2D1EffectContext = null; @this->resourceTextureManagerBuffer = default; @@ -349,6 +357,11 @@ internal unsafe partial struct PixelShaderEffect this.d2D1TransformMapperHandle.Free(); + if (this.d2D1TransformMapperManager is not null) + { + _ = ((IUnknown*)this.d2D1TransformMapperManager)->Release(); + } + if (this.d2D1DrawInfo is not null) { _ = this.d2D1DrawInfo->Release();