diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index 7572930d..e41bb4f6 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -205,8 +205,8 @@ public sealed partial class HlslBokehBlurProcessor // Fill in the complex kernel values Unsafe.Add(ref baseRef, i) = new Complex64( - MathF.Exp(-a * value) * MathF.Cos(b * value), - MathF.Exp(-a * value) * MathF.Sin(b * value)); + (float)(Math.Exp(-a * value) * Math.Cos(b * value)), + (float)(Math.Exp(-a * value) * Math.Sin(b * value))); } return kernel; @@ -245,7 +245,7 @@ public sealed partial class HlslBokehBlurProcessor } // Normalize the kernels - float scalar = 1f / MathF.Sqrt(total); + float scalar = 1f / (float)Math.Sqrt(total); for (int i = 0; i < kernelsSpan.Length; i++) { diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 5176edea..5fb59656 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -80,13 +80,13 @@ public sealed partial class HlslGaussianBlurProcessor private static float Gaussian(float x, float sigma) { const float Numerator = 1.0f; - float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; + float denominator = (float)(Math.Sqrt(2 * Math.PI) * sigma); float exponentNumerator = -x * x; float exponentDenominator = 2 * sigma * sigma; float left = Numerator / denominator; - float right = MathF.Exp(exponentNumerator / exponentDenominator); + float right = (float)Math.Exp(exponentNumerator / exponentDenominator); return left * right; } diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 1803a917..378a9684 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 false diff --git a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs index 4e487777..e563da4a 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Exceptions; +using System; +using ComputeSharp.Exceptions; using ComputeSharp.Shaders.Translation; using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop.DirectX; @@ -32,7 +33,7 @@ public class ShaderCompilerTests [TestMethod] public void CompileTest_Ok() { - using ComPtr dxcBlob = ShaderCompiler.Instance.CompileShader(ShaderSource); + using ComPtr dxcBlob = ShaderCompiler.Instance.CompileShader(ShaderSource.AsSpan()); } [TestMethod] @@ -41,6 +42,6 @@ public class ShaderCompilerTests { var faultyShader = ShaderSource.Replace("ids.x", "ids.X"); - using ComPtr dxcBlob = ShaderCompiler.Instance.CompileShader(faultyShader); + using ComPtr dxcBlob = ShaderCompiler.Instance.CompileShader(faultyShader.AsSpan()); } } diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index d79eb7de..8c59e325 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 false @@ -23,13 +23,13 @@ - - - + $(NoWarn);CS0649 diff --git a/tests/ComputeSharp.Tests/Polyfills/RandomExtensions.cs b/tests/ComputeSharp.Tests/Polyfills/RandomExtensions.cs new file mode 100644 index 00000000..308c6724 --- /dev/null +++ b/tests/ComputeSharp.Tests/Polyfills/RandomExtensions.cs @@ -0,0 +1,25 @@ +#if !NETCOREAPP3_1_OR_GREATER + +namespace System; + +/// +/// A type with some helper methods for the class. +/// +internal static class RandomExtensions +{ + /// + /// Fills the elements of a specified span of bytes with random numbers. + /// + /// The target random instance. + /// The array to be filled with random numbers. + public static void NextBytes(this Random random, Span buffer) + { + byte[] array = new byte[buffer.Length]; + + random.NextBytes(array); + + array.AsSpan().CopyTo(buffer); + } +} + +#endif diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index 2797a423..b360e3c8 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -203,20 +203,26 @@ public partial class Texture3DTests [Data(0, 60, 2, 64, 4, 1)] [Data(63, 2, 0, 1, 60, 2)] [Data(2, 63, 1, 60, 1, 2)] - public void GetData_RangeToVoid_Ok(Device device, Type textureType, int x, int y, int z, int width, int height, int depth) + public unsafe void GetData_RangeToVoid_Ok(Device device, Type textureType, int x, int y, int z, int width, int height, int depth) { int[,,] array = new int[3, 64, 64]; - foreach (var item in array.AsSpan().Enumerate()) + fixed (int* p = array) { - item.Value = item.Index; + foreach (var item in new Span(p, array.Length).Enumerate()) + { + item.Value = item.Index; + } } using Texture3D texture = device.Get().AllocateTexture3D(textureType, array); int[,,] result = new int[depth, height, width]; - texture.CopyTo(result.AsSpan(), x, y, z, width, height, depth); + fixed (int* p = result) + { + texture.CopyTo(new Span(p, result.Length), x, y, z, width, height, depth); + } for (int k = 0; k < depth; k++) { diff --git a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs index be4dda0d..4ab2b901 100644 --- a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Runtime.CompilerServices; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -97,7 +98,7 @@ public partial class TransferTexture2DTests [CombinatorialTestMethod] [AllDevices] - public void Allocate_UploadTexture2D_Copy_Full(Device device) + public unsafe void Allocate_UploadTexture2D_Copy_Full(Device device) { using UploadTexture2D uploadTexture2D = device.Get().AllocateUploadTexture2D(256, 256); @@ -115,9 +116,12 @@ public partial class TransferTexture2DTests Assert.AreEqual(uploadTexture2D.Width, result.GetLength(1)); Assert.AreEqual(uploadTexture2D.Height, result.GetLength(0)); - for (int i = 0; i < uploadTexture2D.Height; i++) + fixed (int* _ = result) { - Assert.IsTrue(uploadTexture2D.View.GetRowSpan(i).SequenceEqual(result.GetRowSpan(i))); + for (int i = 0; i < uploadTexture2D.Height; i++) + { + Assert.IsTrue(uploadTexture2D.View.GetRowSpan(i).SequenceEqual(new Span(Unsafe.AsPointer(ref result[i, 0]), result.GetLength(1)))); + } } } @@ -127,7 +131,7 @@ public partial class TransferTexture2DTests [Data(128, 0, 1813, 2048)] [Data(0, 128, 2000, 944)] [Data(97, 33, 512, 794)] - public void Allocate_UploadTexture2D_Copy_Range(Device device, int x, int y, int width, int height) + public unsafe void Allocate_UploadTexture2D_Copy_Range(Device device, int x, int y, int width, int height) { using UploadTexture2D uploadTexture2D = device.Get().AllocateUploadTexture2D(2048, 2048); @@ -142,24 +146,27 @@ public partial class TransferTexture2DTests int[,] result = readOnlyTexture2D.ToArray(); - for (int i = 0; i < height; i++) + fixed (void* _ = result) { - Span sourceRow = uploadTexture2D.View.GetRowSpan(i + y).Slice(x, width); - Span destinationRow = result.GetRowSpan(i).Slice(0, width); + for (int i = 0; i < height; i++) + { + Span sourceRow = uploadTexture2D.View.GetRowSpan(i + y).Slice(x, width); + Span destinationRow = new Span(Unsafe.AsPointer(ref result[i, 0]), result.GetLength(1)).Slice(0, width); - Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } } } [CombinatorialTestMethod] [AllDevices] - public void Allocate_ReadBackTexture2D_Copy_Full(Device device) + public unsafe void Allocate_ReadBackTexture2D_Copy_Full(Device device) { int[,] source = new int[256, 256]; - for (int i = 0; i < 256; i++) + fixed (int* p = source) { - new Random(42).NextBytes(source.GetRowSpan(i).AsBytes()); + new Random(42).NextBytes(new Span(p, source.Length).AsBytes()); } using ReadOnlyTexture2D readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); @@ -170,9 +177,12 @@ public partial class TransferTexture2DTests Assert.AreEqual(readBackTexture2D.Width, 256); Assert.AreEqual(readBackTexture2D.Height, 256); - for (int i = 0; i < readBackTexture2D.Height; i++) + fixed (int* p = source) { - Assert.IsTrue(readBackTexture2D.View.GetRowSpan(i).SequenceEqual(source.GetRowSpan(i))); + for (int i = 0; i < readBackTexture2D.Height; i++) + { + Assert.IsTrue(readBackTexture2D.View.GetRowSpan(i).SequenceEqual(new Span(Unsafe.AsPointer(ref source[i, 0]), source.GetLength(1)))); + } } } @@ -182,13 +192,13 @@ public partial class TransferTexture2DTests [Data(128, 0, 1813, 2048)] [Data(0, 128, 2000, 944)] [Data(97, 33, 512, 794)] - public void Allocate_ReadBackTexture2D_Copy_Range(Device device, int x, int y, int width, int height) + public unsafe void Allocate_ReadBackTexture2D_Copy_Range(Device device, int x, int y, int width, int height) { int[,] source = new int[2048, 2048]; - for (int i = 0; i < 2048; i++) + fixed (int* p = source) { - new Random(42).NextBytes(source.GetRowSpan(i).AsBytes()); + new Random(42).NextBytes(new Span(p, source.Length).AsBytes()); } using ReadOnlyTexture2D readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); @@ -196,12 +206,15 @@ public partial class TransferTexture2DTests readOnlyTexture2D.CopyTo(readBackTexture2D, x, y, width, height); - for (int i = 0; i < height; i++) + fixed (int* p = source) { - Span sourceRow = source.GetRowSpan(i + y).Slice(x, width); - Span destinationRow = readBackTexture2D.View.GetRowSpan(i).Slice(0, width); + for (int i = 0; i < height; i++) + { + Span sourceRow = new Span(Unsafe.AsPointer(ref source[i + y, 0]), source.GetLength(1)).Slice(x, width); + Span destinationRow = readBackTexture2D.View.GetRowSpan(i).Slice(0, width); - Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } } } } diff --git a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs index c28bb6c6..2999795f 100644 --- a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Runtime.CompilerServices; using ComputeSharp.Resources; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -104,7 +105,7 @@ public partial class TransferTexture3DTests [CombinatorialTestMethod] [AllDevices] - public void Allocate_UploadTexture3D_Copy_Full(Device device) + public unsafe void Allocate_UploadTexture3D_Copy_Full(Device device) { using UploadTexture3D uploadTexture3D = device.Get().AllocateUploadTexture3D(256, 256, 16); @@ -126,14 +127,17 @@ public partial class TransferTexture3DTests Assert.AreEqual(uploadTexture3D.Height, result.GetLength(1)); Assert.AreEqual(uploadTexture3D.Depth, result.GetLength(0)); - for (int i = 0; i < uploadTexture3D.Depth; i++) + fixed (int* p = result) { - for (int j = 0; j < uploadTexture3D.Height; j++) + for (int i = 0; i < uploadTexture3D.Depth; i++) { - Span sourceRow = uploadTexture3D.View.GetRowSpan(j, i); - Span destinationRow = result.AsSpan2D(i).GetRowSpan(j); + for (int j = 0; j < uploadTexture3D.Height; j++) + { + Span sourceRow = uploadTexture3D.View.GetRowSpan(j, i); + Span destinationRow = new(Unsafe.AsPointer(ref result[i, j, 0]), result.GetLength(2)); - Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } } } } @@ -145,7 +149,7 @@ public partial class TransferTexture3DTests [Data(0, 128, 0, 189, 67, 8)] [Data(0, 0, 15, 32, 64, 1)] [Data(97, 33, 4, 128, 99, 7)] - public void Allocate_UploadTexture3D_Copy_Range(Device device, int x, int y, int z, int width, int height, int depth) + public unsafe void Allocate_UploadTexture3D_Copy_Range(Device device, int x, int y, int z, int width, int height, int depth) { using UploadTexture3D uploadTexture3D = device.Get().AllocateUploadTexture3D(256, 256, 16); @@ -163,30 +167,30 @@ public partial class TransferTexture3DTests int[,,] result = readOnlyTexture3D.ToArray(); - for (int i = 0; i < depth; i++) + fixed (int* p = result) { - for (int j = 0; j < height; j++) + for (int i = 0; i < depth; i++) { - Span sourceRow = uploadTexture3D.View.GetRowSpan(j + y, i + z).Slice(x, width); - Span destinationRow = result.AsSpan2D(i).GetRowSpan(j).Slice(0, width); + for (int j = 0; j < height; j++) + { + Span sourceRow = uploadTexture3D.View.GetRowSpan(j + y, i + z).Slice(x, width); + Span destinationRow = new Span(Unsafe.AsPointer(ref result[i, j, 0]), result.GetLength(2)).Slice(0, width); - Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } } } } [CombinatorialTestMethod] [AllDevices] - public void Allocate_ReadBackTexture3D_Copy_Full(Device device) + public unsafe void Allocate_ReadBackTexture3D_Copy_Full(Device device) { int[,,] source = new int[16, 256, 256]; - for (int i = 0; i < 16; i++) + fixed (int* p = source) { - for (int j = 0; j < 256; j++) - { - new Random(i * 5381 + j).NextBytes(source.AsSpan2D(i).GetRowSpan(j).AsBytes()); - } + new Random(42).NextBytes(new Span(p, source.Length).AsBytes()); } using ReadOnlyTexture3D readOnlyTexture3D = device.Get().AllocateReadOnlyTexture3D(source); @@ -198,14 +202,17 @@ public partial class TransferTexture3DTests Assert.AreEqual(readBackTexture3D.Height, 256); Assert.AreEqual(readBackTexture3D.Depth, 16); - for (int i = 0; i < readBackTexture3D.Depth; i++) + fixed (int* p = source) { - for (int j = 0; j < readBackTexture3D.Height; j++) + for (int i = 0; i < readBackTexture3D.Depth; i++) { - Span sourceRow = source.AsSpan2D(i).GetRowSpan(j); - Span destinationRow = readBackTexture3D.View.GetRowSpan(j, i); + for (int j = 0; j < readBackTexture3D.Height; j++) + { + Span sourceRow = new(Unsafe.AsPointer(ref source[i, j, 0]), source.GetLength(2)); + Span destinationRow = readBackTexture3D.View.GetRowSpan(j, i); - Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } } } } @@ -217,16 +224,13 @@ public partial class TransferTexture3DTests [Data(0, 128, 0, 189, 67, 8)] [Data(0, 0, 15, 32, 64, 1)] [Data(97, 33, 4, 128, 99, 7)] - public void Allocate_ReadBackTexture3D_Copy_Range(Device device, int x, int y, int z, int width, int height, int depth) + public unsafe void Allocate_ReadBackTexture3D_Copy_Range(Device device, int x, int y, int z, int width, int height, int depth) { int[,,] source = new int[16, 256, 256]; - for (int i = 0; i < 16; i++) + fixed (int* p = source) { - for (int j = 0; j < 256; j++) - { - new Random(i * 5381 + j).NextBytes(source.AsSpan2D(i).GetRowSpan(j).AsBytes()); - } + new Random(42).NextBytes(new Span(p, source.Length).AsBytes()); } using ReadOnlyTexture3D readOnlyTexture3D = device.Get().AllocateReadOnlyTexture3D(source); @@ -234,14 +238,17 @@ public partial class TransferTexture3DTests readOnlyTexture3D.CopyTo(readBackTexture3D, x, y, z, width, height, depth); - for (int i = 0; i < depth; i++) + fixed (int* p = source) { - for (int j = 0; j < height; j++) + for (int i = 0; i < depth; i++) { - Span sourceRow = source.AsSpan2D(i + z).GetRowSpan(j + y).Slice(x, width); - Span destinationRow = readBackTexture3D.View.GetRowSpan(j, i).Slice(0, width); + for (int j = 0; j < height; j++) + { + Span sourceRow = new Span(Unsafe.AsPointer(ref source[i + z, j + y, 0]), source.GetLength(2)).Slice(x, width); + Span destinationRow = readBackTexture3D.View.GetRowSpan(j, i).Slice(0, width); - Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); + } } } }