From 8c59086ee765d44f0f5193d1722ab2272ae6fbc0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Aug 2022 03:45:25 +0200 Subject: [PATCH] Add RCW support to D2D1ResourceTextureManager --- .../Interop/D2D1ResourceTextureManager.cs | 293 +++++++++++++++++- ...ManagerImpl.ID2D1ResourceTextureManager.cs | 226 ++++++++------ .../D2D1ResourceTextureManagerImpl.cs | 4 +- 3 files changed, 414 insertions(+), 109 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs index ba84df52..25991c82 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs @@ -2,6 +2,7 @@ using System.Runtime.InteropServices; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Interop; @@ -41,11 +42,22 @@ namespace ComputeSharp.D2D1.Interop; /// These can be used to create resource textures, pass them to effects, and also share resources among different effects. /// /// +/// For details of how these work, Initialize maps to ID2D1EffectContext::CreateResourceTexture, whereas +/// Update maps to ID2D1ResourceTexture::Update. The same behavior and parameters as those methods is used. +/// +/// /// This interface is implemented by ComputeSharp.D2D1, and it can be used through the APIs in . -/// That is, is first use to create an ID2D1ResourceTextureManager instance. Then, -/// can be used to initialize the resource texture held by the manager. The manager can also be -/// assigned to an effect at any time, using the available property indices from . -/// The API can also be used to update texture data after the initial creation. +/// That is, is first use to create an ID2D1ResourceTextureManager instance. Then, +/// can be used to initialize the resource texture held by the manager. The manager can also be assigned to an effect at any time, +/// using the available property indices from . To update texture data after its initial +/// creation, can be used. +/// +/// +/// An RCW (runtime callable wrapper, see ), +/// is also available for all of these APIs, implemented by the same . In this case, the constructor can +/// be used to create an initialize an instance (equivalent to calling and , and then +/// is available for texture data updates. +/// The instance implements , which can be used in advanced scenarios to retrieve the underlying COM object. /// /// /// The ID2D1ResourceTextureManager contract allows callers to create a resource texture at any time even before the effect @@ -65,9 +77,270 @@ namespace ComputeSharp.D2D1.Interop; /// }; /// /// +/// +/// Note that due to the fact ID2D1EffectContext and ID2D1ResourceTexture are not thread safe, they are only +/// safe to use without additional synchronization when SetEffectContext and GetResourceTexture are called. +/// This happens from the internal ID2D1EffectImpl object, which is invoked by D2D, which will handle taking the +/// necessary lock before invoking these APIs. Due to the fact the context has to be stored internally, custom implementations +/// have to make sure to retrieve the right ID2D1Multithread instance from the input context and use that to synchronize +/// all accesses to those two objects from all APIs (including those exposed by ID2D1ResourceTextureManager). The +/// built-in implementation of these two interfaces provided by takes care of all this. +/// /// -public static unsafe class D2D1ResourceTextureManager +public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface { + /// + /// The object wrapped by the current instance. + /// + private readonly D2D1ResourceTextureManagerImpl* d2D1ResourceManagerImpl; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The extents of the resource to create. + /// The buffer precision for the resource to create. + /// The channel depth for the resource to create. + /// The filter for the resource to create. + /// The extend modes for the resource to create. + /// Thrown if creating or initializing the instance failed. + public D2D1ResourceTextureManager( + ReadOnlySpan extents, + D2D1BufferPrecision bufferPrecision, + D2D1ChannelDepth channelDepth, + D2D1Filter filter, + ReadOnlySpan extendModes) + { + fixed (D2D1ResourceTextureManagerImpl** d2D1ResourceManagerImpl = &this.d2D1ResourceManagerImpl) + { + D2D1ResourceTextureManagerImpl.Factory(d2D1ResourceManagerImpl); + } + + fixed (uint* pExtents = extents) + fixed (D2D1ExtendMode* pExtendModes = extendModes) + { + D2D1_RESOURCE_TEXTURE_PROPERTIES d2D1ResourceTextureProperties; + d2D1ResourceTextureProperties.extents = pExtents; + d2D1ResourceTextureProperties.dimensions = (uint)extents.Length; + d2D1ResourceTextureProperties.bufferPrecision = (D2D1_BUFFER_PRECISION)bufferPrecision; + d2D1ResourceTextureProperties.channelDepth = (D2D1_CHANNEL_DEPTH)channelDepth; + d2D1ResourceTextureProperties.filter = (D2D1_FILTER)filter; + d2D1ResourceTextureProperties.extendModes = (D2D1_EXTEND_MODE*)pExtendModes; + + int hresult = D2D1ResourceTextureManagerImpl.Initialize( + @this: this.d2D1ResourceManagerImpl, + resourceId: null, + resourceTextureProperties: &d2D1ResourceTextureProperties, + data: null, + strides: null, + dataSize: 0); + + Marshal.ThrowExceptionForHR(hresult); + } + } + + /// + /// Creates a new instance with the specified parameters. + /// + /// The resource id for the resource to create. + /// The extents of the resource to create. + /// The buffer precision for the resource to create. + /// The channel depth for the resource to create. + /// The filter for the resource to create. + /// The extend modes for the resource to create. + /// Thrown if creating or initializing the instance failed. + public D2D1ResourceTextureManager( + Guid resourceId, + ReadOnlySpan extents, + D2D1BufferPrecision bufferPrecision, + D2D1ChannelDepth channelDepth, + D2D1Filter filter, + ReadOnlySpan extendModes) + { + fixed (D2D1ResourceTextureManagerImpl** d2D1ResourceManagerImpl = &this.d2D1ResourceManagerImpl) + { + D2D1ResourceTextureManagerImpl.Factory(d2D1ResourceManagerImpl); + } + + fixed (uint* pExtents = extents) + fixed (D2D1ExtendMode* pExtendModes = extendModes) + { + D2D1_RESOURCE_TEXTURE_PROPERTIES d2D1ResourceTextureProperties; + d2D1ResourceTextureProperties.extents = pExtents; + d2D1ResourceTextureProperties.dimensions = (uint)extents.Length; + d2D1ResourceTextureProperties.bufferPrecision = (D2D1_BUFFER_PRECISION)bufferPrecision; + d2D1ResourceTextureProperties.channelDepth = (D2D1_CHANNEL_DEPTH)channelDepth; + d2D1ResourceTextureProperties.filter = (D2D1_FILTER)filter; + d2D1ResourceTextureProperties.extendModes = (D2D1_EXTEND_MODE*)pExtendModes; + + int hresult = D2D1ResourceTextureManagerImpl.Initialize( + @this: this.d2D1ResourceManagerImpl, + resourceId: &resourceId, + resourceTextureProperties: &d2D1ResourceTextureProperties, + data: null, + strides: null, + dataSize: 0); + + Marshal.ThrowExceptionForHR(hresult); + } + } + + /// + /// Creates a new instance with the specified parameters. + /// + /// The extents of the resource to create. + /// The buffer precision for the resource to create. + /// The channel depth for the resource to create. + /// The filter for the resource to create. + /// The extend modes for the resource to create. + /// The data to load in the resource to create. + /// The strides for the data supplied for the resource to create. + /// Thrown if creating or initializing the instance failed. + public D2D1ResourceTextureManager( + ReadOnlySpan extents, + D2D1BufferPrecision bufferPrecision, + D2D1ChannelDepth channelDepth, + D2D1Filter filter, + ReadOnlySpan extendModes, + ReadOnlySpan data, + ReadOnlySpan strides) + { + fixed (D2D1ResourceTextureManagerImpl** d2D1ResourceManagerImpl = &this.d2D1ResourceManagerImpl) + { + D2D1ResourceTextureManagerImpl.Factory(d2D1ResourceManagerImpl); + } + + fixed (uint* pExtents = extents) + fixed (D2D1ExtendMode* pExtendModes = extendModes) + fixed (byte* pData = data) + fixed (uint* pStrides = strides) + { + D2D1_RESOURCE_TEXTURE_PROPERTIES d2D1ResourceTextureProperties; + d2D1ResourceTextureProperties.extents = pExtents; + d2D1ResourceTextureProperties.dimensions = (uint)extents.Length; + d2D1ResourceTextureProperties.bufferPrecision = (D2D1_BUFFER_PRECISION)bufferPrecision; + d2D1ResourceTextureProperties.channelDepth = (D2D1_CHANNEL_DEPTH)channelDepth; + d2D1ResourceTextureProperties.filter = (D2D1_FILTER)filter; + d2D1ResourceTextureProperties.extendModes = (D2D1_EXTEND_MODE*)pExtendModes; + + int hresult = D2D1ResourceTextureManagerImpl.Initialize( + @this: this.d2D1ResourceManagerImpl, + resourceId: null, + resourceTextureProperties: &d2D1ResourceTextureProperties, + data: pData, + strides: pStrides, + dataSize: (uint)data.Length); + + Marshal.ThrowExceptionForHR(hresult); + } + } + + /// + /// Creates a new instance with the specified parameters. + /// + /// The resource id for the resource to create. + /// The extents of the resource to create. + /// The buffer precision for the resource to create. + /// The channel depth for the resource to create. + /// The filter for the resource to create. + /// The extend modes for the resource to create. + /// The data to load in the resource to create. + /// The strides for the data supplied for the resource to create. + /// Thrown if creating or initializing the instance failed. + public D2D1ResourceTextureManager( + Guid resourceId, + ReadOnlySpan extents, + D2D1BufferPrecision bufferPrecision, + D2D1ChannelDepth channelDepth, + D2D1Filter filter, + ReadOnlySpan extendModes, + ReadOnlySpan data, + ReadOnlySpan strides) + { + fixed (D2D1ResourceTextureManagerImpl** d2D1ResourceManagerImpl = &this.d2D1ResourceManagerImpl) + { + D2D1ResourceTextureManagerImpl.Factory(d2D1ResourceManagerImpl); + } + + fixed (uint* pExtents = extents) + fixed (D2D1ExtendMode* pExtendModes = extendModes) + fixed (byte* pData = data) + fixed (uint* pStrides = strides) + { + D2D1_RESOURCE_TEXTURE_PROPERTIES d2D1ResourceTextureProperties; + d2D1ResourceTextureProperties.extents = pExtents; + d2D1ResourceTextureProperties.dimensions = (uint)extents.Length; + d2D1ResourceTextureProperties.bufferPrecision = (D2D1_BUFFER_PRECISION)bufferPrecision; + d2D1ResourceTextureProperties.channelDepth = (D2D1_CHANNEL_DEPTH)channelDepth; + d2D1ResourceTextureProperties.filter = (D2D1_FILTER)filter; + d2D1ResourceTextureProperties.extendModes = (D2D1_EXTEND_MODE*)pExtendModes; + + int hresult = D2D1ResourceTextureManagerImpl.Initialize( + @this: this.d2D1ResourceManagerImpl, + resourceId: &resourceId, + resourceTextureProperties: &d2D1ResourceTextureProperties, + data: pData, + strides: pStrides, + dataSize: (uint)data.Length); + + Marshal.ThrowExceptionForHR(hresult); + } + } + + /// + /// Releases the underlying ID2D1ResourceTextureManager object. + /// + ~D2D1ResourceTextureManager() + { + this.d2D1ResourceManagerImpl->Release(); + } + + /// + /// Updates the specific resource texture inside the specific range or box using the supplied data. + /// + /// The "left" extent of the updates if specified. If , the entire texture is updated. + /// The "right" extent of the updates if specified. If , the entire texture is updated. + /// The stride to advance through the input data, according to dimension. + /// The data to be placed into the resource texture. + public void Update( + ReadOnlySpan minimumExtents, + ReadOnlySpan maximimumExtents, + ReadOnlySpan strides, + ReadOnlySpan data) + { + fixed (uint* pMinimumExtents = minimumExtents) + fixed (uint* pMaximumExtents = maximimumExtents) + fixed (uint* pStrides = strides) + fixed (byte* pData = data) + { + int hresult = D2D1ResourceTextureManagerImpl.Update( + @this: this.d2D1ResourceManagerImpl, + minimumExtents: pMinimumExtents, + maximimumExtents: pMaximumExtents, + strides: pStrides, + dimensions: (uint)(strides.Length + 1), + data: pData, + dataCount: (uint)data.Length); + + Marshal.ThrowExceptionForHR(hresult); + } + } + + /// + CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv) + { + fixed (Guid* pIid = &iid) + fixed (IntPtr* pPpv = &ppv) + { + int hresult = this.d2D1ResourceManagerImpl->QueryInterface(pIid, (void**)pPpv); + + return hresult switch + { + S.S_OK => CustomQueryInterfaceResult.Handled, + _ => CustomQueryInterfaceResult.Failed + }; + } + } + /// /// Creates a new ID2D1ResourceTextureManager instance. /// @@ -104,8 +377,6 @@ public static unsafe class D2D1ResourceTextureManager ReadOnlySpan data, ReadOnlySpan strides) { - // TODO: validate argument and return - fixed (uint* pExtents = extents) fixed (D2D1ExtendMode* pExtendModes = extendModes) fixed (byte* pData = data) @@ -136,19 +407,15 @@ public static unsafe class D2D1ResourceTextureManager /// A pointer to the ID2D1ResourceTextureManager instance to use. /// The "left" extent of the updates if specified (if empty, the entire texture is updated). /// The "right" extent of the updates if specified (if empty, the entire texture is updated). - /// The stride to advance through the input data, according to dimension. - /// The number of dimensions in the resource texture. This must match the number used to load the texture. + /// The stride to advance through the input data. /// The data to be placed into the resource texture. public static void Update( void* resourceTextureManager, ReadOnlySpan minimumExtents, ReadOnlySpan maximimumExtents, ReadOnlySpan strides, - uint dimensions, ReadOnlySpan data) { - // TODO: validate argument and return - fixed (uint* pMinimumExtents = minimumExtents) fixed (uint* pMaximumExtents = maximimumExtents) fixed (uint* pStrides = strides) @@ -158,7 +425,7 @@ public static unsafe class D2D1ResourceTextureManager minimumExtents: pMinimumExtents, maximimumExtents: pMaximumExtents, strides: pStrides, - dimensions: dimensions, + dimensions: (uint)(strides.Length + 1), data: pData, dataCount: (uint)data.Length); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs index 3864e305..a69e85a6 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs @@ -9,12 +9,125 @@ using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.In namespace ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; /// -partial struct D2D1ResourceTextureManagerImpl +unsafe partial struct D2D1ResourceTextureManagerImpl { + /// + public static int Initialize( + D2D1ResourceTextureManagerImpl* @this, + Guid* resourceId, + D2D1_RESOURCE_TEXTURE_PROPERTIES* resourceTextureProperties, + byte* data, + uint* strides, + uint dataSize) + { + // Return E_POINTER if any input pointer is null + if (resourceTextureProperties is null || + data is null || + resourceTextureProperties->extents is null || + resourceTextureProperties->extendModes is null) + { + return E.E_POINTER; + } + + lock (@this->lockHandle.Target!) + { + // If the method has already been called, fail + if (@this->d2D1ResourceTexture is not null || + @this->data is not null) + { + return E.E_NOT_VALID_STATE; + } + + // If the method has already been called, just forward the call + if (@this->d2D1EffectContext is not null) + { + @this->d2D1Multithread->Enter(); + + // Create the resource after taking a D2D lock + int hresult = @this->d2D1EffectContext->CreateResourceTexture( + resourceId: resourceId, + resourceTextureProperties: resourceTextureProperties, + data: data, + strides: strides, + dataSize: dataSize, + resourceTexture: &@this->d2D1ResourceTexture); + + @this->d2D1Multithread->Leave(); + + return hresult; + } + + // Initialize into a staging buffer + return ID2D1ResourceTextureManagerMethods.InitializeWithStagingBuffer( + @this: @this, + resourceId: resourceId, + resourceTextureProperties: resourceTextureProperties, + data: data, + strides: strides, + dataSize: dataSize); + } + } + + /// + public static int Update( + D2D1ResourceTextureManagerImpl* @this, + uint* minimumExtents, + uint* maximimumExtents, + uint* strides, + uint dimensions, + byte* data, + uint dataCount) + { + if (minimumExtents is null || + maximimumExtents is null || + data is null) + { + return E.E_POINTER; + } + + lock (@this->lockHandle.Target!) + { + if (@this->d2D1ResourceTexture is null && + @this->data is null) + { + return E.E_NOT_VALID_STATE; + } + + // If a resource texture already exists, just forward the call + if (@this->d2D1ResourceTexture is not null) + { + @this->d2D1Multithread->Enter(); + + // Take a D2D lock here too to ensure thread safety + int hresult = @this->d2D1ResourceTexture->Update( + minimumExtents: minimumExtents, + maximimumExtents: maximimumExtents, + strides: strides, + dimensions: dimensions, + data: data, + dataCount: dataCount); + + @this->d2D1Multithread->Leave(); + + return hresult; + } + + // Otherwise update the staging buffer + return ID2D1ResourceTextureManagerMethods.UpdateWithStagingBuffer( + @this: @this, + minimumExtents: minimumExtents, + maximimumExtents: maximimumExtents, + strides: strides, + dimensions: dimensions, + data: data, + dataCount: dataCount); + } + } + /// /// The implementation for ID2D1ResourceTextureManager. /// - private static unsafe class ID2D1ResourceTextureManagerMethods + private static class ID2D1ResourceTextureManagerMethods { #if !NET6_0_OR_GREATER /// @@ -95,52 +208,13 @@ partial struct D2D1ResourceTextureManagerImpl uint* strides, uint dataSize) { - // Return E_POINTER if any input pointer is null - if (resourceTextureProperties is null || - data is null || - resourceTextureProperties->extents is null || - resourceTextureProperties->extendModes is null) - { - return E.E_POINTER; - } - - lock (@this->lockHandle.Target!) - { - // If the method has already been called, fail - if (@this->d2D1ResourceTexture is not null || - @this->data is not null) - { - return E.E_NOT_VALID_STATE; - } - - // If the method has already been called, just forward the call - if (@this->d2D1EffectContext is not null) - { - @this->d2D1Multithread->Enter(); - - // Create the resource after taking a D2D lock - int hresult = @this->d2D1EffectContext->CreateResourceTexture( - resourceId: resourceId, - resourceTextureProperties: resourceTextureProperties, - data: data, - strides: strides, - dataSize: dataSize, - resourceTexture: &@this->d2D1ResourceTexture); - - @this->d2D1Multithread->Leave(); - - return hresult; - } - - // Initialize into a staging buffer - return InitializeWithStagingBuffer( - @this: @this, - resourceId: resourceId, - resourceTextureProperties: resourceTextureProperties, - data: data, - strides: strides, - dataSize: dataSize); - } + return D2D1ResourceTextureManagerImpl.Initialize( + @this, + resourceId, + resourceTextureProperties, + data, + strides, + dataSize); } /// @@ -154,54 +228,18 @@ partial struct D2D1ResourceTextureManagerImpl byte* data, uint dataCount) { - if (minimumExtents is null || - maximimumExtents is null || - data is null) - { - return E.E_POINTER; - } - - lock (@this->lockHandle.Target!) - { - if (@this->d2D1ResourceTexture is null && - @this->data is null) - { - return E.E_NOT_VALID_STATE; - } - - // If a resource texture already exists, just forward the call - if (@this->d2D1ResourceTexture is not null) - { - @this->d2D1Multithread->Enter(); - - // Take a D2D lock here too to ensure thread safety - int hresult = @this->d2D1ResourceTexture->Update( - minimumExtents: minimumExtents, - maximimumExtents: maximimumExtents, - strides: strides, - dimensions: dimensions, - data: data, - dataCount: dataCount); - - @this->d2D1Multithread->Leave(); - - return hresult; - } - - // Otherwise update the staging buffer - return UpdateWithStagingBuffer( - @this: @this, - minimumExtents: minimumExtents, - maximimumExtents: maximimumExtents, - strides: strides, - dimensions: dimensions, - data: data, - dataCount: dataCount); - } + return D2D1ResourceTextureManagerImpl.Update( + @this, + minimumExtents, + maximimumExtents, + strides, + dimensions, + data, + dataCount); } /// - private static int InitializeWithStagingBuffer( + public static int InitializeWithStagingBuffer( D2D1ResourceTextureManagerImpl* @this, Guid* resourceId, D2D1_RESOURCE_TEXTURE_PROPERTIES* resourceTextureProperties, @@ -308,7 +346,7 @@ partial struct D2D1ResourceTextureManagerImpl } /// - private static int UpdateWithStagingBuffer( + public static int UpdateWithStagingBuffer( D2D1ResourceTextureManagerImpl* @this, uint* minimumExtents, uint* maximimumExtents, diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs index 90af4798..df88f646 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs @@ -173,7 +173,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl } /// - private int QueryInterface(Guid* riid, void** ppvObject) + public int QueryInterface(Guid* riid, void** ppvObject) { if (ppvObject is null) { @@ -213,7 +213,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl } /// - private uint Release() + public uint Release() { uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount);