From a3c0d167f8a5cdf9b30320feab7a61406c33e176 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 22 Oct 2023 15:34:46 +0200 Subject: [PATCH] Use collection expressions for stackalloc expressions --- build/Directory.Build.props | 2 +- .../Runners/D2D1ContouredLayersRunner.cs | 6 +- .../D2D1PixelShaderEffectTests.cs | 4 +- .../D2D1ResourceTextureManagerTests.cs | 88 +++++++++---------- .../Effects/BokehBlurEffect.cs | 4 +- tests/ComputeSharp.D2D1.Tests/ShadersTests.cs | 6 +- 6 files changed, 55 insertions(+), 55 deletions(-) diff --git a/build/Directory.Build.props b/build/Directory.Build.props index b760c249..24e29a9f 100644 --- a/build/Directory.Build.props +++ b/build/Directory.Build.props @@ -46,7 +46,7 @@ This is used eg. to replace Unsafe.SizeOf() calls with just sizeof(T). The warning is not necessary since in order to use these APIs the caller already has to be in an unsafe context. --> - $(NoWarn);CS8500;IDE0302 + $(NoWarn);CS8500 diff --git a/samples/ComputeSharp.SwapChain.WinUI/Shaders/Runners/D2D1ContouredLayersRunner.cs b/samples/ComputeSharp.SwapChain.WinUI/Shaders/Runners/D2D1ContouredLayersRunner.cs index 4cd7ab18..54fb3a3c 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Shaders/Runners/D2D1ContouredLayersRunner.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/Shaders/Runners/D2D1ContouredLayersRunner.cs @@ -40,13 +40,13 @@ public sealed class D2D1ContouredLayersEffect : PixelShaderEffect // Create the resource texture manager to use in the shader D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc uint[] { (uint)readBackTexture.Height, (uint)readBackTexture.Width }, + extents: [(uint)readBackTexture.Height, (uint)readBackTexture.Width], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipLinear, - extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror }, + extendModes: [D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror], data: new ReadOnlySpan(dataBuffer, bufferSize), - strides: stackalloc uint[] { (uint)strideInBytes }); + strides: [(uint)strideInBytes]); // Create the new pixel shader effect PixelShaderEffect pixelShaderEffect = new() { ResourceTextureManagers = { [0] = resourceTextureManager } }; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 9df99f1d..f0d927bd 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -69,11 +69,11 @@ public partial class D2D1PixelShaderEffectTests public unsafe void SetResourceTextureManagerForD2D1Effect_RCW_NullD2D1Effect() { D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc uint[] { 64 }, + extents: [64], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinLinearMagMipPoint, - extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Clamp }); + extendModes: [D2D1ExtendMode.Clamp]); D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(null, resourceTextureManager, 0); } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs index 5db7c070..44fdd57b 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ResourceTextureManagerTests.cs @@ -66,11 +66,11 @@ public partial class D2D1ResourceTextureManagerTests public unsafe void VerifyInterfaces_RCW() { D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc uint[] { 64 }, + extents: [64], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinLinearMagMipPoint, - extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Clamp }); + extendModes: [D2D1ExtendMode.Clamp]); using ComPtr unknown = default; using ComPtr resourceTextureManager2 = default; @@ -153,11 +153,11 @@ public partial class D2D1ResourceTextureManagerTests D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), out _); D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc uint[] { 128, 128 }, + extents: [128, 128], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: [], strides: []); @@ -218,13 +218,13 @@ public partial class D2D1ResourceTextureManagerTests D2D1ResourceTextureManager.Initialize( resourceTextureManager: resourceTextureManager.Get(), resourceId: Guid.NewGuid(), - extents: stackalloc[] { (uint)expected.Width, (uint)expected.Height }, + extents: [(uint)expected.Width, (uint)expected.Height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(pixels.Span), - strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width) }); + strides: [(uint)(sizeof(Rgba32) * expected.Width)]); using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height); @@ -277,13 +277,13 @@ public partial class D2D1ResourceTextureManagerTests D2D1ResourceTextureManager.Initialize( resourceTextureManager: resourceTextureManager.Get(), resourceId: Guid.NewGuid(), - extents: stackalloc[] { (uint)expected.Width, (uint)expected.Height }, + extents: [(uint)expected.Width, (uint)expected.Height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(pixels.Span), - strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width) }); + strides: [(uint)(sizeof(Rgba32) * expected.Width)]); for (int i = 0; i < drawCount; i++) { @@ -334,13 +334,13 @@ public partial class D2D1ResourceTextureManagerTests D2D1ResourceTextureManager resourceTextureManager = new( resourceId: Guid.NewGuid(), - extents: stackalloc[] { (uint)expected.Width, (uint)expected.Height }, + extents: [(uint)expected.Width, (uint)expected.Height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(pixels.Span), - strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width) }); + strides: [(uint)(sizeof(Rgba32) * expected.Width)]); using ComPtr unknown = default; @@ -399,24 +399,24 @@ public partial class D2D1ResourceTextureManagerTests D2D1ResourceTextureManager.Initialize( resourceTextureManager: resourceTextureManager0.Get(), resourceId: Guid.NewGuid(), - extents: stackalloc[] { (uint)expected0.Width, (uint)expected0.Height }, + extents: [(uint)expected0.Width, (uint)expected0.Height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(pixels0.Span), - strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected0.Width) }); + strides: [(uint)(sizeof(Rgba32) * expected0.Width)]); D2D1ResourceTextureManager.Initialize( resourceTextureManager: resourceTextureManager1.Get(), resourceId: Guid.NewGuid(), - extents: stackalloc[] { (uint)expected0.Width, (uint)expected0.Height }, + extents: [(uint)expected0.Width, (uint)expected0.Height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(pixels1.Span), - strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected0.Width) }); + strides: [(uint)(sizeof(Rgba32) * expected0.Width)]); using (ComPtr d2D1Effect = default) { @@ -509,13 +509,13 @@ public partial class D2D1ResourceTextureManagerTests D2D1ResourceTextureManager.Initialize( resourceTextureManager: resourceTextureManager.Get(), resourceId: Guid.NewGuid(), - extents: stackalloc[] { (uint)expected.Width, (uint)(expected.Height / 4), 4u }, + extents: [(uint)expected.Width, (uint)(expected.Height / 4), 4u], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(pixels.Span), - strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width), (uint)(sizeof(Rgba32) * expected.Width * (expected.Height / 4)) }); + strides: [(uint)(sizeof(Rgba32) * expected.Width), (uint)(sizeof(Rgba32) * expected.Width * (expected.Height / 4))]); using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height); @@ -581,11 +581,11 @@ public partial class D2D1ResourceTextureManagerTests byte[] texture = new byte[width]; D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc[] { (uint)width }, + extents: [(uint)width], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp], data: texture, strides: null); @@ -594,8 +594,8 @@ public partial class D2D1ResourceTextureManagerTests RandomNumberGenerator.Create().GetBytes(data); resourceTextureManager.Update( - minimumExtents: stackalloc uint[] { (uint)startOffset }, - maximimumExtents: stackalloc uint[] { (uint)(startOffset + updateLength) }, + minimumExtents: [(uint)startOffset], + maximimumExtents: [(uint)(startOffset + updateLength)], strides: [], data: data); @@ -668,22 +668,22 @@ public partial class D2D1ResourceTextureManagerTests byte[] texture = new byte[width * height]; D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc[] { (uint)width, (uint)height }, + extents: [(uint)width, (uint)height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: texture, - strides: stackalloc uint[] { (uint)width }); + strides: [(uint)width]); byte[] data = new byte[updateLengthX * updateLengthY]; RandomNumberGenerator.Create().GetBytes(data); resourceTextureManager.Update( - minimumExtents: stackalloc uint[] { (uint)startOffsetX, (uint)startOffsetY }, - maximimumExtents: stackalloc uint[] { (uint)(startOffsetX + updateLengthX), (uint)(startOffsetY + updateLengthY) }, - strides: stackalloc uint[] { (uint)updateLengthX }, + minimumExtents: [(uint)startOffsetX, (uint)startOffsetY], + maximimumExtents: [(uint)(startOffsetX + updateLengthX), (uint)(startOffsetY + updateLengthY)], + strides: [(uint)updateLengthX], data: data); for (int y = 0; y < updateLengthY; y++) @@ -740,13 +740,13 @@ public partial class D2D1ResourceTextureManagerTests byte[] texture = new byte[width * height]; D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc[] { (uint)width, (uint)height }, + extents: [(uint)width, (uint)height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: texture, - strides: stackalloc uint[] { width }); + strides: [width]); D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, 0); @@ -755,9 +755,9 @@ public partial class D2D1ResourceTextureManagerTests // Thread As are constantly updating the resource texture void UpdateResourceTexture() { - ReadOnlySpan minimumExtents = stackalloc uint[] { 0, 0 }; - ReadOnlySpan maximumExtents = stackalloc uint[] { width, height }; - ReadOnlySpan strides = stackalloc uint[] { width }; + ReadOnlySpan minimumExtents = [0, 0]; + ReadOnlySpan maximumExtents = [width, height]; + ReadOnlySpan strides = [width]; while (!cancellationTokenSource.IsCancellationRequested) { @@ -841,22 +841,22 @@ public partial class D2D1ResourceTextureManagerTests byte[] texture = new byte[width * height * depth]; D2D1ResourceTextureManager resourceTextureManager = new( - extents: stackalloc[] { (uint)width, (uint)height, (uint)depth }, + extents: [(uint)width, (uint)height, (uint)depth], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp], data: texture, - strides: stackalloc uint[] { (uint)width, (uint)(width * height) }); + strides: [(uint)width, (uint)(width * height)]); byte[] data = new byte[updateLengthX * updateLengthY * updateLengthZ]; RandomNumberGenerator.Create().GetBytes(data); resourceTextureManager.Update( - minimumExtents: stackalloc uint[] { (uint)startOffsetX, (uint)startOffsetY, (uint)startOffsetZ }, - maximimumExtents: stackalloc uint[] { (uint)(startOffsetX + updateLengthX), (uint)(startOffsetY + updateLengthY), (uint)(startOffsetZ + updateLengthZ) }, - strides: stackalloc uint[] { (uint)updateLengthX, (uint)(updateLengthX * updateLengthY) }, + minimumExtents: [(uint)startOffsetX, (uint)startOffsetY, (uint)startOffsetZ], + maximimumExtents: [(uint)(startOffsetX + updateLengthX), (uint)(startOffsetY + updateLengthY), (uint)(startOffsetZ + updateLengthZ)], + strides: [(uint)updateLengthX, (uint)(updateLengthX * updateLengthY)], data: data); for (int z = 0; z < updateLengthZ; z++) diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index a823b59a..283f6c6e 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -377,7 +377,7 @@ public sealed partial class BokehBlurEffect bufferPrecision: D2D1BufferPrecision.Float32, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(this.kernels[i].Real.AsSpan()), strides: []); @@ -387,7 +387,7 @@ public sealed partial class BokehBlurEffect bufferPrecision: D2D1BufferPrecision.Float32, channelDepth: D2D1ChannelDepth.One, filter: D2D1Filter.MinMagMipPoint, - extendModes: stackalloc[] { D2D1ExtendMode.Clamp }, + extendModes: [D2D1ExtendMode.Clamp], data: MemoryMarshal.AsBytes(this.kernels[i].Imaginary.AsSpan()), strides: []); diff --git a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs index c171a6fb..8982bf79 100644 --- a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs @@ -94,13 +94,13 @@ public class ShadersTests } resourceTextureManager = new D2D1ResourceTextureManager( - extents: stackalloc uint[] { (uint)texture.Width, (uint)texture.Height }, + extents: [(uint)texture.Width, (uint)texture.Height], bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipLinear, - extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror }, + extendModes: [D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror], data: MemoryMarshal.AsBytes(pixels.Span), - strides: stackalloc uint[] { (uint)(texture.Width * sizeof(Rgba32)) }); + strides: [(uint)(texture.Width * sizeof(Rgba32))]); } ContouredLayers shader = new(0f, new int2(1280, 720));