From 56f07203dc47bce66b72c3653b8df86180cf603e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Nov 2023 02:27:37 +0100 Subject: [PATCH 1/3] 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)); From fd283ac37f60d192026c55c7b9b0639f6024e6e1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Nov 2023 02:31:17 +0100 Subject: [PATCH 2/3] Use [InlineArray] in ResourceTextureManagerBuffer --- .../Buffers/ResourceTextureManagerBuffer.cs | 103 +----------------- 1 file changed, 4 insertions(+), 99 deletions(-) diff --git a/src/ComputeSharp.D2D1.WinUI/Buffers/ResourceTextureManagerBuffer.cs b/src/ComputeSharp.D2D1.WinUI/Buffers/ResourceTextureManagerBuffer.cs index 66744195..a4a38b55 100644 --- a/src/ComputeSharp.D2D1.WinUI/Buffers/ResourceTextureManagerBuffer.cs +++ b/src/ComputeSharp.D2D1.WinUI/Buffers/ResourceTextureManagerBuffer.cs @@ -1,113 +1,18 @@ -using System; -using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Interop; +#pragma warning disable IDE0044, IDE0051 + namespace ComputeSharp.D2D1.WinUI.Buffers; /// /// A fixed buffer type containing 16 fields. /// +[InlineArray(16)] internal struct ResourceTextureManagerBuffer { /// /// The instance at index 0. /// - public D2D1ResourceTextureManager? ResourceTextureManager0; - - /// - /// The instance at index 1. - /// - public D2D1ResourceTextureManager? ResourceTextureManager1; - - /// - /// The instance at index 2. - /// - public D2D1ResourceTextureManager? ResourceTextureManager2; - - /// - /// The instance at index 3. - /// - public D2D1ResourceTextureManager? ResourceTextureManager3; - - /// - /// The instance at index 4. - /// - public D2D1ResourceTextureManager? ResourceTextureManager4; - - /// - /// The instance at index 5. - /// - public D2D1ResourceTextureManager? ResourceTextureManager5; - - /// - /// The instance at index 6. - /// - public D2D1ResourceTextureManager? ResourceTextureManager6; - - /// - /// The instance at index 7. - /// - public D2D1ResourceTextureManager? ResourceTextureManager7; - - /// - /// The instance at index 8. - /// - public D2D1ResourceTextureManager? ResourceTextureManager8; - - /// - /// The instance at index 9. - /// - public D2D1ResourceTextureManager? ResourceTextureManager9; - - /// - /// The instance at index 10. - /// - public D2D1ResourceTextureManager? ResourceTextureManager10; - - /// - /// The instance at index 11. - /// - public D2D1ResourceTextureManager? ResourceTextureManager11; - - /// - /// The instance at index 12. - /// - public D2D1ResourceTextureManager? ResourceTextureManager12; - - /// - /// The instance at index 13. - /// - public D2D1ResourceTextureManager? ResourceTextureManager13; - - /// - /// The instance at index 14. - /// - public D2D1ResourceTextureManager? ResourceTextureManager14; - - /// - /// The instance at index 15. - /// - public D2D1ResourceTextureManager? ResourceTextureManager15; - - /// - /// Gets a reference to the value at a given index. - /// - /// The index of the value to get a reference to. - /// A reference to the value at a given index. - /// Thrown if is not a valid index for the current effect. - [UnscopedRef] - public ref D2D1ResourceTextureManager? this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16); - - ref D2D1ResourceTextureManager? r0 = ref Unsafe.As(ref this); - ref D2D1ResourceTextureManager? r1 = ref Unsafe.Add(ref r0, index); - - return ref r1; - } - } + private D2D1ResourceTextureManager? resourceTextureManager0; } \ No newline at end of file From fc56e7b99934e5c1a8c88bc357fa6241efc37863 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Nov 2023 02:31:25 +0100 Subject: [PATCH 3/3] Use [InlineArray] in GraphicsEffectSourceBuffer --- .../Buffers/GraphicsEffectSourceBuffer.cs | 103 +----------------- 1 file changed, 4 insertions(+), 99 deletions(-) diff --git a/src/ComputeSharp.D2D1.WinUI/Buffers/GraphicsEffectSourceBuffer.cs b/src/ComputeSharp.D2D1.WinUI/Buffers/GraphicsEffectSourceBuffer.cs index 34502c69..a6db6977 100644 --- a/src/ComputeSharp.D2D1.WinUI/Buffers/GraphicsEffectSourceBuffer.cs +++ b/src/ComputeSharp.D2D1.WinUI/Buffers/GraphicsEffectSourceBuffer.cs @@ -1,113 +1,18 @@ -using System; -using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using Windows.Graphics.Effects; +#pragma warning disable IDE0051 + namespace ComputeSharp.D2D1.WinUI.Buffers; /// /// A fixed buffer type containing 16 fields. /// +[InlineArray(16)] internal struct GraphicsEffectSourceBuffer { /// /// The instance at index 0. /// - public SourceReference Source0; - - /// - /// The instance at index 1. - /// - public SourceReference Source1; - - /// - /// The instance at index 2. - /// - public SourceReference Source2; - - /// - /// The instance at index 3. - /// - public SourceReference Source3; - - /// - /// The instance at index 4. - /// - public SourceReference Source4; - - /// - /// The instance at index 5. - /// - public SourceReference Source5; - - /// - /// The instance at index 6. - /// - public SourceReference Source6; - - /// - /// The instance at index 7. - /// - public SourceReference Source7; - - /// - /// The instance at index 8. - /// - public SourceReference Source8; - - /// - /// The instance at index 9. - /// - public SourceReference Source9; - - /// - /// The instance at index 10. - /// - public SourceReference Source10; - - /// - /// The instance at index 11. - /// - public SourceReference Source11; - - /// - /// The instance at index 12. - /// - public SourceReference Source12; - - /// - /// The instance at index 13. - /// - public SourceReference Source13; - - /// - /// The instance at index 14. - /// - public SourceReference Source14; - - /// - /// The instance at index 15. - /// - public SourceReference Source15; - - /// - /// Gets a reference to the value at a given index. - /// - /// The index of the value to get a reference to. - /// A reference to the value at a given index. - /// Thrown if is not a valid index for the current effect. - [UnscopedRef] - public ref SourceReference this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16); - - ref SourceReference r0 = ref Unsafe.As(ref this); - ref SourceReference r1 = ref Unsafe.Add(ref r0, index); - - return ref r1; - } - } + private SourceReference source0; } \ No newline at end of file