diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 1badc5cc..fa25ac2b 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -7,7 +7,7 @@ jobs: # Build the ComputeSharp project directly. This is the project that # would be built when running any of the local samples or unit tests. build-project: - runs-on: windows-latest + runs-on: windows-2022 steps: - name: Git checkout uses: actions/checkout@v2 @@ -23,7 +23,7 @@ jobs: run-tests: if: success() needs: [build-project] - runs-on: windows-latest + runs-on: windows-2022 steps: - name: Git checkout uses: actions/checkout@v2 @@ -47,6 +47,12 @@ jobs: - name: Run ComputeSharp.Tests.Internals (.NET Core 3.1) run: dotnet test tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj -c Release -f netcoreapp3.1 /p:Platform=x64 -v n -l "console;verbosity=detailed" shell: cmd + - name: Run ComputeSharp.Tests (.NET Framework 4.7.2) + run: dotnet test tests\ComputeSharp.Tests\ComputeSharp.Tests.csproj -c Release -f net472 /p:Platform=x64 -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.Tests.Internals (.NET Framework 4.7.2) + run: dotnet test tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj -c Release -f net472 /p:Platform=x64 -v n -l "console;verbosity=detailed" + shell: cmd - name: Run ComputeSharp.Tests.SourceGenerators run: dotnet test tests\ComputeSharp.Tests.SourceGenerators\ComputeSharp.Tests.SourceGenerators.csproj -v n -l "console;verbosity=detailed" shell: cmd @@ -55,7 +61,7 @@ jobs: run-samples: if: success() needs: [run-tests] - runs-on: windows-latest + runs-on: windows-2022 steps: - name: Git checkout uses: actions/checkout@v2 @@ -79,6 +85,12 @@ jobs: - name: Run ComputeSharp.Sample (.NET Core 3.1) run: samples\ComputeSharp.Sample\bin\x64\Release\netcoreapp3.1\win-x64\ComputeSharp.Sample.exe shell: cmd + - name: Build ComputeSharp.Sample (.NET Framework 4.7.2) + run: dotnet build samples\ComputeSharp.Sample\ComputeSharp.Sample.csproj -c Release -f net472 /p:Platform=x64 + shell: cmd + - name: Run ComputeSharp.Sample (.NET Framework 4.7.2) + run: samples\ComputeSharp.Sample\bin\x64\Release\net472\ComputeSharp.Sample.exe + shell: cmd - name: Build ComputeSharp.Sample.FSharp run: dotnet build samples\ComputeSharp.Sample.FSharp\ComputeSharp.Sample.FSharp.fsproj -c Release shell: cmd @@ -97,6 +109,12 @@ jobs: - name: Run ComputeSharp.ImageProcessing.csproj (.NET Core 3.1) run: samples\ComputeSharp.ImageProcessing\bin\x64\Release\netcoreapp3.1\win-x64\ComputeSharp.ImageProcessing.exe shell: cmd + - name: Build ComputeSharp.ImageProcessing.csproj (.NET Framework 4.7.2) + run: dotnet build samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj -c Release -f net472 /p:Platform=x64 + shell: cmd + - name: Run ComputeSharp.ImageProcessing.csproj (.NET Framework 4.7.2) + run: samples\ComputeSharp.ImageProcessing\bin\x64\Release\net472\ComputeSharp.ImageProcessing.exe + shell: cmd - name: Build ComputeSharp.Benchmark.csproj run: dotnet build samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj -c Release shell: cmd @@ -107,7 +125,7 @@ jobs: # Buils the .msbuildproj project to generate the NuGet package. # This workflow also uploads the resulting package as an artifact. build-package: - runs-on: windows-latest + runs-on: windows-2022 steps: - name: Git checkout uses: actions/checkout@v2 @@ -130,7 +148,7 @@ jobs: verify-package: if: success() needs: [build-package] - runs-on: windows-latest + runs-on: windows-2022 steps: - name: Git checkout uses: actions/checkout@v2 @@ -158,6 +176,12 @@ jobs: - name: Run ComputeSharp.Sample.NuGet (.NET Core 3.1) run: samples\ComputeSharp.Sample.NuGet\bin\Release\netcoreapp3.1\win-x64\ComputeSharp.Sample.NuGet.exe shell: cmd + - name: Build ComputeSharp.Sample.NuGet (.NET Framework 4.7.2) + run: dotnet build samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj -c Release -f net472 /p:Platform=x64 + shell: cmd + - name: Run ComputeSharp.Sample.NuGet (.NET Framework 4.7.2) + run: samples\ComputeSharp.Sample.NuGet\bin\x64\Release\net472\ComputeSharp.Sample.NuGet.exe + shell: cmd # Run the extra tests to validate a number of build and publishing configurations. # This is used to ensure the native dependencies can always be loaded correctly @@ -165,7 +189,7 @@ jobs: verify-libs: if: success() needs: [verify-package] - runs-on: windows-latest + runs-on: windows-2022 steps: - name: Git checkout uses: actions/checkout@v2 diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 0f85d34e..ac5b7e06 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -112,26 +112,26 @@ Global {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.Build.0 = Debug|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|ARM64.ActiveCfg = Debug|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|ARM64.Build.0 = Debug|Any CPU - {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.ActiveCfg = Debug|Any CPU - {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.Build.0 = Debug|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.ActiveCfg = Debug|x64 + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.Build.0 = Debug|x64 {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|Any CPU.ActiveCfg = Release|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|Any CPU.Build.0 = Release|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|ARM64.ActiveCfg = Release|Any CPU {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|ARM64.Build.0 = Release|Any CPU - {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.ActiveCfg = Release|Any CPU - {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.Build.0 = Release|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.ActiveCfg = Release|x64 + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.Build.0 = Release|x64 {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|Any CPU.Build.0 = Debug|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|ARM64.ActiveCfg = Debug|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|ARM64.Build.0 = Debug|Any CPU - {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|x64.ActiveCfg = Debug|Any CPU - {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|x64.Build.0 = Debug|Any CPU + {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|x64.ActiveCfg = Debug|x64 + {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Debug|x64.Build.0 = Debug|x64 {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.ActiveCfg = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|Any CPU.Build.0 = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|ARM64.ActiveCfg = Release|Any CPU {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|ARM64.Build.0 = Release|Any CPU - {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|Any CPU - {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|Any CPU + {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.ActiveCfg = Release|x64 + {E889E6D6-A814-4950-AAC1-68F1ECFC5BD6}.Release|x64.Build.0 = Release|x64 {EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|Any CPU.Build.0 = Debug|Any CPU {EB52CB67-4162-4722-874B-44EA8DA5450E}.Debug|ARM64.ActiveCfg = Debug|Any CPU diff --git a/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj index 8e8919f8..e9acb27c 100644 --- a/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj +++ b/samples/ComputeSharp.ImageProcessing/ComputeSharp.ImageProcessing.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 AnyCPU;x64;ARM64 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/samples/ComputeSharp.ImageProcessing/Program.cs b/samples/ComputeSharp.ImageProcessing/Program.cs index 2487916f..0318a63e 100644 --- a/samples/ComputeSharp.ImageProcessing/Program.cs +++ b/samples/ComputeSharp.ImageProcessing/Program.cs @@ -28,9 +28,16 @@ class Program using Image copy = image.Clone(c => c.ApplyProcessor(effect.Processor)); Console.WriteLine($">> Saving {effect.Name} to disk"); - string targetPath = Path.Combine( - Path.GetRelativePath(Path.GetDirectoryName(path)!, @"..\..\..\"), - $"{Path.GetFileNameWithoutExtension(path)}_{effect.Name}{Path.GetExtension(path)}"); + + string targetPath = Path.GetDirectoryName(path)!; + + while (Path.GetFileName(targetPath) != "ComputeSharp.ImageProcessing") + { + targetPath = Path.GetDirectoryName(targetPath)!; + } + + targetPath = Path.Combine(targetPath, $"{Path.GetFileNameWithoutExtension(path)}_{effect.Name}{Path.GetExtension(path)}"); + copy.Save(targetPath); } } diff --git a/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj b/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj index 0469d323..a518813b 100644 --- a/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj +++ b/samples/ComputeSharp.Sample.NuGet/ComputeSharp.Sample.NuGet.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 AnyCPU;x64;ARM64 diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index 3fc4ead9..c1dfd234 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -59,7 +59,7 @@ partial class Program for (int i = 0; i < height; i++) { - var row = array[(i * width)..((i + 1) * width)]; + var row = array.AsSpan(i * width, width).ToArray(); var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth))); Console.WriteLine(text); diff --git a/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj b/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj index 6b126849..ce698002 100644 --- a/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj +++ b/samples/ComputeSharp.Sample/ComputeSharp.Sample.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 AnyCPU;x64;ARM64 diff --git a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj index 1803a917..bb8d58fb 100644 --- a/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj +++ b/tests/ComputeSharp.Tests.Internals/ComputeSharp.Tests.Internals.csproj @@ -1,8 +1,9 @@  - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 false + AnyCPU;x64;ARM64 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..df48a716 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -1,8 +1,9 @@  - netcoreapp3.1;net6.0 + net472;netcoreapp3.1;net6.0 false + AnyCPU;x64;ARM64 @@ -23,13 +24,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)); + } } } }