From 56f07203dc47bce66b72c3653b8df86180cf603e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Nov 2023 02:27:37 +0100 Subject: [PATCH] Use [InlineArray] in ResourceTextureManagerBuffer --- ...ixelShaderEffect.ID2D1EffectImplMethods.cs | 2 +- .../Effects/PixelShaderEffect.Properties.cs | 11 +-- ...aderEffect.ResourceTextureManagerBuffer.cs | 99 +------------------ .../Interop/Effects/PixelShaderEffect.cs | 9 +- 4 files changed, 13 insertions(+), 108 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index 2eb6e2b5..d60fca99 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -108,7 +108,7 @@ internal unsafe partial struct PixelShaderEffect for (int i = 0; i < resourceTextureDescriptions.Length; i++) { - using ComPtr resourceTextureManager = new(@this->resourceTextureManagerBuffer[i]); + ref readonly ComPtr resourceTextureManager = ref @this->resourceTextureManagerBuffer[i]; // If the current resource texture manager is not set, we cannot render, as there's an unbound resource texture if (resourceTextureManager.Get() is null) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index bf214d23..733d9f57 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -180,9 +180,7 @@ unsafe partial struct PixelShaderEffect default(ArgumentOutOfRangeException).ThrowIfLessThan((int)dataSize, sizeof(void*), nameof(dataSize)); default(ArgumentOutOfRangeException).ThrowIfGreaterThanOrEqual(index, GetGlobals().ResourceTextureCount); - using ComPtr resourceTextureManager = new(this.resourceTextureManagerBuffer[index]); - - resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data).Assert(); + this.resourceTextureManagerBuffer[index].CopyTo((ID2D1ResourceTextureManager**)data).Assert(); if (actualSize is not null) { @@ -240,13 +238,8 @@ unsafe partial struct PixelShaderEffect resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions).Assert(); } - ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index]; - - // If there's already an existing manager at this index, release it - ComPtr.Dispose(currentResourceTextureManager); - // Store the resource texture manager into the buffer - currentResourceTextureManager = resourceTextureManager.Detach(); + this.resourceTextureManagerBuffer[index].Attach(resourceTextureManager.Detach()); return S.S_OK; } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs index 66715d60..1525ac58 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ResourceTextureManagerBuffer.cs @@ -1,7 +1,8 @@ using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; +using ComputeSharp.Win32; -#pragma warning disable CS0649 +#pragma warning disable IDE0051 namespace ComputeSharp.D2D1.Interop.Effects; @@ -11,102 +12,12 @@ partial struct PixelShaderEffect /// /// A buffer of 16 objects. /// + [InlineArray(16)] private unsafe struct ResourceTextureManagerBuffer { /// - /// The pointer to the instance at index 0. + /// The instance at index 0. /// - public ID2D1ResourceTextureManager* ResourceTextureManager0; - - /// - /// The pointer to the instance at index 1. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager1; - - /// - /// The pointer to the instance at index 2. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager2; - - /// - /// The pointer to the instance at index 3. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager3; - - /// - /// The pointer to the instance at index 4. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager4; - - /// - /// The pointer to the instance at index 5. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager5; - - /// - /// The pointer to the instance at index 6. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager6; - - /// - /// The pointer to the instance at index 7. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager7; - - /// - /// The pointer to the instance at index 8. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager8; - - /// - /// The pointer to the instance at index 9. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager9; - - /// - /// The pointer to the instance at index 10. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager10; - - /// - /// The pointer to the instance at index 11. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager11; - - /// - /// The pointer to the instance at index 12. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager12; - - /// - /// The pointer to the instance at index 13. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager13; - - /// - /// The pointer to the instance at index 14. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager14; - - /// - /// The pointer to the instance at index 15. - /// - public ID2D1ResourceTextureManager* ResourceTextureManager15; - - /// - /// Gets a to the pointer at a given index. - /// - /// The index of the instance to retrieve. - /// A to the pointer at a given index. - public ref ID2D1ResourceTextureManager* this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - ID2D1ResourceTextureManager** @this = (ID2D1ResourceTextureManager**)Unsafe.AsPointer(ref this); - - return ref @this[index]; - } - } + private ComPtr resourceTextureManager0; } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 3c411ee8..5cbc4ca6 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using ComputeSharp.Win32; @@ -201,11 +202,11 @@ internal unsafe partial struct PixelShaderEffect ComPtr.Dispose(this.d2D1EffectContext); // Retrieve all possible resource texture managers in use and release the ones that had been - // assigned (from one of the property bindings). We just hardcode 16 here and dont access - // the globals, as technically invoking APIs on it might throw an exception. - for (int i = 0; i < 16; i++) + // assigned (from one of the property bindings). We just try to dispose all of them here and + // don't access the globals, as technically invoking APIs on it might throw an exception. + foreach (ref ComPtr resourceTextureManager in (Span>)this.resourceTextureManagerBuffer) { - ComPtr.Dispose(this.resourceTextureManagerBuffer[i]); + resourceTextureManager.Dispose(); } NativeMemory.Free(Unsafe.AsPointer(ref this));