From 2481bc7e0a226713dd01f961ab69f311a6b2ed91 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 30 Oct 2022 23:59:22 +0100 Subject: [PATCH] Complete D2D1DrawInfoUpdateContextImpl implementation --- .../Effects/PixelShaderEffect.Properties.cs | 13 ++-- ...teContextImpl.ID2D1DrawInfoUpdateContex.cs | 66 ++++++++++++++++++- ...tImpl.ID2D1DrawInfoUpdateContexInternal.cs | 2 +- .../D2D1DrawInfoUpdateContextImpl.cs | 30 ++++++++- .../TerraFX.Interop.Windows.D2D1.projitems | 1 + .../Windows/shared/winerror/RO.cs | 12 ++++ 6 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/RO.cs diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index 44d86690..4f454906 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -131,16 +131,19 @@ unsafe partial struct PixelShaderEffect return E.E_INVALIDARG; } + // Reuse the existing buffer if there is one, otherwise allocate a new one if (@this->constantBuffer is not null) { - NativeMemory.Free(@this->constantBuffer); + Buffer.MemoryCopy(data, @this->constantBuffer, dataSize, dataSize); } + else + { + void* buffer = NativeMemory.Alloc(dataSize); - void* buffer = NativeMemory.Alloc(dataSize); + Buffer.MemoryCopy(data, buffer, dataSize, dataSize); - Buffer.MemoryCopy(data, buffer, dataSize, dataSize); - - @this->constantBuffer = (byte*)buffer; + @this->constantBuffer = (byte*)buffer; + } return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs index 2ced1422..53b61c26 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContex.cs @@ -84,7 +84,17 @@ partial struct D2D1DrawInfoUpdateContextImpl [UnmanagedCallersOnly] public static int GetConstantBufferSize(D2D1DrawInfoUpdateContextImpl* @this, uint* size) { - // TODO + if (@this->d2D1DrawInfo is null) + { + return RO.RO_E_CLOSED; + } + + if (size is null) + { + return E.E_POINTER; + } + + *size = (uint)@this->constantBufferSize; return S.S_OK; } @@ -93,7 +103,30 @@ partial struct D2D1DrawInfoUpdateContextImpl [UnmanagedCallersOnly] public static int GetConstantBuffer(D2D1DrawInfoUpdateContextImpl* @this, byte* buffer, uint bufferCount) { - // TODO + if (@this->d2D1DrawInfo is null) + { + return RO.RO_E_CLOSED; + } + + if (buffer is null) + { + return E.E_POINTER; + } + + if (bufferCount < @this->constantBufferSize) + { + return E.E_NOT_SUFFICIENT_BUFFER; + } + + if (@this->constantBuffer is null) + { + return E.E_NOT_VALID_STATE; + } + + if (@this->constantBufferSize > 0) + { + Buffer.MemoryCopy(@this->constantBuffer, buffer, bufferCount, @this->constantBufferSize); + } return S.S_OK; } @@ -102,7 +135,34 @@ partial struct D2D1DrawInfoUpdateContextImpl [UnmanagedCallersOnly] public static int SetConstantBuffer(D2D1DrawInfoUpdateContextImpl* @this, byte* buffer, uint bufferCount) { - // TODO + if (@this->d2D1DrawInfo is null) + { + return RO.RO_E_CLOSED; + } + + if (buffer is null) + { + return E.E_POINTER; + } + + if (bufferCount != (uint)@this->constantBufferSize) + { + return E.E_INVALIDARG; + } + + // Reuse the existing buffer if there is one, otherwise allocate a new one + if (@this->constantBuffer is not null) + { + Buffer.MemoryCopy(buffer, @this->constantBuffer, bufferCount, bufferCount); + } + else + { + void* constantBuffer = NativeMemory.Alloc(bufferCount); + + Buffer.MemoryCopy(buffer, constantBuffer, bufferCount, bufferCount); + + @this->constantBuffer = (byte*)constantBuffer; + } return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs index 7bbce424..dd81b194 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.ID2D1DrawInfoUpdateContexInternal.cs @@ -74,7 +74,7 @@ partial struct D2D1DrawInfoUpdateContextImpl { @this = (D2D1DrawInfoUpdateContextImpl*)&((void**)@this)[-1]; - // TODO + @this->d2D1DrawInfo = null; return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.cs index 84284fa1..e00f3a34 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMapperManagers/D2D1DrawInfoUpdateContextImpl.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if NET6_0_OR_GREATER using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers; @@ -96,11 +97,33 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl /// private volatile int referenceCount; + /// + /// The constant buffer data, if set. + /// + private byte* constantBuffer; + + /// + /// The size of . + /// + private int constantBufferSize; + + /// + /// The instance currently in use. + /// + private ID2D1DrawInfo* d2D1DrawInfo; + /// /// The factory method for instances. /// /// The resulting draw info update context instance. - public static void Factory(D2D1DrawInfoUpdateContextImpl** drawInfoUpdateContext) + /// The constant buffer data, if set. + /// The size of . + /// The instance currently in use. + public static void Factory( + D2D1DrawInfoUpdateContextImpl** drawInfoUpdateContext, + byte* constantBuffer, + int constantBufferSize, + ID2D1DrawInfo* d2D1DrawInfo) { D2D1DrawInfoUpdateContextImpl* @this = (D2D1DrawInfoUpdateContextImpl*)NativeMemory.Alloc((nuint)sizeof(D2D1DrawInfoUpdateContextImpl)); @@ -109,6 +132,9 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl @this->lpVtblForID2D1DrawInfoUpdateContex = VtblForID2D1DrawInfoUpdateContex; @this->lpVtblForID2D1DrawInfoUpdateContexInternal = VtblForID2D1DrawInfoUpdateContexInternal; @this->referenceCount = 1; + @this->constantBuffer = constantBuffer; + @this->constantBufferSize = constantBufferSize; + @this->d2D1DrawInfo = d2D1DrawInfo; *drawInfoUpdateContext = @this; } @@ -160,8 +186,6 @@ internal unsafe partial struct D2D1DrawInfoUpdateContextImpl if (referenceCount == 0) { - // TODO - NativeMemory.Free(Unsafe.AsPointer(ref this)); } diff --git a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems index d9f59f77..c62669ca 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems @@ -63,6 +63,7 @@ + \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/RO.cs b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/RO.cs new file mode 100644 index 00000000..1e972113 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/RO.cs @@ -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 shared/winerror.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.Windows; + +internal static partial class RO +{ + [NativeTypeName("#define RO_E_CLOSED _HRESULT_TYPEDEF_(0x80000013L)")] + public const int RO_E_CLOSED = unchecked((int)(0x80000013)); +} \ No newline at end of file