From ced3fdc78b66978ae1da3b75175fb0fe43c23329 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Aug 2022 16:17:49 +0200 Subject: [PATCH] Make resource texture manager thread safe --- ...tureManager.ID2D1ResourceTextureManager.cs | 221 ++++++++++++------ ...ger.ID2D1ResourceTextureManagerInternal.cs | 118 +++++----- .../ResourceTextureManager.cs | 73 +++--- 3 files changed, 251 insertions(+), 161 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs index dfc428fe..f48dd4ed 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManager.cs @@ -104,73 +104,36 @@ partial struct ResourceTextureManager return E.E_POINTER; } - // If the method has already been called, fail - if (@this->d2D1ResourceTexture is not null || - @this->data is not null) + lock (@this->lockHandle.Target!) { - return E.E_NOT_VALID_STATE; - } + // 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 Initialize has already been called, just forward the call - if (@this->d2D1EffectContext is not null) - { - return @this->d2D1EffectContext->CreateResourceTexture( + // If the method has already been called, just forward the call + if (@this->d2D1EffectContext is not null) + { + return @this->d2D1EffectContext->CreateResourceTexture( + resourceId: resourceId, + resourceTextureProperties: resourceTextureProperties, + data: data, + strides: strides, + dataSize: dataSize, + resourceTexture: &@this->d2D1ResourceTexture); + } + + // Initialize into a staging buffer + return InitializeWithStagingBuffer( + @this: @this, resourceId: resourceId, resourceTextureProperties: resourceTextureProperties, data: data, strides: strides, - dataSize: dataSize, - resourceTexture: &@this->d2D1ResourceTexture); + dataSize: dataSize); } - - @this->resourceId = (Guid*)NativeMemory.Alloc((nuint)sizeof(Guid)); - - *@this->resourceId = *resourceId; - - uint* extents = (uint*)NativeMemory.Alloc(sizeof(uint) * resourceTextureProperties->dimensions); - D2D1_EXTEND_MODE* extendModes = (D2D1_EXTEND_MODE*)NativeMemory.Alloc(sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions); - - Buffer.MemoryCopy( - source: resourceTextureProperties->extents, - destination: extents, - destinationSizeInBytes: sizeof(uint) * resourceTextureProperties->dimensions, - sourceBytesToCopy: sizeof(uint) * resourceTextureProperties->dimensions); - - Buffer.MemoryCopy( - source: resourceTextureProperties->extendModes, - destination: extendModes, - destinationSizeInBytes: sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions, - sourceBytesToCopy: sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions); - - @this->resourceTextureProperties.extents = extents; - @this->resourceTextureProperties.dimensions = resourceTextureProperties->dimensions; - @this->resourceTextureProperties.bufferPrecision = resourceTextureProperties->bufferPrecision; - @this->resourceTextureProperties.channelDepth = resourceTextureProperties->channelDepth; - @this->resourceTextureProperties.filter = resourceTextureProperties->filter; - @this->resourceTextureProperties.extendModes = extendModes; - - @this->data = (byte*)NativeMemory.Alloc(dataSize); - - Buffer.MemoryCopy(data, @this->data, dataSize, dataSize); - - @this->dataSize = dataSize; - - if (strides is not null) - { - @this->strides = (uint*)NativeMemory.Alloc(sizeof(uint) * (resourceTextureProperties->dimensions - 1)); - - Buffer.MemoryCopy( - source: strides, - destination: @this->strides, - destinationSizeInBytes: sizeof(uint) * (resourceTextureProperties->dimensions - 1), - sourceBytesToCopy: sizeof(uint) * (resourceTextureProperties->dimensions - 1)); - } - else - { - @this->strides = null; - } - - return 0; } /// @@ -191,27 +154,137 @@ partial struct ResourceTextureManager return E.E_POINTER; } - if (@this->d2D1ResourceTexture is null && - @this->data is null) + lock (@this->lockHandle.Target!) { - return E.E_NOT_VALID_STATE; + 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) + { + return @this->d2D1ResourceTexture->Update( + minimumExtents: minimumExtents, + maximimumExtents: maximumExtents, + strides: strides, + dimensions: dimensions, + data: data, + dataCount: dataCount); + } + + // TODO: implement Update over buffered data + + return 0; + } + } + + /// + private static int InitializeWithStagingBuffer( + ResourceTextureManager* @this, + Guid* resourceId, + D2D1_RESOURCE_TEXTURE_PROPERTIES* resourceTextureProperties, + byte* data, + uint* strides, + uint dataSize) + { + // Allocate a buffer for the resource id, if there is one + if (resourceId is not null) + { + try + { + @this->resourceId = (Guid*)NativeMemory.Alloc((nuint)sizeof(Guid)); + } + catch (OutOfMemoryException) + { + return E.E_OUTOFMEMORY; + } + + *@this->resourceId = *resourceId; } - // If a resource texture already exists, just forward the call - if (@this->d2D1ResourceTexture is not null) + uint* extents; + + // Allocate a buffer for the extents (one per dimension) + try { - return @this->d2D1ResourceTexture->Update( - minimumExtents: minimumExtents, - maximimumExtents: maximumExtents, - strides: strides, - dimensions: dimensions, - data: data, - dataCount: dataCount); + extents = (uint*)NativeMemory.Alloc(sizeof(uint) * resourceTextureProperties->dimensions); + } + catch (OutOfMemoryException) + { + return E.E_OUTOFMEMORY; } - // TODO: implement Update over buffered data + D2D1_EXTEND_MODE* extendModes; - return 0; + // Allocate a buffer for the extend modes (one per dimension) + try + { + extendModes = (D2D1_EXTEND_MODE*)NativeMemory.Alloc(sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions); + } + catch (OutOfMemoryException) + { + return E.E_OUTOFMEMORY; + } + + // Copy the extents into the buffer + Buffer.MemoryCopy( + source: resourceTextureProperties->extents, + destination: extents, + destinationSizeInBytes: sizeof(uint) * resourceTextureProperties->dimensions, + sourceBytesToCopy: sizeof(uint) * resourceTextureProperties->dimensions); + + // Copy the extend modes into the buffer + Buffer.MemoryCopy( + source: resourceTextureProperties->extendModes, + destination: extendModes, + destinationSizeInBytes: sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions, + sourceBytesToCopy: sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions); + + // Initialize the other resource texture properties + @this->resourceTextureProperties.extents = extents; + @this->resourceTextureProperties.dimensions = resourceTextureProperties->dimensions; + @this->resourceTextureProperties.bufferPrecision = resourceTextureProperties->bufferPrecision; + @this->resourceTextureProperties.channelDepth = resourceTextureProperties->channelDepth; + @this->resourceTextureProperties.filter = resourceTextureProperties->filter; + @this->resourceTextureProperties.extendModes = extendModes; + + // Allocate the data buffer (this is the one most likely to potentially throw) + try + { + @this->data = (byte*)NativeMemory.Alloc(dataSize); + } + catch (OutOfMemoryException) + { + return E.E_OUTOFMEMORY; + } + + // Copy the data buffer + Buffer.MemoryCopy(data, @this->data, dataSize, dataSize); + + @this->dataSize = dataSize; + + // If there are strides (ie. the texture has dimension greater than 1), initialize them as well + if (strides is not null) + { + try + { + @this->strides = (uint*)NativeMemory.Alloc(sizeof(uint) * (resourceTextureProperties->dimensions - 1)); + } + catch (OutOfMemoryException) + { + return E.E_OUTOFMEMORY; + } + + Buffer.MemoryCopy( + source: strides, + destination: @this->strides, + destinationSizeInBytes: sizeof(uint) * (resourceTextureProperties->dimensions - 1), + sourceBytesToCopy: sizeof(uint) * (resourceTextureProperties->dimensions - 1)); + } + + return S.S_OK; } } } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs index e7f1a0cf..0354601a 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs @@ -89,20 +89,23 @@ partial struct ResourceTextureManager return E.E_POINTER; } - // If this is the first time the method is called, just store the context - if (@this->d2D1EffectContext is null) + lock (@this->lockHandle.Target!) { - effectContext->AddRef(); + // If this is the first time the method is called, just store the context + if (@this->d2D1EffectContext is null) + { + effectContext->AddRef(); - @this->d2D1EffectContext = effectContext; + @this->d2D1EffectContext = effectContext; - return S.S_OK; + return S.S_OK; + } + + // Otherwise, just do nothing and return S_FALSE. This allows an existing resource texture + // manager to be shared across effects. If the resource cannot be shared, it will just + // return an error when effect is actually set, with ID2D1DrawInfo::SetResourceTexture. + return S.S_FALSE; } - - // Otherwise, just do nothing and return S_FALSE. This allows an existing resource texture - // manager to be shared across effects. If the resource cannot be shared, it will just - // return an error when effect is actually set, with ID2D1DrawInfo::SetResourceTexture. - return S.S_FALSE; } /// @@ -116,54 +119,57 @@ partial struct ResourceTextureManager return E.E_POINTER; } - // If the effect context is null, it means Initialize has not been called yet - if (@this->d2D1EffectContext is null) + lock (@this->lockHandle.Target!) { - return E.E_NOT_VALID_STATE; + // If the effect context is null, it means Initialize has not been called yet + if (@this->d2D1EffectContext is null) + { + return E.E_NOT_VALID_STATE; + } + + // If the texture has already been created, just return it + if (@this->d2D1ResourceTexture is not null) + { + @this->d2D1ResourceTexture->AddRef(); + + *resourceTexture = @this->d2D1ResourceTexture; + + return S.S_OK; + } + + // If the data is null at this point, it means CreateResourceTexture has not been called yet + if (@this->data is null) + { + return E.E_NOT_VALID_STATE; + } + + // Create the resource now, as it hasn't been created yet + int result = @this->d2D1EffectContext->CreateResourceTexture( + resourceId: @this->resourceId, + resourceTextureProperties: &@this->resourceTextureProperties, + data: @this->data, + strides: @this->strides, + dataSize: @this->dataSize, + resourceTexture: resourceTexture); + + // If creation was successful, release the buffered data. Going forwards, + // the resource texture will be used directly for all updates requested. + if (result == S.S_OK) + { + NativeMemory.Free(@this->resourceTextureProperties.extents); + NativeMemory.Free(@this->resourceTextureProperties.extendModes); + NativeMemory.Free(@this->data); + NativeMemory.Free(@this->strides); + + // Reset the stored pointers to avoid double frees from Release() + @this->resourceTextureProperties.extents = null; + @this->resourceTextureProperties.extendModes = null; + @this->data = null; + @this->strides = null; + } + + return result; } - - // If the texture has already been created, just return it - if (@this->d2D1ResourceTexture is not null) - { - @this->d2D1ResourceTexture->AddRef(); - - *resourceTexture = @this->d2D1ResourceTexture; - - return S.S_OK; - } - - // If the data is null at this point, it means CreateResourceTexture has not been called yet - if (@this->data is null) - { - return E.E_NOT_VALID_STATE; - } - - // Create the resource now, as it hasn't been created yet - int result = @this->d2D1EffectContext->CreateResourceTexture( - resourceId: @this->resourceId, - resourceTextureProperties: &@this->resourceTextureProperties, - data: @this->data, - strides: @this->strides, - dataSize: @this->dataSize, - resourceTexture: resourceTexture); - - // If creation was successful, release the buffered data. Going forwards, - // the resource texture will be used directly for all updates requested. - if (result == S.S_OK) - { - NativeMemory.Free(@this->resourceTextureProperties.extents); - NativeMemory.Free(@this->resourceTextureProperties.extendModes); - NativeMemory.Free(@this->data); - NativeMemory.Free(@this->strides); - - // Reset the stored pointers to avoid double frees from Release() - @this->resourceTextureProperties.extents = null; - @this->resourceTextureProperties.extendModes = null; - @this->data = null; - @this->strides = null; - } - - return result; } } } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs index 83e79c75..989024a3 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.cs @@ -136,6 +136,11 @@ internal unsafe partial struct ResourceTextureManager /// private uint dataSize; + /// + /// A to a dummy to use for locking. + /// + private GCHandle lockHandle; + /// /// The factory method for instances. /// @@ -156,6 +161,7 @@ internal unsafe partial struct ResourceTextureManager @this->data = null; @this->strides = null; @this->dataSize = 0; + @this->lockHandle = GCHandle.Alloc(new object()); *resourceTextureManager = @this; } @@ -207,40 +213,45 @@ internal unsafe partial struct ResourceTextureManager if (referenceCount == 0) { - if (this.d2D1EffectContext is not null) + lock (this.lockHandle.Target!) { - this.d2D1EffectContext->Release(); + if (this.d2D1EffectContext is not null) + { + this.d2D1EffectContext->Release(); + } + + if (this.d2D1ResourceTexture is not null) + { + this.d2D1ResourceTexture->Release(); + } + + if (this.resourceId is not null) + { + NativeMemory.Free(this.resourceId); + } + + if (this.resourceTextureProperties.extents is not null) + { + NativeMemory.Free(this.resourceTextureProperties.extents); + } + + if (this.resourceTextureProperties.extendModes is not null) + { + NativeMemory.Free(this.resourceTextureProperties.extendModes); + } + + if (this.data is not null) + { + NativeMemory.Free(this.data); + } + + if (this.strides is not null) + { + NativeMemory.Free(this.strides); + } } - if (this.d2D1ResourceTexture is not null) - { - this.d2D1ResourceTexture->Release(); - } - - if (this.resourceId is not null) - { - NativeMemory.Free(this.resourceId); - } - - if (this.resourceTextureProperties.extents is not null) - { - NativeMemory.Free(this.resourceTextureProperties.extents); - } - - if (this.resourceTextureProperties.extendModes is not null) - { - NativeMemory.Free(this.resourceTextureProperties.extendModes); - } - - if (this.data is not null) - { - NativeMemory.Free(this.data); - } - - if (this.strides is not null) - { - NativeMemory.Free(this.strides); - } + this.lockHandle.Free(); } return referenceCount;