From 36326f2336f89a9838a38c30e3ee23e9bb91bf19 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 17 Nov 2022 22:45:31 +0100 Subject: [PATCH] Implement D2D1DrawInfoUpdateContext type --- .../Interop/D2D1DrawInfoUpdateContext{T}.cs | 47 +++++++++++++++++-- ...lShaderEffect.ID2D1DrawTransformMethods.cs | 2 +- ...teContextImpl.ID2D1DrawInfoUpdateContex.cs | 8 ++-- .../D2D1DrawInfoUpdateContextImpl.cs | 12 ++--- ...ontex.cs => ID2D1DrawInfoUpdateContext.cs} | 16 +++---- ... => ID2D1DrawInfoUpdateContextInternal.cs} | 4 +- .../TransformMappers/ID2D1TransformMapper.cs | 6 +-- .../Loaders/D2D1DrawInfoDispatchDataLoader.cs | 5 +- ...DrawInfoUpdateContextDispatchDataLoader.cs | 39 +++++++++++++++ 9 files changed, 109 insertions(+), 30 deletions(-) rename src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/{ID2D1DrawInfoUpdateContex.cs => ID2D1DrawInfoUpdateContext.cs} (86%) rename src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/{ID2D1DrawInfoUpdateContexInternal.cs => ID2D1DrawInfoUpdateContextInternal.cs} (87%) create mode 100644 src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoUpdateContextDispatchDataLoader.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs index f42dfeb5..e725eaa1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1DrawInfoUpdateContext{T}.cs @@ -1,4 +1,10 @@ -using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using ComputeSharp.D2D1.Extensions; +using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; +using ComputeSharp.D2D1.Shaders.Loaders; + +#pragma warning disable CS0618 namespace ComputeSharp.D2D1.Interop; @@ -6,16 +12,47 @@ namespace ComputeSharp.D2D1.Interop; /// An object allowing to interact with the shader data from within a transform. /// /// The type of shader the transform will interact with. -public readonly ref struct D2D1DrawInfoUpdateContext +public readonly unsafe ref struct D2D1DrawInfoUpdateContext where T : unmanaged, ID2D1PixelShader { + /// + /// The instance associated with the current object. + /// + private readonly ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The instance associated with the current object. + internal D2D1DrawInfoUpdateContext(ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext) + { + this.d2D1DrawInfoUpdateContext = d2D1DrawInfoUpdateContext; + } + /// /// Retrieves the instance currently being used in the associated effect. /// /// The instance currently being used in the associated effect. public T GetConstantBuffer() { - throw new NotImplementedException(); + uint constantBufferSize; + + this.d2D1DrawInfoUpdateContext->GetConstantBufferSize(&constantBufferSize).Assert(); + + byte[] buffer = ArrayPool.Shared.Rent((int)constantBufferSize); + + fixed (byte* pBuffer = buffer) + { + this.d2D1DrawInfoUpdateContext->GetConstantBuffer(pBuffer, constantBufferSize).Assert(); + } + + Unsafe.SkipInit(out T shader); + + shader.InitializeFromDispatchData(buffer); + + ArrayPool.Shared.Return(buffer); + + return shader; } /// @@ -24,6 +61,8 @@ public readonly ref struct D2D1DrawInfoUpdateContext /// The instance to be used in the associated effect. public void SetConstantBuffer(in T shader) { - throw new NotImplementedException(); + D2D1DrawInfoUpdateContextDispatchDataLoader dataLoader = new(this.d2D1DrawInfoUpdateContext); + + Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 0347be41..a5d848eb 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -192,7 +192,7 @@ partial struct PixelShaderEffect // Forward the call to the input ID2D1TransformMapper instance hresult = @this->d2D1TransformMapper->MapInputRectsToOutputRect( - updateContext: (ID2D1DrawInfoUpdateContex*)d2D1DrawInfoUpdateContext.Get(), + updateContext: (ID2D1DrawInfoUpdateContext*)d2D1DrawInfoUpdateContext.Get(), inputRects: inputRects, inputOpaqueSubRects: inputOpaqueSubRects, inputRectCount: inputRectCount, diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs index 906fd3aa..5a48809b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs @@ -11,7 +11,7 @@ namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; partial struct D2D1DrawInfoUpdateContextImpl { /// - /// The implementation for . + /// The implementation for . /// private static unsafe class ID2D1DrawInfoUpdateContexMethods { @@ -80,7 +80,7 @@ partial struct D2D1DrawInfoUpdateContextImpl return @this->Release(); } - /// + /// [UnmanagedCallersOnly] public static int GetConstantBufferSize(D2D1DrawInfoUpdateContextImpl* @this, uint* size) { @@ -99,7 +99,7 @@ partial struct D2D1DrawInfoUpdateContextImpl return S.S_OK; } - /// + /// [UnmanagedCallersOnly] public static int GetConstantBuffer(D2D1DrawInfoUpdateContextImpl* @this, byte* buffer, uint bufferCount) { @@ -131,7 +131,7 @@ partial struct D2D1DrawInfoUpdateContextImpl return S.S_OK; } - /// + /// [UnmanagedCallersOnly] public static int SetConstantBuffer(D2D1DrawInfoUpdateContextImpl* @this, byte* buffer, uint bufferCount) { diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs index c549a45e..840d0729 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1DrawInfoUpdateContextImpl.cs @@ -13,7 +13,7 @@ using RuntimeHelpers = ComputeSharp.D2D1.NetStandard.System.Runtime.CompilerServ namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; /// -/// An implementation of the and interfaces. +/// An implementation of the and interfaces. /// internal unsafe partial struct D2D1DrawInfoUpdateContextImpl { @@ -32,7 +32,7 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl #endif /// - /// The shared vtable pointer for instance, for . + /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1DrawInfoUpdateContex = InitVtblForID2D1DrawInfoUpdateContexAndID2D1DrawInfoUpdateContexInternal(); @@ -42,9 +42,9 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl private static readonly void** VtblForID2D1DrawInfoUpdateContexInternal = &VtblForID2D1DrawInfoUpdateContex[5]; /// - /// Initializes the combined vtable for and . + /// Initializes the combined vtable for and . /// - /// The combined vtable for and . + /// The combined vtable for and . private static void** InitVtblForID2D1DrawInfoUpdateContexAndID2D1DrawInfoUpdateContexInternal() { void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(D2D1DrawInfoUpdateContextImpl), sizeof(void*) * 10); @@ -83,7 +83,7 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl } /// - /// The vtable pointer for the current instance, for . + /// The vtable pointer for the current instance, for . /// private void** lpVtblForID2D1DrawInfoUpdateContex; @@ -161,7 +161,7 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl // ID2D1DrawInfoUpdateContex if (riid->Equals(Windows.__uuidof()) || - riid->Equals(ID2D1DrawInfoUpdateContex.Guid)) + riid->Equals(ID2D1DrawInfoUpdateContext.Guid)) { _ = Interlocked.Increment(ref this.referenceCount); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContex.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContext.cs similarity index 86% rename from src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContex.cs rename to src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContext.cs index 983bb4a1..5c3886ca 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContex.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContext.cs @@ -11,10 +11,10 @@ namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; /// The updater for type to use with built-in effects. /// [Guid("430C5B40-AE16-485F-90E6-4FA4915144B6")] -internal unsafe struct ID2D1DrawInfoUpdateContex +internal unsafe struct ID2D1DrawInfoUpdateContext { /// - /// Gets the for (430C5B40-AE16-485F-90E6-4FA4915144B6). + /// Gets the for (430C5B40-AE16-485F-90E6-4FA4915144B6). /// public static ref readonly Guid Guid { @@ -50,8 +50,8 @@ internal unsafe struct ID2D1DrawInfoUpdateContex [MethodImpl(MethodImplOptions.AggressiveInlining)] public HRESULT GetConstantBufferSize(uint* size) { - return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( - (ID2D1DrawInfoUpdateContex*)Unsafe.AsPointer(ref this), + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( + (ID2D1DrawInfoUpdateContext*)Unsafe.AsPointer(ref this), size); } @@ -64,8 +64,8 @@ internal unsafe struct ID2D1DrawInfoUpdateContex [MethodImpl(MethodImplOptions.AggressiveInlining)] public HRESULT GetConstantBuffer(byte* buffer, uint bufferCount) { - return ((delegate* unmanaged[Stdcall])this.lpVtbl[4])( - (ID2D1DrawInfoUpdateContex*)Unsafe.AsPointer(ref this), + return ((delegate* unmanaged[Stdcall])this.lpVtbl[4])( + (ID2D1DrawInfoUpdateContext*)Unsafe.AsPointer(ref this), buffer, bufferCount); } @@ -79,8 +79,8 @@ internal unsafe struct ID2D1DrawInfoUpdateContex [MethodImpl(MethodImplOptions.AggressiveInlining)] public HRESULT SetConstantBuffer(byte* buffer, uint bufferCount) { - return ((delegate* unmanaged[Stdcall])this.lpVtbl[5])( - (ID2D1DrawInfoUpdateContex*)Unsafe.AsPointer(ref this), + return ((delegate* unmanaged[Stdcall])this.lpVtbl[5])( + (ID2D1DrawInfoUpdateContext*)Unsafe.AsPointer(ref this), buffer, bufferCount); } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContexInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContextInternal.cs similarity index 87% rename from src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContexInternal.cs rename to src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContextInternal.cs index d69abb8e..440ad3b1 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContexInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1DrawInfoUpdateContextInternal.cs @@ -8,7 +8,7 @@ using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; /// -/// An internal version of that supports being closed, to manage lifetime scopes. +/// An internal version of that supports being closed, to manage lifetime scopes. /// [Guid("CF2F5BB0-3F5E-4D6E-8FF1-D9A7EB6C0250")] internal unsafe struct ID2D1DrawInfoUpdateContexInternal @@ -49,6 +49,6 @@ internal unsafe struct ID2D1DrawInfoUpdateContexInternal [MethodImpl(MethodImplOptions.AggressiveInlining)] public HRESULT Close() { - return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ID2D1DrawInfoUpdateContex*)Unsafe.AsPointer(ref this)); + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ID2D1DrawInfoUpdateContext*)Unsafe.AsPointer(ref this)); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapper.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapper.cs index 0aa65f52..db26e42b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapper.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapper.cs @@ -45,7 +45,7 @@ internal unsafe struct ID2D1TransformMapper /// /// Performs the inverse mapping to . /// - /// The input instance. + /// The input instance. /// An array of input rectangles to be mapped to the output rectangle. /// An array of input rectangles to be mapped to the opaque output rectangle. /// The number of inputs specified. @@ -54,14 +54,14 @@ internal unsafe struct ID2D1TransformMapper /// An for the operation. [MethodImpl(MethodImplOptions.AggressiveInlining)] public HRESULT MapInputRectsToOutputRect( - ID2D1DrawInfoUpdateContex* updateContext, + ID2D1DrawInfoUpdateContext* updateContext, RECT* inputRects, RECT* inputOpaqueSubRects, uint inputRectCount, RECT* outputRect, RECT* outputOpaqueSubRect) { - return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])( (ID2D1TransformMapper*)Unsafe.AsPointer(ref this), updateContext, inputRects, diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs index 0f302f4b..88bf71d0 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoDispatchDataLoader.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.__Internals; +using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.DirectX; #pragma warning disable CS0618 @@ -31,8 +32,8 @@ internal readonly unsafe struct D2D1DrawInfoDispatchDataLoader : ID2D1DispatchDa /// void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data) { - _ = this.d2D1DrawInfo->SetPixelShaderConstantBuffer( + this.d2D1DrawInfo->SetPixelShaderConstantBuffer( buffer: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)), - bufferCount: (uint)data.Length); + bufferCount: (uint)data.Length).Assert(); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoUpdateContextDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoUpdateContextDispatchDataLoader.cs new file mode 100644 index 00000000..74a3690d --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1DrawInfoUpdateContextDispatchDataLoader.cs @@ -0,0 +1,39 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.D2D1.__Internals; +using ComputeSharp.D2D1.Extensions; +using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.Shaders.Loaders; + +/// +/// A data loader for D2D1 pixel shaders to be passed onto an object. +/// +internal readonly unsafe struct D2D1DrawInfoUpdateContextDispatchDataLoader : ID2D1DispatchDataLoader +{ + /// + /// The object in use. + /// + private readonly ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext; + + /// + /// Creates a new instance. + /// + /// The object to use. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal D2D1DrawInfoUpdateContextDispatchDataLoader(ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext) + { + this.d2D1DrawInfoUpdateContext = d2D1DrawInfoUpdateContext; + } + + /// + void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan data) + { + this.d2D1DrawInfoUpdateContext->SetConstantBuffer( + buffer: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)), + bufferCount: (uint)data.Length).Assert(); + } +} \ No newline at end of file