diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index e5b91118..5a9af369 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -3,6 +3,83 @@ name: .NET on: [push, pull_request] 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 + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.x + - name: Create local NuGet feed + run: mkdir src\ComputeSharp.Package\bin\Release + shell: cmd + - name: Build + run: dotnet build src\ComputeSharp\ComputeSharp.csproj -c Release + shell: cmd + + # Run all the unit tests referencing the ComputeSharp project directly. + run-tests: + if: success() + needs: [build-project] + runs-on: windows-latest + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.x + - name: Create local NuGet feed + run: mkdir src\ComputeSharp.Package\bin\Release + shell: cmd + - name: Run ComputeSharp.Tests + run: dotnet test tests\ComputeSharp.Tests\ComputeSharp.Tests.csproj -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.Tests.Internals + run: dotnet test tests\ComputeSharp.Tests.Internals\ComputeSharp.Tests.Internals.csproj -v n -l "console;verbosity=detailed" + shell: cmd + - name: Run ComputeSharp.Tests.Internals.SourceGenerators + run: dotnet test tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj -v n -l "console;verbosity=detailed" + shell: cmd + + # Run all the local samples to ensure they build and run with no errors. + run-samples: + if: success() + needs: [run-tests] + runs-on: windows-latest + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.x + - name: Create local NuGet feed + run: mkdir src\ComputeSharp.Package\bin\Release + shell: cmd + - name: Build ComputeSharp.Sample.Local + run: dotnet build samples\ComputeSharp.Sample.Local\ComputeSharp.Sample.Local.csproj -c Release + shell: cmd + - name: Run ComputeSharp.Sample.Local + run: dotnet samples\ComputeSharp.Sample.Local\bin\Release\net5.0\ComputeSharp.Sample.Local.dll + shell: cmd + - name: Build ComputeSharp.ImageProcessing.csproj + run: dotnet build samples\ComputeSharp.ImageProcessing\ComputeSharp.ImageProcessing.csproj -c Release + shell: cmd + - name: Run ComputeSharp.ImageProcessing.csproj + run: dotnet samples\ComputeSharp.ImageProcessing\bin\Release\net5.0\ComputeSharp.ImageProcessing.dll + shell: cmd + - name: Build ComputeSharp.Benchmark.csproj + run: dotnet build samples\ComputeSharp.Benchmark\ComputeSharp.Benchmark.csproj -c Release + shell: cmd + + # 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 steps: @@ -22,4 +99,54 @@ jobs: uses: actions/upload-artifact@v2 with: name: ComputeSharp.nupkg - path: src\ComputeSharp.Package\bin\Release\*.nupkg \ No newline at end of file + path: src\ComputeSharp.Package\bin\Release\*.nupkg + + # Download the NuGet package generated in the previous job and use it + # to build and run the sample project referencing it. This is used as + # a test to ensure the NuGet package works in a consuming project. + verify-package: + if: success() + needs: [build-package] + runs-on: windows-latest + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.x + - name: Create local NuGet feed + run: mkdir src\ComputeSharp.Package\bin\Release + shell: cmd + - name: Download package artifact + uses: actions/download-artifact@v2 + with: + name: ComputeSharp.nupkg + path: src\ComputeSharp.Package\bin\Release\ComputeSharp.nupkg + - name: Build ComputeSharp.Sample.NuGet + run: dotnet build samples\ComputeSharp.Sample.NuGet\ComputeSharp.Sample.NuGet.csproj -c Release + shell: cmd + - name: Run ComputeSharp.Sample.NuGet + run: dotnet samples\ComputeSharp.Sample.NuGet\bin\Release\net5.0\ComputeSharp.Sample.NuGet.dll + 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 + # regardless of whether the app is shipping with R2R, self-contained, etc. + verify-libs: + if: success() + needs: [verify-package] + runs-on: windows-latest + steps: + - name: Git checkout + uses: actions/checkout@v2 + - name: Setup .NET SDK + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 5.0.x + - name: Create local NuGet feed + run: mkdir src\ComputeSharp.Package\bin\Release + shell: cmd + - name: Run ComputeSharp.Tests.NativeLibrariesResolver + run: dotnet test tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj -v n -l "console;verbosity=detailed" + shell: cmd \ No newline at end of file diff --git a/nuget.config b/nuget.config index 5d5d8d13..c5173dab 100644 --- a/nuget.config +++ b/nuget.config @@ -3,5 +3,6 @@ + diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index 6e2d6959..44e91014 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -18,7 +18,7 @@ - + diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index dd4ecb24..25e9dfc9 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -26,7 +26,7 @@ namespace ComputeSharp.BokehBlur.Processors /// Initializes a new instance of the class. /// public HlslBokehBlurProcessor() - : this(DefaultRadius, DefaultComponents) + : this(Gpu.Default, DefaultRadius, DefaultComponents) { } @@ -36,11 +36,28 @@ namespace ComputeSharp.BokehBlur.Processors /// The size of the area to sample. /// The number of components to use to approximate the original 2D bokeh blur convolution kernel. public HlslBokehBlurProcessor(int radius, int components) + : this(Gpu.Default, radius, components) { + } + + /// + /// Initializes a new instance of the class. + /// + /// The associated with the current instance. + /// The size of the area to sample. + /// The number of components to use to approximate the original 2D bokeh blur convolution kernel. + public HlslBokehBlurProcessor(GraphicsDevice device, int radius, int components) + { + GraphicsDevice = device; Radius = radius; Components = components; } + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { get; } + /// /// Gets the radius. /// diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index f53f3759..eddd54af 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -20,6 +20,11 @@ namespace ComputeSharp.BokehBlur.Processors /// This processor is based on the code from Mike Pound, see github.com/mikepound/convolve. public sealed partial class Implementation : ImageProcessor { + /// + /// The instance in use. + /// + private readonly GraphicsDevice GraphicsDevice; + /// /// The kernel radius. /// @@ -65,6 +70,7 @@ namespace ComputeSharp.BokehBlur.Processors public Implementation(HlslBokehBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) : base(configuration, source, sourceRectangle) { + GraphicsDevice = definition.GraphicsDevice; Radius = definition.Radius; KernelSize = Radius * 2 + 1; ComponentsCount = definition.Components; @@ -256,14 +262,14 @@ namespace ComputeSharp.BokehBlur.Processors Span span = MemoryMarshal.Cast(pixelSpan); - using ReadWriteTexture2D texture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height); - using ReadWriteTexture2D temporary = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height, AllocationMode.Clear); - using ReadWriteTexture2D reals = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); - using ReadWriteTexture2D imaginaries = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); - using ReadOnlyBuffer kernel = Gpu.Default.AllocateReadOnlyBuffer(KernelSize); + using ReadWriteTexture2D texture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D temporary = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height, AllocationMode.Clear); + using ReadWriteTexture2D reals = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D imaginaries = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadOnlyBuffer kernel = GraphicsDevice.AllocateReadOnlyBuffer(KernelSize); // Preliminary gamma highlight pass - Gpu.Default.For(source.Height, new(texture)); + GraphicsDevice.For(source.Height, new(texture)); // Perform two 1D convolutions for each component in the current instance for (int j = 0; j < Kernels.Length; j++) @@ -272,19 +278,19 @@ namespace ComputeSharp.BokehBlur.Processors kernel.CopyFrom(Kernels[j]); - Gpu.Default.For( + GraphicsDevice.For( source.Width, source.Height, new(texture, reals, imaginaries, kernel)); - Gpu.Default.For( + GraphicsDevice.For( source.Width, source.Height, new(parameters.Z, parameters.W, reals, imaginaries, temporary, kernel)); } // Apply the inverse gamma exposure pass - Gpu.Default.For(source.Height, new(temporary, texture)); + GraphicsDevice.For(source.Height, new(temporary, texture)); // Write the final pixel data texture.CopyTo(span); @@ -409,7 +415,7 @@ namespace ComputeSharp.BokehBlur.Processors { Float4 v = source[i, ThreadIds.X]; - v.XYZ = Hlsl.Clamp(v.XYZ, 0, float.MaxValue); + v = Hlsl.Clamp(v, 0, float.MaxValue); v.XYZ = Hlsl.Pow(v.XYZ, 1 / 3f); target[i, ThreadIds.X] = v; diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs index 962db9cf..159e1965 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -20,25 +20,48 @@ namespace ComputeSharp.BokehBlur.Processors /// /// Initializes a new instance of the class. /// - public HlslGaussianBlurProcessor() : this(DefaultSigma, 9) { } + public HlslGaussianBlurProcessor() + : this(Gpu.Default, DefaultSigma, 9) + { + } /// /// Initializes a new instance of the class. /// /// The radius value representing the size of the area to sample. - public HlslGaussianBlurProcessor(int radius) : this(radius / 3F, radius) { } + public HlslGaussianBlurProcessor(int radius) + : this(Gpu.Default, radius / 3F, radius) + { + } /// /// Initializes a new instance of the class. /// + /// The associated with the current instance. + /// The radius value representing the size of the area to sample. + public HlslGaussianBlurProcessor(GraphicsDevice device, int radius) + : this(device, radius / 3F, radius) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The associated with the current instance. /// The sigma value representing the weight of the blur. /// The radius value representing the size of the area to sample (this should be at least twice the sigma value) - public HlslGaussianBlurProcessor(float sigma, int radius) + public HlslGaussianBlurProcessor(GraphicsDevice device, float sigma, int radius) { + GraphicsDevice = device; Sigma = sigma; Radius = radius; } + /// + /// The instance in use. + /// + private readonly GraphicsDevice GraphicsDevice; + /// /// Gets the sigma value representing the weight of the blur. /// diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index dcb932bd..e3b5a7c4 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -17,6 +17,11 @@ namespace ComputeSharp.BokehBlur.Processors /// internal sealed partial class Implementation : ImageProcessor { + /// + /// The instance in use. + /// + private readonly GraphicsDevice GraphicsDevice; + /// /// The 1D kernel to apply. /// @@ -33,6 +38,8 @@ namespace ComputeSharp.BokehBlur.Processors : base(configuration, source, sourceRectangle) { int kernelSize = definition.Radius * 2 + 1; + + GraphicsDevice = definition.GraphicsDevice; Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma); } @@ -94,12 +101,12 @@ namespace ComputeSharp.BokehBlur.Processors Span span = MemoryMarshal.Cast(pixelSpan); - using ReadWriteTexture2D sourceTexture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height); - using ReadWriteTexture2D firstPassTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); - using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); + using ReadWriteTexture2D sourceTexture = GraphicsDevice.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D firstPassTexture = GraphicsDevice.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadOnlyBuffer kernelBuffer = GraphicsDevice.AllocateReadOnlyBuffer(Kernel); - Gpu.Default.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer)); - Gpu.Default.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer)); + GraphicsDevice.For(source.Width, source.Height, new(sourceTexture, firstPassTexture, kernelBuffer)); + GraphicsDevice.For(source.Width, source.Height, new(firstPassTexture, sourceTexture, kernelBuffer)); sourceTexture.CopyTo(span); } diff --git a/samples/ComputeSharp.Sample.Shared/Program.cs b/samples/ComputeSharp.Sample.Shared/Program.cs index 30807dac..ee01f2a1 100644 --- a/samples/ComputeSharp.Sample.Shared/Program.cs +++ b/samples/ComputeSharp.Sample.Shared/Program.cs @@ -18,24 +18,19 @@ namespace ComputeSharp.Sample array[i] = i + 1; } - // Print the initial matrix - Console.WriteLine("===================== BEFORE ====================="); - PrintMatrix(array, width, height); - Console.WriteLine("=================================================="); - Console.WriteLine(); - using ReadWriteBuffer gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array); // Run the shader Gpu.Default.For(100, new MainKernel(gpuBuffer)); + // Print the initial matrix + PrintMatrix(array, width, height, "BEFORE"); + // Get the data back gpuBuffer.CopyTo(array); // Print the updated matrix - Console.WriteLine("===================== AFTER ======================"); - PrintMatrix(array, width, height); - Console.WriteLine("=================================================="); + PrintMatrix(array, width, height, "AFTER"); } /// @@ -59,8 +54,14 @@ namespace ComputeSharp.Sample /// The input array representing the matrix to print. /// The width of the array to print. /// The height of the array to print. - private static void PrintMatrix(float[] array, int width, int height) + /// The name of the matrix to print. + private static void PrintMatrix(float[] array, int width, int height, string name) { + int pad = 48 - name.Length; + string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}"; + + Console.WriteLine(title); + int length = width * height; int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length; ref float r = ref array[0]; @@ -73,6 +74,8 @@ namespace ComputeSharp.Sample if (i < length - 1) Console.Write(", "); if (i > 0 && (i + 1) % width == 0) Console.WriteLine(); } + + Console.WriteLine(new string('=', 50)); } } } diff --git a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs index f085dccc..d3a5b855 100644 --- a/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/HResultExtensions.cs @@ -1,6 +1,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using HRESULT = System.Int32; @@ -20,10 +21,24 @@ namespace ComputeSharp.Core.Extensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Assert(this HRESULT result) { +#if DEBUG + bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings(); + if (result < 0) { ThrowHelper.ThrowWin32Exception(result); } + + if (hasErrorsOrWarnings) + { + ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue"); + } +#else + if (result < 0) + { + ThrowHelper.ThrowWin32Exception(result); + } +#endif } } } diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index b101e51a..f7f1bd7e 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using ComputeSharp.Core.Extensions; +using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; @@ -11,7 +12,7 @@ using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; using static TerraFX.Interop.D3D12_FEATURE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; -using static TerraFX.Interop.D3D12_HEAP_FLAGS; +using static TerraFX.Interop.D3D12_MESSAGE_ID; using static TerraFX.Interop.D3D12_HEAP_TYPE; using static TerraFX.Interop.D3D12_RESOURCE_FLAGS; using static TerraFX.Interop.D3D12_RESOURCE_STATES; @@ -27,6 +28,34 @@ namespace ComputeSharp.Graphics.Extensions /// internal static unsafe class ID3D12DeviceExtensions { + /// + /// Creates a new for a given device. + /// + /// The target to use to create the info queue. + /// A pointer to the newly created instance. + /// Thrown when the creation of the info queue fails. + public static ComPtr CreateInfoQueue(this ref ID3D12Device d3D12Device) + { + ComPtr d3D12InfoQueue = default; + + d3D12Device.QueryInterface(FX.__uuidof(), d3D12InfoQueue.GetVoidAddressOf()).Assert(); + + D3D12_MESSAGE_ID* d3D12MessageIds = stackalloc D3D12_MESSAGE_ID[3] + { + D3D12_MESSAGE_ID_CREATEDEVICE_DEBUG_LAYER_STARTUP_OPTIONS, + D3D12_MESSAGE_ID_MAP_INVALID_NULLRANGE, + D3D12_MESSAGE_ID_UNMAP_INVALID_NULLRANGE + }; + + D3D12_INFO_QUEUE_FILTER d3D12InfoQueueFilter = default; + d3D12InfoQueueFilter.DenyList.NumIDs = 3; + d3D12InfoQueueFilter.DenyList.pIDList = d3D12MessageIds; + + d3D12InfoQueue.Get()->PushRetrievalFilter(&d3D12InfoQueueFilter).Assert(); + + return d3D12InfoQueue.Move(); + } + /// /// Creates a new of the specified type, for a given device. /// @@ -112,7 +141,6 @@ namespace ComputeSharp.Graphics.Extensions ulong sizeInBytes, bool isCacheCoherentUMA) { - D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; (D3D12_HEAP_TYPE d3D12HeapType, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, D3D12_RESOURCE_STATES d3D12ResourceStates) = resourceType switch @@ -148,7 +176,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - d3D12HeapFlags, + D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode), &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -180,7 +208,6 @@ namespace ComputeSharp.Graphics.Extensions bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { - D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -213,7 +240,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - d3D12HeapFlags, + D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode), &d3D12ResourceDescription, d3D12ResourceStates, null, @@ -247,7 +274,6 @@ namespace ComputeSharp.Graphics.Extensions bool isCacheCoherentUMA, out D3D12_RESOURCE_STATES d3D12ResourceStates) { - D3D12_HEAP_FLAGS d3D12HeapFlags = allocationMode == AllocationMode.Default ? D3D12_HEAP_FLAG_CREATE_NOT_ZEROED : D3D12_HEAP_FLAG_NONE; D3D12_RESOURCE_FLAGS d3D12ResourceFlags; (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch @@ -280,7 +306,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CreateCommittedResource( &d3D12HeapProperties, - d3D12HeapFlags, + D3D12FeatureHelper.GetD3D12HeapFlags(allocationMode), &d3D12ResourceDescription, d3D12ResourceStates, null, diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs index c6afd4bd..3995f71e 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Resources.Interop; @@ -38,5 +39,22 @@ namespace ComputeSharp.Graphics.Extensions { d3D12Resource.Unmap(0, null); } + + /// + /// Assigns a name to a given . + /// + /// The target to map. + /// The wrapper object to get the name from. + /// Thrown when the mapping operation fails. + [Conditional("DEBUG")] + public static void SetName(this ref ID3D12Resource d3D12Resource, object wrapper) + { + string name = wrapper.ToString()!; + + fixed (char* p = name) + { + d3D12Resource.SetName((ushort*)p).Assert(); + } + } } } diff --git a/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs b/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs new file mode 100644 index 00000000..8c3e003b --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/D3D12FeatureHelper.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_FEATURE; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// A helper exposing some runtime or OS dependent D3D12 features. + /// + internal static class D3D12FeatureHelper + { + /// + /// The currently supported flag (or fallback). + /// + private static readonly D3D12_HEAP_FLAGS D3D12_HEAP_FLAG_CREATE_NOT_ZEROED = GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED(); + + /// + /// Gets the value for a given setting. + /// + /// The allocation mode to use for creating new resources. + /// The matching that is currently supported + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static D3D12_HEAP_FLAGS GetD3D12HeapFlags(AllocationMode allocationMode) + { + if (allocationMode == AllocationMode.Default) + { + return D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; + } + + return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE; + } + + /// + /// Initializes the field. + /// + /// The currently supported flag. + private static unsafe D3D12_HEAP_FLAGS GetD3D12_HEAP_FLAG_CREATE_NOT_ZEROED() + { + lock (DeviceHelper.DevicesCache) + { + foreach (KeyValuePair pair in DeviceHelper.DevicesCache) + { + D3D12_FEATURE_DATA_D3D12_OPTIONS7 d3D12Options7; + + int result = pair.Value.D3D12Device->CheckFeatureSupport( + D3D12_FEATURE_D3D12_OPTIONS7, + &d3D12Options7, + (uint)sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS7)); + + if (result == FX.S_OK) + { + return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_CREATE_NOT_ZEROED; + } + + break; + } + + return D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE; + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs index d841433d..e2665b77 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Factory.cs @@ -1,4 +1,7 @@ using System.Collections.Generic; +#if DEBUG +using ComputeSharp.Graphics.Extensions; +#endif using TerraFX.Interop; namespace ComputeSharp.Graphics.Helpers @@ -9,7 +12,14 @@ namespace ComputeSharp.Graphics.Helpers /// /// The local cache of instances that are currently usable. /// - private static readonly Dictionary DevicesCache = new(); + internal static readonly Dictionary DevicesCache = new(); + +#if DEBUG + /// + /// The local map of instances for the existing devices. + /// + private static readonly Dictionary> D3D12InfoQueueMap = new(); +#endif /// /// Retrieves a instance for an object. @@ -28,6 +38,10 @@ namespace ComputeSharp.Graphics.Helpers device = new GraphicsDevice(d3D12Device, dxgiDescription1); DevicesCache.Add(luid, device); + +#if DEBUG + D3D12InfoQueueMap.Add(luid, d3D12Device.Get()->CreateInfoQueue()); +#endif } return device; @@ -43,6 +57,12 @@ namespace ComputeSharp.Graphics.Helpers lock (DevicesCache) { DevicesCache.Remove(device.Luid); + +#if DEBUG + D3D12InfoQueueMap.Remove(device.Luid, out ComPtr queue); + + queue.Dispose(); +#endif } } } diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs new file mode 100644 index 00000000..ad37f005 --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -0,0 +1,82 @@ +#if DEBUG + +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_MESSAGE_SEVERITY; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + internal static partial class DeviceHelper + { + /// + /// Flushes all the pending debug messages for all existing instances to the console/debugger. + /// It also checks whether or not there are any error messages being logged that didn't result in an actual crash yet. + /// + /// Whether or not there are any logged errors or warnings. + public static unsafe bool FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings() + { + bool hasErrorsOrWarnings = false; + + lock (DevicesCache) + { + int j = 0; + StringBuilder builder = new(1024); + + foreach (var pair in D3D12InfoQueueMap) + { + var device = DevicesCache[pair.Key]; + var queue = pair.Value; + + ulong messages = queue.Get()->GetNumStoredMessagesAllowedByRetrievalFilter(); + + for (ulong i = 0; i < messages; i++) + { + nuint length; + queue.Get()->GetMessage(i, null, &length); + + D3D12_MESSAGE* message = (D3D12_MESSAGE*)Marshal.AllocHGlobal((nint)length); + + queue.Get()->GetMessage(i, message, &length); + + builder.Clear(); + builder.AppendLine($"[D3D12 message #{i} for \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]"); + builder.AppendLine($"[Category]: {Enum.GetName(message->Category)}"); + builder.AppendLine($"[Severity]: {Enum.GetName(message->Severity)}"); + builder.AppendLine($"[ID]: {Enum.GetName(message->ID)}"); + builder.Append($"[Description]: \"{new string(message->pDescription)}\""); + + Marshal.FreeHGlobal((IntPtr)message); + + if (message->Severity is D3D12_MESSAGE_SEVERITY_ERROR or D3D12_MESSAGE_SEVERITY_CORRUPTION or D3D12_MESSAGE_SEVERITY_WARNING) + { + hasErrorsOrWarnings = true; + } + + string text = builder.ToString(); + + if (Debugger.IsAttached) + { + Debug.WriteLine(text); + } + else + { + Console.WriteLine(text); + } + } + + queue.Get()->ClearStoredMessages(); + + j++; + } + } + + return hasErrorsOrWarnings; + } + } +} + +#endif diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 4a30f2b8..c3f56001 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -8,29 +8,6 @@ namespace ComputeSharp.Graphics.Helpers /// internal static class MemoryHelper { - /// - /// Copies the content of a source memory area to a target one. - /// - /// The source memory area to read from. - /// The pointer for the destination memory area. - /// The destination offset to start writing data to. - /// The number of items to copy. - /// The size in bytes of each item to copy. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy( - void* source, - void* destination, - uint offset, - uint length, - uint elementSizeInBytes) - { - Buffer.MemoryCopy( - source, - (byte*)destination + offset * elementSizeInBytes, - ulong.MaxValue, - elementSizeInBytes * length); - } - /// /// Copies the content of a source memory area to a target one, accounting for padding. /// The destination memory area has padding for each element, while the source does not. @@ -40,19 +17,31 @@ namespace ComputeSharp.Graphics.Helpers /// The destination offset to start writing data to. /// The number of items to copy. /// The padded size of each element. + /// Indicates whether or not the source is padded too. public static unsafe void Copy( void* source, void* destination, uint offset, uint length, - uint elementPitchInBytes) + uint elementPitchInBytes, + bool isPaddingInSourceToo) where T : unmanaged { destination = (byte*)destination + offset * elementPitchInBytes; - for (int i = 0; i < length; i++) + if (isPaddingInSourceToo) { - *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i]; + for (int i = 0; i < length; i++) + { + *(T*)&((byte*)destination)[i * elementPitchInBytes] = *(T*)&((byte*)source)[i * elementPitchInBytes]; + } + } + else + { + for (int i = 0; i < length; i++) + { + *(T*)&((byte*)destination)[i * elementPitchInBytes] = ((T*)source)[i]; + } } } diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs index 46fbdc5a..fbb8ba9b 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Buffer{T}.cs @@ -80,6 +80,8 @@ namespace ComputeSharp.Resources device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); break; } + + this.d3D12Resource.Get()->SetName(this); } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 14395371..5c25645e 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -101,15 +101,29 @@ namespace ComputeSharp.Resources Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset)); Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(length)); - ulong - byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T), - byteOffset = (uint)offset * (uint)sizeof(T), - byteLength = (uint)length * (uint)sizeof(T); + if (GraphicsDevice.IsCacheCoherentUMA) + { + using ID3D12ResourceMap resource = D3D12Resource->Map(); - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + MemoryHelper.Copy( + resource.Pointer, + (uint)offset, + (uint)length, + (uint)sizeof(T), + destination.MappedData + destinationOffset); + } + else + { + ulong + byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T), + byteOffset = (uint)offset * (uint)sizeof(T), + byteLength = (uint)length * (uint)sizeof(T); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength); - copyCommandList.ExecuteAndWaitForCompletion(); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); + } } /// @@ -183,15 +197,29 @@ namespace ComputeSharp.Resources Guard.IsInRange(sourceOffset, 0, source.Length, nameof(sourceOffset)); Guard.IsLessThanOrEqualTo(sourceOffset + length, source.Length, nameof(length)); - ulong - byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T), - byteOffset = (uint)offset * (uint)sizeof(T), - byteLength = (uint)length * (uint)sizeof(T); + if (GraphicsDevice.IsCacheCoherentUMA) + { + using ID3D12ResourceMap resource = D3D12Resource->Map(); - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + MemoryHelper.Copy( + source.MappedData, + (uint)sourceOffset, + (uint)length, + (uint)sizeof(T), + (T*)resource.Pointer + offset); + } + else + { + ulong + byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T), + byteOffset = (uint)offset * (uint)sizeof(T), + byteLength = (uint)length * (uint)sizeof(T); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength); - copyCommandList.ExecuteAndWaitForCompletion(); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); + } } /// @@ -199,12 +227,14 @@ namespace ComputeSharp.Resources { GraphicsDevice.ThrowIfDisposed(); - ThrowIfDeviceMismatch(source.GraphicsDevice); ThrowIfDisposed(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); source.ThrowIfDisposed(); - if (!source.IsPaddingPresent && - source.GraphicsDevice == GraphicsDevice) + Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length)); + + if (!source.IsPaddingPresent) { // Directly copy the input buffer using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs index bed6e970..9510905c 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs @@ -111,6 +111,8 @@ namespace ComputeSharp.Resources device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); break; } + + this.d3D12Resource.Get()->SetName(this); } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index 9e4e6d54..3b1c9253 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -115,6 +115,8 @@ namespace ComputeSharp.Resources device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); break; } + + this.d3D12Resource.Get()->SetName(this); } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs index 431c5806..9c52d2a3 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -48,6 +48,8 @@ namespace ComputeSharp.Resources this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, sizeInBytes, device.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + + this.d3D12Resource.Get()->SetName(this); } /// @@ -65,6 +67,11 @@ namespace ComputeSharp.Resources /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Gets the pointer to the start of the mapped buffer data. + /// + internal T* MappedData => this.mappedData; + /// public Memory Memory { diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 57390ada..003a04c2 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -65,6 +65,8 @@ namespace ComputeSharp.Resources this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + + this.d3D12Resource.Get()->SetName(this); } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 16acda8a..3e92d0e5 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -68,6 +68,8 @@ namespace ComputeSharp.Resources this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + + this.d3D12Resource.Get()->SetName(this); } /// diff --git a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs index 5274b1f3..96d673af 100644 --- a/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ConstantBuffer{T}.cs @@ -3,7 +3,6 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; -using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Enums; @@ -11,7 +10,6 @@ using ComputeSharp.Graphics.Resources.Interop; using ComputeSharp.Resources; using ComputeSharp.Resources.Debug; using Microsoft.Toolkit.Diagnostics; -using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using FX = TerraFX.Interop.Windows; namespace ComputeSharp @@ -114,7 +112,8 @@ namespace ComputeSharp resource.Pointer, (uint)offset, (uint)size, - (uint)GetPaddedSize()); + (uint)GetPaddedSize(), + isPaddingInSourceToo: false); } else { @@ -133,18 +132,37 @@ namespace ComputeSharp { GraphicsDevice.ThrowIfDisposed(); - ThrowIfDeviceMismatch(source.GraphicsDevice); ThrowIfDisposed(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); source.ThrowIfDisposed(); - if (source is ConstantBuffer && - source.GraphicsDevice == GraphicsDevice) - { - // Directly copy the input buffer, if possible - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + Guard.IsLessThanOrEqualTo(source.Length, Length, nameof(Length)); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, 0, source.D3D12Resource, 0,(ulong)SizeInBytes); - copyCommandList.ExecuteAndWaitForCompletion(); + if (source is ConstantBuffer buffer) + { + using ID3D12ResourceMap sourceMap = buffer.D3D12Resource->Map(); + using ID3D12ResourceMap destinationMap = D3D12Resource->Map(); + + if (IsPaddingPresent) + { + MemoryHelper.Copy( + sourceMap.Pointer, + destinationMap.Pointer, + 0, + (uint)source.Length, + (uint)GetPaddedSize(), + isPaddingInSourceToo: true); + } + else + { + MemoryHelper.Copy( + sourceMap.Pointer, + 0, + (uint)source.Length, + (uint)sizeof(T), + destinationMap.Pointer); + } } else CopyFromWithCpuBuffer(source); } diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 9c25bf58..b044bc0e 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -48,7 +48,7 @@ - + diff --git a/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs new file mode 100644 index 00000000..54550319 --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs @@ -0,0 +1,12 @@ +using System; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with targeting all supported devices. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class AllDevicesAttribute : Attribute + { + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs new file mode 100644 index 00000000..ff68a4aa --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// A custom implementation that also acts as a combinatorial + /// instance. It has dedicated paths for target devices and resource types, and arbitrary inputs for the test methods. + /// + /// + /// Use this attribute in combination with , , + /// or , not other existing MSTest attributes. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITestDataSource + { + /// + IEnumerable ITestDataSource.GetData(MethodInfo methodInfo) + { + Device[] devices; + + if (methodInfo.GetCustomAttribute() is not null) + { + devices = Enum.GetValues(); + } + else if (methodInfo.GetCustomAttributes().ToArray() is { Length: > 0 } values) + { + devices = values.Select(static value => value.Device).ToArray(); + } + else + { + devices = Array.Empty(); + } + + Type[] resources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); + object[][] data = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray(); + + if (devices.Length > 0) + { + foreach (Device device in devices) + { + if (resources.Length > 0) + { + foreach (Type type in resources) + { + if (data.Length == 0) + { + yield return new object[] { device, type }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device, type }.Concat(items).ToArray(); + } + } + } + } + else + { + if (data.Length == 0) + { + yield return new object[] { device }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device }.Concat(items).ToArray(); + } + } + } + } + } + else if (resources.Length > 0) + { + foreach (Type type in resources) + { + if (data.Length == 0) + { + yield return new object[] { type }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { type }.Concat(items).ToArray(); + } + } + } + } + else + { + if (data.Length == 0) + { + yield return Array.Empty(); + } + else + { + foreach (object[] items in data) + { + yield return items; + } + } + } + } + + /// + string ITestDataSource.GetDisplayName(MethodInfo methodInfo, object[] data) + { + return $"{methodInfo.Name} ({string.Join(", ", data)})"; + } + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs new file mode 100644 index 00000000..fa99504e --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs @@ -0,0 +1,21 @@ +using System; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with adding arbitrary input data. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public sealed class DataAttribute : Attribute + { + public DataAttribute(params object[] data) + { + Data = data; + } + + /// + /// Gets the input data for the current attribute. + /// + public object[] Data { get; } + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs new file mode 100644 index 00000000..ce13a6a1 --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs @@ -0,0 +1,22 @@ +using System; +using ComputeSharp.Tests.Extensions; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with adding a specific input. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class DeviceAttribute : Attribute + { + public DeviceAttribute(Device device) + { + Device = device; + } + + /// + /// Gets the target to use. + /// + public Device Device { get; } + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs new file mode 100644 index 00000000..afdff7fd --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with targeting a specific resource type. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public sealed class ResourceAttribute : Attribute + { + public ResourceAttribute(Type type) + { + Type = type; + } + + public Type Type { get; } + } +} diff --git a/tests/ComputeSharp.Tests/BufferTests.cs b/tests/ComputeSharp.Tests/BufferTests.cs index ad1ad729..b1ffbb42 100644 --- a/tests/ComputeSharp.Tests/BufferTests.cs +++ b/tests/ComputeSharp.Tests/BufferTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,45 +11,47 @@ namespace ComputeSharp.Tests [TestCategory("Buffer")] public partial class BufferTests { - [TestMethod] - [DataRow(typeof(ConstantBuffer<>))] - [DataRow(typeof(ReadOnlyBuffer<>))] - [DataRow(typeof(ReadWriteBuffer<>))] - public void Allocate_Uninitialized_Ok(Type bufferType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + public void Allocate_Uninitialized_Ok(Device device, Type bufferType) { - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, 128); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Length, 128); - Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + Assert.AreSame(buffer.GraphicsDevice, device.Get()); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>), -247824)] - [DataRow(typeof(ConstantBuffer<>), -1)] - [DataRow(typeof(ReadOnlyBuffer<>), -247824)] - [DataRow(typeof(ReadOnlyBuffer<>), -1)] - [DataRow(typeof(ReadWriteBuffer<>), -247824)] - [DataRow(typeof(ReadWriteBuffer<>), -1)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(-247824)] + [Data(-1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Type bufferType, int length) + public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int length) { - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, length); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, length); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>))] - [DataRow(typeof(ReadOnlyBuffer<>))] - [DataRow(typeof(ReadWriteBuffer<>))] - public void Allocate_FromArray(Type bufferType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + public void Allocate_FromArray(Device device, Type bufferType) { float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, data); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, data); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Length, 128); - Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + Assert.AreSame(buffer.GraphicsDevice, device.Get()); float[] result = buffer.ToArray(); @@ -57,26 +60,24 @@ namespace ComputeSharp.Tests Assert.IsTrue(data.SequenceEqual(result)); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>), typeof(ConstantBuffer<>))] - [DataRow(typeof(ConstantBuffer<>), typeof(ReadOnlyBuffer<>))] - [DataRow(typeof(ConstantBuffer<>), typeof(ReadWriteBuffer<>))] - [DataRow(typeof(ReadOnlyBuffer<>), typeof(ConstantBuffer<>))] - [DataRow(typeof(ReadOnlyBuffer<>), typeof(ReadOnlyBuffer<>))] - [DataRow(typeof(ReadOnlyBuffer<>), typeof(ReadWriteBuffer<>))] - [DataRow(typeof(ReadWriteBuffer<>), typeof(ConstantBuffer<>))] - [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadOnlyBuffer<>))] - [DataRow(typeof(ReadWriteBuffer<>), typeof(ReadWriteBuffer<>))] - public void Allocate_FromBuffer(Type sourceType, Type destinationType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(typeof(ConstantBuffer<>))] + [Data(typeof(ReadOnlyBuffer<>))] + [Data(typeof(ReadWriteBuffer<>))] + public void Allocate_FromBuffer(Device device, Type sourceType, Type destinationType) { float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray(); - using Buffer source = Gpu.Default.AllocateBuffer(sourceType, data); - using Buffer destination = Gpu.Default.AllocateBuffer(destinationType, source); + using Buffer source = device.Get().AllocateBuffer(sourceType, data); + using Buffer destination = device.Get().AllocateBuffer(destinationType, source); Assert.IsNotNull(destination); Assert.AreEqual(destination.Length, 128); - Assert.AreSame(destination.GraphicsDevice, Gpu.Default); + Assert.AreSame(destination.GraphicsDevice, device.Get()); float[] result = destination.ToArray(); @@ -85,35 +86,34 @@ namespace ComputeSharp.Tests Assert.IsTrue(data.SequenceEqual(result)); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>))] - [DataRow(typeof(ReadOnlyBuffer<>))] - [DataRow(typeof(ReadWriteBuffer<>))] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Type bufferType) + public void UsageAfterDispose(Device device, Type bufferType) { - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, 128); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, 128); buffer.Dispose(); _ = buffer.ToArray(); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>), 0, 4096)] - [DataRow(typeof(ConstantBuffer<>), 128, 512)] - [DataRow(typeof(ConstantBuffer<>), 2048, 2048)] - [DataRow(typeof(ReadOnlyBuffer<>), 0, 4096)] - [DataRow(typeof(ReadOnlyBuffer<>), 128, 512)] - [DataRow(typeof(ReadOnlyBuffer<>), 2048, 2048)] - [DataRow(typeof(ReadWriteBuffer<>), 0, 4096)] - [DataRow(typeof(ReadWriteBuffer<>), 128, 512)] - [DataRow(typeof(ReadWriteBuffer<>), 2048, 2048)] - public void CopyTo_RangeToArray_Ok(Type bufferType, int offset, int count) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(0, 4096)] + [Data(128, 512)] + [Data(2048, 2048)] + public void CopyTo_RangeToArray_Ok(Device device, Type bufferType, int offset, int count) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, array); float[] result = buffer.ToArray(offset, count); @@ -122,47 +122,39 @@ namespace ComputeSharp.Tests Assert.IsTrue(array.AsSpan(offset, count).SequenceEqual(result)); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>), -1, 128)] - [DataRow(typeof(ConstantBuffer<>), 8192, 128)] - [DataRow(typeof(ConstantBuffer<>), 512, 4096)] - [DataRow(typeof(ConstantBuffer<>), 512, -128)] - [DataRow(typeof(ReadOnlyBuffer<>), -1, 128)] - [DataRow(typeof(ReadOnlyBuffer<>), 8192, 128)] - [DataRow(typeof(ReadOnlyBuffer<>), 512, 4096)] - [DataRow(typeof(ReadOnlyBuffer<>), 512, -128)] - [DataRow(typeof(ReadWriteBuffer<>), -1, 128)] - [DataRow(typeof(ReadWriteBuffer<>), 8192, 128)] - [DataRow(typeof(ReadWriteBuffer<>), 512, 4096)] - [DataRow(typeof(ReadWriteBuffer<>), 512, -128)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(-1, 128)] + [Data(8192, 128)] + [Data(512, 4096)] + [Data(512, -128)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void CopyTo_RangeToArray_Fail(Type bufferType, int offset, int count) + public void CopyTo_RangeToArray_Fail(Device device, Type bufferType, int offset, int count) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, array); _ = buffer.ToArray(offset, count); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>), 0, 0, 4096)] - [DataRow(typeof(ConstantBuffer<>), 512, 0, 2048)] - [DataRow(typeof(ConstantBuffer<>), 0, 512, 2048)] - [DataRow(typeof(ConstantBuffer<>), 127, 1024, 587)] - [DataRow(typeof(ReadOnlyBuffer<>), 0, 0, 4096)] - [DataRow(typeof(ReadOnlyBuffer<>), 512, 0, 2048)] - [DataRow(typeof(ReadOnlyBuffer<>), 0, 512, 2048)] - [DataRow(typeof(ReadOnlyBuffer<>), 127, 1024, 587)] - [DataRow(typeof(ReadWriteBuffer<>), 0, 0, 4096)] - [DataRow(typeof(ReadWriteBuffer<>), 512, 0, 2048)] - [DataRow(typeof(ReadWriteBuffer<>), 0, 512, 2048)] - [DataRow(typeof(ReadWriteBuffer<>), 127, 1024, 587)] - public void CopyTo_RangeToVoid_Ok(Type bufferType, int destinationOffset, int bufferOffset, int count) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(0, 0, 4096)] + [Data(512, 0, 2048)] + [Data(0, 512, 2048)] + [Data(127, 1024, 587)] + public void CopyTo_RangeToVoid_Ok(Device device, Type bufferType, int destinationOffset, int bufferOffset, int count) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, array); float[] result = new float[4096]; @@ -171,46 +163,39 @@ namespace ComputeSharp.Tests Assert.IsTrue(array.AsSpan(bufferOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count))); } - [TestMethod] - [DataRow(typeof(ConstantBuffer<>), 0, 0, 8196)] - [DataRow(typeof(ConstantBuffer<>), -12, 0, 1024)] - [DataRow(typeof(ConstantBuffer<>), 0, -56, 1024)] - [DataRow(typeof(ConstantBuffer<>), 512, 0, 4096)] - [DataRow(typeof(ConstantBuffer<>), 12, 1024, 3600)] - [DataRow(typeof(ConstantBuffer<>), 12, 1024, -2096)] - [DataRow(typeof(ReadOnlyBuffer<>), 0, 0, 8196)] - [DataRow(typeof(ReadOnlyBuffer<>), -12, 0, 1024)] - [DataRow(typeof(ReadOnlyBuffer<>), 0, -56, 1024)] - [DataRow(typeof(ReadOnlyBuffer<>), 512, 0, 4096)] - [DataRow(typeof(ReadOnlyBuffer<>), 12, 1024, 3600)] - [DataRow(typeof(ReadOnlyBuffer<>), 12, 1024, -2096)] - [DataRow(typeof(ReadWriteBuffer<>), 0, 0, 8196)] - [DataRow(typeof(ReadWriteBuffer<>), -12, 0, 1024)] - [DataRow(typeof(ReadWriteBuffer<>), 0, -56, 1024)] - [DataRow(typeof(ReadWriteBuffer<>), 512, 0, 4096)] - [DataRow(typeof(ReadWriteBuffer<>), 12, 1024, 3600)] - [DataRow(typeof(ReadWriteBuffer<>), 12, 1024, -2096)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ConstantBuffer<>))] + [Resource(typeof(ReadOnlyBuffer<>))] + [Resource(typeof(ReadWriteBuffer<>))] + [Data(0, 0, 8196)] + [Data(-12, 0, 1024)] + [Data(0, -56, 1024)] + [Data(512, 0, 4096)] + [Data(12, 1024, 3600)] + [Data(12, 1024, -2096)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void CopyTo_RangeToVoid_Fail(Type bufferType, int destinationOffset, int bufferOffset, int count) + public void CopyTo_RangeToVoid_Fail(Device device, Type bufferType, int destinationOffset, int bufferOffset, int count) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); - using Buffer buffer = Gpu.Default.AllocateBuffer(bufferType, array); + using Buffer buffer = device.Get().AllocateBuffer(bufferType, array); float[] result = new float[4096]; buffer.CopyTo(result, destinationOffset, bufferOffset, count); } - [TestMethod] - public void Dispatch_ConstantBuffer() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ConstantBuffer(Device device) { int[] data = Enumerable.Range(0, 1024).ToArray(); - using ConstantBuffer source = Gpu.Default.AllocateConstantBuffer(data); - using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + using ConstantBuffer source = device.Get().AllocateConstantBuffer(data); + using ReadWriteBuffer destination = device.Get().AllocateReadWriteBuffer(data.Length); - Gpu.Default.For(source.Length, new ConstantBufferKernel(source, destination)); + device.Get().For(source.Length, new ConstantBufferKernel(source, destination)); int[] result = destination.ToArray(); @@ -229,15 +214,16 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Dispatch_ReadOnlyBuffer() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ReadOnlyBuffer(Device device) { int[] data = Enumerable.Range(0, 1024).ToArray(); - using ReadOnlyBuffer source = Gpu.Default.AllocateReadOnlyBuffer(data); - using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + using ReadOnlyBuffer source = device.Get().AllocateReadOnlyBuffer(data); + using ReadWriteBuffer destination = device.Get().AllocateReadWriteBuffer(data.Length); - Gpu.Default.For(source.Length, new ReadOnlyBufferKernel(source, destination)); + device.Get().For(source.Length, new ReadOnlyBufferKernel(source, destination)); int[] result = destination.ToArray(); @@ -256,15 +242,16 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Dispatch_ReadWriteBuffer() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ReadWriteBuffer(Device device) { int[] data = Enumerable.Range(0, 1024).ToArray(); - using ReadWriteBuffer source = Gpu.Default.AllocateReadWriteBuffer(data); - using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + using ReadWriteBuffer source = device.Get().AllocateReadWriteBuffer(data); + using ReadWriteBuffer destination = device.Get().AllocateReadWriteBuffer(data.Length); - Gpu.Default.For(source.Length, new ReadWriteBufferKernel(source, destination)); + device.Get().For(source.Length, new ReadWriteBufferKernel(source, destination)); int[] result = destination.ToArray(); diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 11ddfefb..b65efcb0 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -4,6 +4,7 @@ net5.0 false 9.0 + enable true @@ -12,6 +13,7 @@ + @@ -22,6 +24,18 @@ PrivateAssets="contentfiles;build"/> + + + + + + + + + + + + TextTemplatingFileGenerator diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index 2252e6f9..bb413b67 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -1,4 +1,6 @@ using System; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -7,12 +9,13 @@ namespace ComputeSharp.Tests [TestCategory("Dispatch")] public partial class DispatchTests { - [TestMethod] - public void Verify_ThreadIds() + [CombinatorialTestMethod] + [AllDevices] + public void Verify_ThreadIds(Device device) { - using ReadWriteTexture3D buffer = Gpu.Default.AllocateReadWriteTexture3D(50, 50, 50); + using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(50, 50, 50); - Gpu.Default.For(buffer.Width, buffer.Height, buffer.Depth, new ThreadIdsShader(buffer)); + device.Get().For(buffer.Width, buffer.Height, buffer.Depth, new ThreadIdsShader(buffer)); Int4[,,] data = buffer.ToArray(); @@ -32,18 +35,19 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(0, 8, 8)] - [DataRow(8, 0, 8)] - [DataRow(8, 8, 0)] - [DataRow(8, -3, 16)] - [DataRow(-1, -1, -1)] + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 8, 8)] + [Data(8, 0, 8)] + [Data(8, 8, 0)] + [Data(8, -3, 16)] + [Data(-1, -1, -1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Verify_ThreadIds_OutOfRange(int x, int y, int z) + public void Verify_ThreadIds_OutOfRange(Device device, int x, int y, int z) { - using ReadWriteTexture3D buffer = Gpu.Default.AllocateReadWriteTexture3D(50, 50, 50); + using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(50, 50, 50); - Gpu.Default.For(x, y, z, new ThreadIdsShader(buffer)); + device.Get().For(x, y, z, new ThreadIdsShader(buffer)); Assert.Fail(); } @@ -59,12 +63,13 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Verify_GroupIds() + [CombinatorialTestMethod] + [AllDevices] + public void Verify_GroupIds(Device device) { - using ReadWriteTexture3D buffer = Gpu.Default.AllocateReadWriteTexture3D(50, 50, 50); + using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(50, 50, 50); - Gpu.Default.For(buffer.Width, buffer.Height, buffer.Depth, 4, 4, 4, new GroupIdsShader(buffer)); + device.Get().For(buffer.Width, buffer.Height, buffer.Depth, 4, 4, 4, new GroupIdsShader(buffer)); Int4[,,] data = buffer.ToArray(); @@ -85,21 +90,22 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(0, 8, 8)] - [DataRow(8, 0, 8)] - [DataRow(8, 8, 0)] - [DataRow(1025, 8, 8)] - [DataRow(8, 2000, 8)] - [DataRow(8, 8, 70)] - [DataRow(8, -1, 8)] - [DataRow(8, 15, 16)] + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 8, 8)] + [Data(8, 0, 8)] + [Data(8, 8, 0)] + [Data(1025, 8, 8)] + [Data(8, 2000, 8)] + [Data(8, 8, 70)] + [Data(8, -1, 8)] + [Data(8, 15, 16)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Verify_GroupIds_OutOfRange(int x, int y, int z) + public void Verify_GroupIds_OutOfRange(Device device, int x, int y, int z) { - using ReadWriteTexture3D buffer = Gpu.Default.AllocateReadWriteTexture3D(50, 50, 50); + using ReadWriteTexture3D buffer = device.Get().AllocateReadWriteTexture3D(50, 50, 50); - Gpu.Default.For(buffer.Width, buffer.Height, buffer.Depth, x, y, z, new GroupIdsShader(buffer)); + device.Get().For(buffer.Width, buffer.Height, buffer.Depth, x, y, z, new GroupIdsShader(buffer)); Assert.Fail(); } @@ -116,12 +122,13 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Verify_GroupSize() + [CombinatorialTestMethod] + [AllDevices] + public void Verify_GroupSize(Device device) { - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(32); + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(32); - Gpu.Default.For(1, 1, 1, 4, 15, 7, new GroupSizeShader(buffer)); + device.Get().For(1, 1, 1, 4, 15, 7, new GroupSizeShader(buffer)); int[] data = buffer.ToArray(); diff --git a/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs new file mode 100644 index 00000000..0b4d44c5 --- /dev/null +++ b/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs @@ -0,0 +1,80 @@ +using System.Linq; +using Microsoft.Toolkit.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Extensions +{ + /// + /// A helper class for testing APIs. + /// + public static class DeviceExtensions + { + /// + /// The instance for the default discrete device. + /// + private static readonly GraphicsDevice? DiscreteDevice; + + /// + /// The instance for the WARP device. + /// + private static readonly GraphicsDevice WarpDevice; + + /// + /// Initializes and . + /// + static DeviceExtensions() + { + GraphicsDevice defaultDevice = Gpu.Default; + + if (defaultDevice.IsHardwareAccelerated) + { + DiscreteDevice = defaultDevice; + WarpDevice = Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(); + } + else + { + DiscreteDevice = null; + WarpDevice = defaultDevice; + } + } + + /// + /// Gets a instance matching a specified type. + /// + /// The device to retrieve. + /// The device of the requested type. + /// Thrown when the requested device is not present. + public static GraphicsDevice Get(this Device type) + { + GraphicsDevice? device = type switch + { + Device.Discrete => DiscreteDevice, + Device.Warp => WarpDevice, + _ => ThrowHelper.ThrowArgumentException("Invalid device") + }; + + if (device is null) + { + Assert.Inconclusive(); + } + + return device!; + } + } + + /// + /// Indicates a specific compute device. + /// + public enum Device + { + /// + /// A discrete device. + /// + Discrete, + + /// + /// A WARP device (software fallback). + /// + Warp + } +} diff --git a/tests/ComputeSharp.Tests/Extensions/SpanExtensions.cs b/tests/ComputeSharp.Tests/Extensions/SpanExtensions.cs deleted file mode 100644 index 59c2e235..00000000 --- a/tests/ComputeSharp.Tests/Extensions/SpanExtensions.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; - -namespace ComputeSharp.Tests.Extensions -{ - /// - /// An helper with methods to process fixed-size buffers. - /// - public static class SpanExtensions - { - /// - /// The maximum threshold to validate two values as being equal. - /// - private const float FloatThreshold = 0.00001f; - - /// - /// Checks whether or not the two input instances have the same content. - /// - /// The first to check. - /// The second to check. - [Pure] - public static bool ContentEquals(this Span a, Span b) - { - if (a.Length != b.Length) return false; - if (a.Length == 0) return true; - - var l = a.Length; - ref var ra = ref a.GetPinnableReference(); - ref var rb = ref b.GetPinnableReference(); - - for (var i = 0; i < l; i++) - if (Math.Abs(Unsafe.Add(ref ra, i) - Unsafe.Add(ref rb, i)) > FloatThreshold) - return false; - - return true; - } - } -} diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs new file mode 100644 index 00000000..c51cb09a --- /dev/null +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using ComputeSharp.BokehBlur.Processors; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Processing; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; + +namespace ComputeSharp.Tests +{ + [TestClass] + [TestCategory("Imaging")] + public class ImagingTests + { + [CombinatorialTestMethod] + [AllDevices] + public void BokehBlur(Device device) + { + // Early test to ensure the device is available. This saves time when running the + // unit test if the target device is not available, as we skip the preprocessing. + _ = device.Get(); + + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + + using var original = Image.Load(Path.Combine(path, "city.jpg")); + using var cpu = original.Clone(c => c.BokehBlur(80, 2, 3)); + using var gpu = original.Clone(c => c.ApplyProcessor(new HlslBokehBlurProcessor(device.Get(), 80, 2))); + + string + expectedPath = Path.Combine(path, "city_bokeh_cpu.jpg"), + actualPath = Path.Combine(path, "city_bokeh_gpu.jpg"); + + cpu.Save(expectedPath); + gpu.Save(actualPath); + + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000009f); + } + + [CombinatorialTestMethod] + [AllDevices] + public void GaussianBlur(Device device) + { + _ = device.Get(); + + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + + using var original = Image.Load(Path.Combine(path, "city.jpg")); + using var cpu = original.Clone(c => c.GaussianBlur(30f)); + using var gpu = original.Clone(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(device.Get(), 90))); + + string + expectedPath = Path.Combine(path, "city_gaussian_cpu.jpg"), + actualPath = Path.Combine(path, "city_gaussian_gpu.jpg"); + + cpu.Save(expectedPath); + gpu.Save(actualPath); + + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.000003f); + } + + /// + /// An tolerant image comparer type, from ImageSharp. + /// + private static class TolerantImageComparer + { + /// + /// Asserts that two images are equal. + /// + /// The path to the reference image. + /// The path to the expected image. + /// The allowed difference threshold for the normalized delta. + public static void AssertEqual(string expectedPath, string actualPath, float threshold) + { + using var expected = Image.Load(expectedPath); + using var actual = Image.Load(actualPath); + + if (expected.Size() != actual.Size()) + { + Assert.Fail($"The input images have different sizes: {expected.Size()} and {actual.Size()}"); + } + + if (expected.Frames.Count != actual.Frames.Count || + expected.Frames.Count != 1 || + actual.Frames.Count != 1) + { + Assert.Fail("The two input images must have 1 frame each"); + } + + int width = actual.Width; + + float totalDifference = 0F; + + var differences = new List(20); + + for (int y = 0; y < actual.Height; y++) + { + Span aSpan = expected.GetPixelRowSpan(y); + Span bSpan = actual.GetPixelRowSpan(y); + + for (int x = 0; x < width; x++) + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int GetManhattanDistanceInRgbaSpace(ref ImageSharpRgba32 a, ref ImageSharpRgba32 b) + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int Diff(ushort a, ushort b) => Math.Abs(a - b); + + return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); + } + + int d = GetManhattanDistanceInRgbaSpace(ref aSpan[x], ref bSpan[x]); + + if (d > 0) + { + if (differences.Count < 20) + { + var diff = new PixelDifference(new Point(x, y), aSpan[x], bSpan[x]); + differences.Add(diff); + } + + totalDifference += d; + } + } + } + + float normalizedDifference = totalDifference / (actual.Width * (float)actual.Height); + normalizedDifference /= 4F * 65535F; + + if (normalizedDifference > threshold) + { + StringBuilder builder = new(); + builder.AppendLine($"The input images don't match. Normalized delta: {normalizedDifference}%"); + builder.AppendLine($"First {differences.Count} pixel deltas:"); + + foreach (var delta in differences) + { + builder.AppendLine(delta.ToString()); + } + + Assert.Fail(builder.ToString()); + } + } + + /// + /// A simple model repressenting info about a difference between two pixels. + /// + private readonly struct PixelDifference + { + public PixelDifference(Point position, ImageSharpRgba32 expected, ImageSharpRgba32 actual) + { + this.Position = position; + this.RedDifference = actual.R - expected.R; + this.GreenDifference = actual.G - expected.G; + this.BlueDifference = actual.B - expected.B; + this.AlphaDifference = actual.A - expected.A; + } + + public readonly Point Position; + + public readonly int RedDifference; + + public readonly int GreenDifference; + + public readonly int BlueDifference; + + public readonly int AlphaDifference; + + /// + public override string ToString() + { + return $"[delta({RedDifference},{GreenDifference},{BlueDifference},{AlphaDifference}) @ ({Position.X},{Position.Y})]"; + } + } + } + } +} diff --git a/tests/ComputeSharp.Tests/InitializationTests.cs b/tests/ComputeSharp.Tests/InitializationTests.cs index 2f9e563b..372cebba 100644 --- a/tests/ComputeSharp.Tests/InitializationTests.cs +++ b/tests/ComputeSharp.Tests/InitializationTests.cs @@ -1,5 +1,7 @@ using System; using System.Linq; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests @@ -14,23 +16,26 @@ namespace ComputeSharp.Tests Assert.IsTrue(Gpu.IsSupported); } - [TestMethod] - public void DefaultDeviceInfo() + [CombinatorialTestMethod] + [AllDevices] + public void DeviceInfo(Device device) { - Assert.IsTrue(Gpu.Default.Luid != default); - Assert.IsTrue(Gpu.Default.Name is { Length: > 0 }); + GraphicsDevice gpu = device.Get(); - if (Gpu.Default.IsHardwareAccelerated) + Assert.IsTrue(gpu.Luid != default); + Assert.IsTrue(gpu.Name is { Length: > 0 }); + + if (gpu.IsHardwareAccelerated) { - Assert.IsTrue(Gpu.Default.DedicatedMemorySize != 0); + Assert.IsTrue(gpu.DedicatedMemorySize != 0); } else { - Assert.IsTrue(Gpu.Default.SharedMemorySize != 0); + Assert.IsTrue(gpu.SharedMemorySize != 0); } - Assert.IsTrue(Gpu.Default.ComputeUnits != 0); - Assert.IsTrue(Gpu.Default.WavefrontSize != 0); + Assert.IsTrue(gpu.ComputeUnits != 0); + Assert.IsTrue(gpu.WavefrontSize != 0); } [TestMethod] diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs index e7cd0d74..5eaf8df4 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -3,6 +3,7 @@ using System.Numerics; using System.Reflection; using ComputeSharp.__Internals; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -12,43 +13,34 @@ namespace ComputeSharp.Tests { public partial class Texture2DTests { - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Vector4))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Float4))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R16), typeof(float))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R8), typeof(float))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Vector2))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Float2))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Vector2))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Float2))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Vector4))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Float4))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Vector4))] - [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Float4))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Vector4))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Float4))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R16), typeof(float))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R8), typeof(float))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Vector2))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Float2))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Vector2))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Float2))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Vector4))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Float4))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Vector4))] - [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Float4))] - public void Allocate_Uninitialized_Pixel_Ok(Type textureType, Type t, Type tPixel) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [Data(typeof(Bgra32), typeof(Vector4))] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(Vector2))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Vector2))] + [Data(typeof(Rg32), typeof(Float2))] + [Data(typeof(Rgba32), typeof(Vector4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Vector4))] + [Data(typeof(Rgba64), typeof(Float4))] + public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel) { - static void Test(Type textureType) + static void Test(Device device, Type textureType) where T : unmanaged, IUnorm where TPixel : unmanaged { - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 128, 128); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, 128, 128); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); Assert.AreEqual(texture.Height, 128); - Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + Assert.AreSame(texture.GraphicsDevice, device.Get()); if (textureType == typeof(ReadOnlyTexture2D<,>)) { @@ -62,7 +54,7 @@ namespace ComputeSharp.Tests try { - new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new[] { textureType }); + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); } catch (TargetInvocationException e) when (e.InnerException is not null) { diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs index f914c901..9b9f7295 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs @@ -1,109 +1,111 @@ using System; using System.Numerics; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; namespace ComputeSharp.Tests { public partial class Texture2DTests { - [TestMethod] - [DataRow(typeof(Bgra32), typeof(Vector4))] - [DataRow(typeof(Bgra32), typeof(Float4))] - [DataRow(typeof(R16), typeof(float))] - [DataRow(typeof(R8), typeof(float))] - [DataRow(typeof(Rg16), typeof(Vector2))] - [DataRow(typeof(Rg16), typeof(Float2))] - [DataRow(typeof(Rg32), typeof(Vector2))] - [DataRow(typeof(Rg32), typeof(Float2))] - [DataRow(typeof(Rgba32), typeof(Vector4))] - [DataRow(typeof(Rgba32), typeof(Float4))] - [DataRow(typeof(Rgba64), typeof(Vector4))] - [DataRow(typeof(Rgba64), typeof(Float4))] - public void Dispatch_NormalizedTexture2D(Type t, Type tPixel) + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Vector4))] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(Vector2))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Vector2))] + [Data(typeof(Rg32), typeof(Float2))] + [Data(typeof(Rgba32), typeof(Vector4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Vector4))] + [Data(typeof(Rgba64), typeof(Float4))] + public void Dispatch_NormalizedTexture2D(Device device, Type t, Type tPixel) { if (t == typeof(Bgra32) && tPixel == typeof(Vector4)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Bgra32_Vector4(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Bgra32_Vector4(source, destination)); } if (t == typeof(Bgra32) && tPixel == typeof(Float4)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Bgra32_Float4(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Bgra32_Float4(source, destination)); } if (t == typeof(R16) && tPixel == typeof(float)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_R16_float(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_R16_float(source, destination)); } if (t == typeof(R8) && tPixel == typeof(float)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_R8_float(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_R8_float(source, destination)); } if (t == typeof(Rg16) && tPixel == typeof(Vector2)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rg16_Vector2(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rg16_Vector2(source, destination)); } if (t == typeof(Rg16) && tPixel == typeof(Float2)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rg16_Float2(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rg16_Float2(source, destination)); } if (t == typeof(Rg32) && tPixel == typeof(Vector2)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rg32_Vector2(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rg32_Vector2(source, destination)); } if (t == typeof(Rg32) && tPixel == typeof(Float2)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rg32_Float2(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rg32_Float2(source, destination)); } if (t == typeof(Rgba32) && tPixel == typeof(Vector4)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rgba32_Vector4(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rgba32_Vector4(source, destination)); } if (t == typeof(Rgba32) && tPixel == typeof(Float4)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rgba32_Float4(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rgba32_Float4(source, destination)); } if (t == typeof(Rgba64) && tPixel == typeof(Vector4)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rgba64_Vector4(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rgba64_Vector4(source, destination)); } if (t == typeof(Rgba64) && tPixel == typeof(Float4)) { - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_Rgba64_Float4(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_Rgba64_Float4(source, destination)); } } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt index 991ce833..e0de4242 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt @@ -25,22 +25,24 @@ IEnumerable<(string T, string TPixel)> EnumerateTypes() #> using System; using System.Numerics; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; namespace ComputeSharp.Tests { public partial class Texture2DTests { - [TestMethod] + [CombinatorialTestMethod] + [AllDevices] <# foreach (var (t, tPixel) in EnumerateTypes()) { #> - [DataRow(typeof(<#=t#>), typeof(<#=tPixel#>))] + [Data(typeof(<#=t#>), typeof(<#=tPixel#>))] <# } #> - public void Dispatch_NormalizedTexture2D(Type t, Type tPixel) + public void Dispatch_NormalizedTexture2D(Device device, Type t, Type tPixel) { <# foreach (var (t, tPixel) in EnumerateTypes()) @@ -48,10 +50,10 @@ foreach (var (t, tPixel) in EnumerateTypes()) #> if (t == typeof(<#=t#>) && tPixel == typeof(<#=tPixel#>)) { - using ReadOnlyTexture2D<<#=t#>, <#=tPixel#>> source = Gpu.Default.AllocateReadOnlyTexture2D<<#=t#>, <#=tPixel#>>(128, 128); - using ReadWriteTexture2D<<#=t#>, <#=tPixel#>> destination = Gpu.Default.AllocateReadWriteTexture2D<<#=t#>, <#=tPixel#>>(128, 128); + using ReadOnlyTexture2D<<#=t#>, <#=tPixel#>> source = device.Get().AllocateReadOnlyTexture2D<<#=t#>, <#=tPixel#>>(128, 128); + using ReadWriteTexture2D<<#=t#>, <#=tPixel#>> destination = device.Get().AllocateReadWriteTexture2D<<#=t#>, <#=tPixel#>>(128, 128); - Gpu.Default.For(128, 128, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination)); + device.Get().For(128, 128, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination)); } <# } diff --git a/tests/ComputeSharp.Tests/Texture2DTests.cs b/tests/ComputeSharp.Tests/Texture2DTests.cs index 61e93068..a0192a65 100644 --- a/tests/ComputeSharp.Tests/Texture2DTests.cs +++ b/tests/ComputeSharp.Tests/Texture2DTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.Toolkit.HighPerformance.Memory; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,47 +12,48 @@ namespace ComputeSharp.Tests [TestCategory("Texture2D")] public partial class Texture2DTests { - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<>))] - [DataRow(typeof(ReadWriteTexture2D<>))] - public void Allocate_Uninitialized_Ok(Type textureType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + public void Allocate_Uninitialized_Ok(Device device, Type textureType) { - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 128, 128); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, 128, 128); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); Assert.AreEqual(texture.Height, 128); - Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + Assert.AreSame(texture.GraphicsDevice, device.Get()); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<>), 128, -14253)] - [DataRow(typeof(ReadOnlyTexture2D<>), 128, -1)] - [DataRow(typeof(ReadOnlyTexture2D<>), 0, -4314)] - [DataRow(typeof(ReadOnlyTexture2D<>), -14, -53)] - [DataRow(typeof(ReadWriteTexture2D<>), 128, -14253)] - [DataRow(typeof(ReadWriteTexture2D<>), 128, -1)] - [DataRow(typeof(ReadWriteTexture2D<>), 0, -4314)] - [DataRow(typeof(ReadWriteTexture2D<>), -14, -53)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(128, -14253)] + [Data(128, -1)] + [Data(0, -4314)] + [Data(-14, -53)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Type textureType, int width, int height) + public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width, int height) { - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, width, height); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, width, height); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<>))] - [DataRow(typeof(ReadWriteTexture2D<>))] - public void Allocate_FromArray(Type textureType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + public void Allocate_FromArray(Device device, Type textureType) { float[] data = Enumerable.Range(0, 128 * 128).Select(static i => (float)i).ToArray(); - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, data, 128, 128); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, data, 128, 128); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); Assert.AreEqual(texture.Height, 128); - Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + Assert.AreSame(texture.GraphicsDevice, device.Get()); float[,] result = texture.ToArray(); @@ -60,43 +62,38 @@ namespace ComputeSharp.Tests Assert.IsTrue(data.SequenceEqual(result.Cast())); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<>))] - [DataRow(typeof(ReadWriteTexture2D<>))] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Type textureType) + public void UsageAfterDispose(Device device, Type textureType) { - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 10, 10); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, 10, 10); texture.Dispose(); _ = texture.ToArray(); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<>), 0, 0, 64, 64)] - [DataRow(typeof(ReadOnlyTexture2D<>), 0, 14, 64, 50)] - [DataRow(typeof(ReadOnlyTexture2D<>), 14, 0, 50, 64)] - [DataRow(typeof(ReadOnlyTexture2D<>), 10, 10, 54, 54)] - [DataRow(typeof(ReadOnlyTexture2D<>), 20, 20, 32, 27)] - [DataRow(typeof(ReadOnlyTexture2D<>), 60, 0, 4, 64)] - [DataRow(typeof(ReadOnlyTexture2D<>), 0, 60, 64, 4)] - [DataRow(typeof(ReadOnlyTexture2D<>), 63, 2, 1, 60)] - [DataRow(typeof(ReadOnlyTexture2D<>), 2, 63, 60, 1)] - [DataRow(typeof(ReadWriteTexture2D<>), 0, 0, 64, 64)] - [DataRow(typeof(ReadWriteTexture2D<>), 0, 14, 64, 50)] - [DataRow(typeof(ReadWriteTexture2D<>), 14, 0, 50, 64)] - [DataRow(typeof(ReadWriteTexture2D<>), 10, 10, 54, 54)] - [DataRow(typeof(ReadWriteTexture2D<>), 20, 20, 32, 27)] - [DataRow(typeof(ReadWriteTexture2D<>), 60, 0, 4, 64)] - [DataRow(typeof(ReadWriteTexture2D<>), 0, 60, 64, 4)] - [DataRow(typeof(ReadWriteTexture2D<>), 63, 2, 1, 60)] - [DataRow(typeof(ReadWriteTexture2D<>), 2, 63, 60, 1)] - public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int width, int height) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(0, 0, 64, 64)] + [Data(0, 14, 64, 50)] + [Data(14, 0, 50, 64)] + [Data(10, 10, 54, 54)] + [Data(20, 20, 32, 27)] + [Data(60, 0, 4, 64)] + [Data(0, 60, 64, 4)] + [Data(63, 2, 1, 60)] + [Data(2, 63, 60, 1)] + public void GetData_RangeToVoid_Ok(Device device, Type textureType, int x, int y, int width, int height) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, array, 64, 64); float[] result = new float[width * height]; @@ -109,44 +106,40 @@ namespace ComputeSharp.Tests CollectionAssert.AreEqual(expected.ToArray(), data.ToArray()); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture2D<>), 0, -1, 50, 50)] - [DataRow(typeof(ReadOnlyTexture2D<>), -1, 0, 50, 50)] - [DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, -1, 50)] - [DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, 20, -1)] - [DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 20, 50)] - [DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 60, 20)] - [DataRow(typeof(ReadOnlyTexture2D<>), 80, 20, 40, 20)] - [DataRow(typeof(ReadOnlyTexture2D<>), 0, 80, 40, 20)] - [DataRow(typeof(ReadWriteTexture2D<>), 0, -1, 50, 50)] - [DataRow(typeof(ReadWriteTexture2D<>), -1, 0, 50, 50)] - [DataRow(typeof(ReadWriteTexture2D<>), 12, 0, -1, 50)] - [DataRow(typeof(ReadWriteTexture2D<>), 12, 0, 20, -1)] - [DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 20, 50)] - [DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 60, 20)] - [DataRow(typeof(ReadWriteTexture2D<>), 80, 20, 40, 20)] - [DataRow(typeof(ReadWriteTexture2D<>), 0, 80, 40, 20)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<>))] + [Resource(typeof(ReadWriteTexture2D<>))] + [Data(0, -1, 50, 50)] + [Data(-1, 0, 50, 50)] + [Data(12, 0, -1, 50)] + [Data(12, 0, 20, -1)] + [Data(12, 20, 20, 50)] + [Data(12, 20, 60, 20)] + [Data(80, 20, 40, 20)] + [Data(0, 80, 40, 20)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int width, int height) + public void GetData_RangeToVoid_Fail(Device device, Type textureType, int x, int y, int width, int height) { float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray(); - using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64); + using Texture2D texture = device.Get().AllocateTexture2D(textureType, array, 64, 64); float[] result = new float[4096]; texture.CopyTo(result, x, y, width, height); } - [TestMethod] - public void Dispatch_ReadOnlyTexture2D() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ReadOnlyTexture2D(Device device) { int[] data = Enumerable.Range(0, 32 * 32).ToArray(); - using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(data, 32, 32); - using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + using ReadOnlyTexture2D source = device.Get().AllocateReadOnlyTexture2D(data, 32, 32); + using ReadWriteBuffer destination = device.Get().AllocateReadWriteBuffer(data.Length); - Gpu.Default.For(source.Width, source.Height, new ReadOnlyTexture2DKernel(source, destination)); + device.Get().For(source.Width, source.Height, new ReadOnlyTexture2DKernel(source, destination)); int[] result = destination.ToArray(); @@ -165,15 +158,16 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Dispatch_ReadWriteTexture2D() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ReadWriteTexture2D(Device device) { int[] data = Enumerable.Range(0, 32 * 32).ToArray(); - using ReadWriteTexture2D source = Gpu.Default.AllocateReadWriteTexture2D(data, 32, 32); - using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(32, 32); + using ReadWriteTexture2D source = device.Get().AllocateReadWriteTexture2D(data, 32, 32); + using ReadWriteTexture2D destination = device.Get().AllocateReadWriteTexture2D(32, 32); - Gpu.Default.For(source.Width, source.Height, new ReadWriteTexture2DKernel(source, destination)); + device.Get().For(source.Width, source.Height, new ReadWriteTexture2DKernel(source, destination)); int[] result = new int[data.Length]; diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs index c4c9e3b4..b2827155 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs @@ -3,6 +3,7 @@ using System.Numerics; using System.Reflection; using ComputeSharp.__Internals; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -12,44 +13,35 @@ namespace ComputeSharp.Tests { public partial class Texture3DTests { - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Vector4))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Float4))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R16), typeof(float))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R8), typeof(float))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Vector2))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Float2))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Vector2))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Float2))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Vector4))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Float4))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Vector4))] - [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Float4))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Vector4))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Float4))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R16), typeof(float))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R8), typeof(float))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Vector2))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Float2))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Vector2))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Float2))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Vector4))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Float4))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Vector4))] - [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Float4))] - public void Allocate_Uninitialized_Pixel_Ok(Type textureType, Type t, Type tPixel) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<,>))] + [Resource(typeof(ReadWriteTexture3D<,>))] + [Data(typeof(Bgra32), typeof(Vector4))] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(Vector2))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Vector2))] + [Data(typeof(Rg32), typeof(Float2))] + [Data(typeof(Rgba32), typeof(Vector4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Vector4))] + [Data(typeof(Rgba64), typeof(Float4))] + public void Allocate_Uninitialized_Pixel_Ok(Device device, Type textureType, Type t, Type tPixel) { - static void Test(Type textureType) + static void Test(Device device, Type textureType) where T : unmanaged, IUnorm where TPixel : unmanaged { - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 128, 128, 2); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, 128, 128, 2); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 128); Assert.AreEqual(texture.Height, 128); Assert.AreEqual(texture.Depth, 2); - Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + Assert.AreSame(texture.GraphicsDevice, device.Get()); if (textureType == typeof(ReadOnlyTexture3D<,>)) { @@ -63,7 +55,7 @@ namespace ComputeSharp.Tests try { - new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new[] { textureType }); + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new object[] { device, textureType }); } catch (TargetInvocationException e) when (e.InnerException is not null) { diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs index debd29cf..134a4ef4 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs @@ -1,109 +1,111 @@ using System; using System.Numerics; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; namespace ComputeSharp.Tests { public partial class Texture3DTests { - [TestMethod] - [DataRow(typeof(Bgra32), typeof(Vector4))] - [DataRow(typeof(Bgra32), typeof(Float4))] - [DataRow(typeof(R16), typeof(float))] - [DataRow(typeof(R8), typeof(float))] - [DataRow(typeof(Rg16), typeof(Vector2))] - [DataRow(typeof(Rg16), typeof(Float2))] - [DataRow(typeof(Rg32), typeof(Vector2))] - [DataRow(typeof(Rg32), typeof(Float2))] - [DataRow(typeof(Rgba32), typeof(Vector4))] - [DataRow(typeof(Rgba32), typeof(Float4))] - [DataRow(typeof(Rgba64), typeof(Vector4))] - [DataRow(typeof(Rgba64), typeof(Float4))] - public void Dispatch_NormalizedTexture3D(Type t, Type tPixel) + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Vector4))] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(Rg16), typeof(Vector2))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Vector2))] + [Data(typeof(Rg32), typeof(Float2))] + [Data(typeof(Rgba32), typeof(Vector4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Vector4))] + [Data(typeof(Rgba64), typeof(Float4))] + public void Dispatch_NormalizedTexture3D(Device device, Type t, Type tPixel) { if (t == typeof(Bgra32) && tPixel == typeof(Vector4)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Bgra32_Vector4(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Bgra32_Vector4(source, destination)); } if (t == typeof(Bgra32) && tPixel == typeof(Float4)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Bgra32_Float4(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Bgra32_Float4(source, destination)); } if (t == typeof(R16) && tPixel == typeof(float)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_R16_float(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_R16_float(source, destination)); } if (t == typeof(R8) && tPixel == typeof(float)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_R8_float(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_R8_float(source, destination)); } if (t == typeof(Rg16) && tPixel == typeof(Vector2)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg16_Vector2(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rg16_Vector2(source, destination)); } if (t == typeof(Rg16) && tPixel == typeof(Float2)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg16_Float2(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rg16_Float2(source, destination)); } if (t == typeof(Rg32) && tPixel == typeof(Vector2)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg32_Vector2(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rg32_Vector2(source, destination)); } if (t == typeof(Rg32) && tPixel == typeof(Float2)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg32_Float2(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rg32_Float2(source, destination)); } if (t == typeof(Rgba32) && tPixel == typeof(Vector4)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba32_Vector4(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rgba32_Vector4(source, destination)); } if (t == typeof(Rgba32) && tPixel == typeof(Float4)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba32_Float4(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rgba32_Float4(source, destination)); } if (t == typeof(Rgba64) && tPixel == typeof(Vector4)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba64_Vector4(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rgba64_Vector4(source, destination)); } if (t == typeof(Rgba64) && tPixel == typeof(Float4)) { - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba64_Float4(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_Rgba64_Float4(source, destination)); } } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt index 57be04b8..97dfb563 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt @@ -25,22 +25,24 @@ IEnumerable<(string T, string TPixel)> EnumerateTypes() #> using System; using System.Numerics; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; namespace ComputeSharp.Tests { public partial class Texture3DTests { - [TestMethod] + [CombinatorialTestMethod] + [AllDevices] <# foreach (var (t, tPixel) in EnumerateTypes()) { #> - [DataRow(typeof(<#=t#>), typeof(<#=tPixel#>))] + [Data(typeof(<#=t#>), typeof(<#=tPixel#>))] <# } #> - public void Dispatch_NormalizedTexture3D(Type t, Type tPixel) + public void Dispatch_NormalizedTexture3D(Device device, Type t, Type tPixel) { <# foreach (var (t, tPixel) in EnumerateTypes()) @@ -48,10 +50,10 @@ foreach (var (t, tPixel) in EnumerateTypes()) #> if (t == typeof(<#=t#>) && tPixel == typeof(<#=tPixel#>)) { - using ReadOnlyTexture3D<<#=t#>, <#=tPixel#>> source = Gpu.Default.AllocateReadOnlyTexture3D<<#=t#>, <#=tPixel#>>(128, 128, 2); - using ReadWriteTexture3D<<#=t#>, <#=tPixel#>> destination = Gpu.Default.AllocateReadWriteTexture3D<<#=t#>, <#=tPixel#>>(128, 128, 2); + using ReadOnlyTexture3D<<#=t#>, <#=tPixel#>> source = device.Get().AllocateReadOnlyTexture3D<<#=t#>, <#=tPixel#>>(128, 128, 2); + using ReadWriteTexture3D<<#=t#>, <#=tPixel#>> destination = device.Get().AllocateReadWriteTexture3D<<#=t#>, <#=tPixel#>>(128, 128, 2); - Gpu.Default.For(128, 128, 2, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination)); + device.Get().For(128, 128, 2, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination)); } <# } diff --git a/tests/ComputeSharp.Tests/Texture3DTests.cs b/tests/ComputeSharp.Tests/Texture3DTests.cs index 3c8e0ec1..d535bc97 100644 --- a/tests/ComputeSharp.Tests/Texture3DTests.cs +++ b/tests/ComputeSharp.Tests/Texture3DTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.Toolkit.HighPerformance.Extensions; using Microsoft.Toolkit.HighPerformance.Memory; @@ -12,59 +13,56 @@ namespace ComputeSharp.Tests [TestCategory("Texture3D")] public partial class Texture3DTests { - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<>))] - [DataRow(typeof(ReadWriteTexture3D<>))] - public void Allocate_Uninitialized_Ok(Type textureType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + public void Allocate_Uninitialized_Ok(Device device, Type textureType) { - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 64, 64, 12); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, 64, 64, 12); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, 64); Assert.AreEqual(texture.Height, 64); Assert.AreEqual(texture.Depth, 12); - Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + Assert.AreSame(texture.GraphicsDevice, device.Get()); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<>), 128, -14253, 4)] - [DataRow(typeof(ReadOnlyTexture3D<>), 128, -1, 4)] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, -4314, 4)] - [DataRow(typeof(ReadOnlyTexture3D<>), -14, -53, 4)] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 12, -1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 2, 4, ushort.MaxValue + 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 128, -14253, 4)] - [DataRow(typeof(ReadWriteTexture3D<>), 128, -1, 4)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, -4314, 4)] - [DataRow(typeof(ReadWriteTexture3D<>), -14, -53, 4)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 12, -1)] - [DataRow(typeof(ReadWriteTexture3D<>), 2, 4, ushort.MaxValue + 1)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(128, -14253, 4)] + [Data(128, -1, 4)] + [Data(0, -4314, 4)] + [Data(-14, -53, 4)] + [Data(12, 12, -1)] + [Data(2, 4, ushort.MaxValue + 1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Type textureType, int width, int height, int depth) + public void Allocate_Uninitialized_Fail(Device device, Type textureType, int width, int height, int depth) { - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, width, height, depth); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, width, height, depth); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 12, 4)] - [DataRow(typeof(ReadOnlyTexture3D<>), 2, 12, 4)] - [DataRow(typeof(ReadOnlyTexture3D<>), 64, 64, 1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 64, 27, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 12, 4)] - [DataRow(typeof(ReadWriteTexture3D<>), 2, 12, 4)] - [DataRow(typeof(ReadWriteTexture3D<>), 64, 64, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 64, 27, 3)] - public void Allocate_FromArray(Type textureType, int width, int height, int depth) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(12, 12, 4)] + [Data(2, 12, 4)] + [Data(64, 64, 1)] + [Data(64, 27, 3)] + public void Allocate_FromArray(Device device, Type textureType, int width, int height, int depth) { float[] data = Enumerable.Range(0, width * height * depth).Select(static i => (float)i).ToArray(); - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, data, width, height, depth); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, data, width, height, depth); Assert.IsNotNull(texture); Assert.AreEqual(texture.Width, width); Assert.AreEqual(texture.Height, height); Assert.AreEqual(texture.Depth, depth); - Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + Assert.AreSame(texture.GraphicsDevice, device.Get()); float[,,] result = texture.ToArray(); @@ -84,48 +82,42 @@ namespace ComputeSharp.Tests CollectionAssert.AreEqual(flat, data); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<>))] - [DataRow(typeof(ReadWriteTexture3D<>))] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Type textureType) + public void UsageAfterDispose(Device device, Type textureType) { - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 10, 10, 4); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, 10, 10, 4); texture.Dispose(); _ = texture.ToArray(); } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, 0, 0, 64, 64, 3)] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, 14, 0, 64, 50, 3)] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, 14, 1, 64, 50, 2)] - [DataRow(typeof(ReadOnlyTexture3D<>), 14, 0, 1, 50, 64, 2)] - [DataRow(typeof(ReadOnlyTexture3D<>), 10, 10, 2, 54, 54, 1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 20, 20, 0, 32, 27, 3)] - [DataRow(typeof(ReadOnlyTexture3D<>), 60, 0, 0, 4, 64, 3)] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, 60, 2, 64, 4, 1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 63, 2, 0, 1, 60, 2)] - [DataRow(typeof(ReadOnlyTexture3D<>), 2, 63, 1, 60, 1, 2)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, 0, 0, 64, 64, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, 14, 0, 64, 50, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, 14, 1, 64, 50, 2)] - [DataRow(typeof(ReadWriteTexture3D<>), 14, 0, 1, 50, 64, 2)] - [DataRow(typeof(ReadWriteTexture3D<>), 10, 10, 2, 54, 54, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 20, 20, 0, 32, 27, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 60, 0, 0, 4, 64, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, 60, 2, 64, 4, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 63, 2, 0, 1, 60, 2)] - [DataRow(typeof(ReadWriteTexture3D<>), 2, 63, 1, 60, 1, 2)] - public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int z, int width, int height, int depth) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(0, 0, 0, 64, 64, 3)] + [Data(0, 14, 0, 64, 50, 3)] + [Data(0, 14, 1, 64, 50, 2)] + [Data(14, 0, 1, 50, 64, 2)] + [Data(10, 10, 2, 54, 54, 1)] + [Data(20, 20, 0, 32, 27, 3)] + [Data(60, 0, 0, 4, 64, 3)] + [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) { int[,,] array = new int[3, 64, 64]; foreach (var item in array.AsSpan().Enumerate()) item.Value = item.Index; - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, array); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, array); int[,,] result = new int[depth, height, width]; @@ -141,46 +133,41 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, -1, 0, 50, 50, 2)] - [DataRow(typeof(ReadOnlyTexture3D<>), -1, 0, 0, 50, 50, 3)] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 0, -1, 50, 1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 0, 20, 50, 0)] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 0, 1, 20, -1, 2)] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 20, 2, 20, 50, 1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 12, 20, 0, 60, 20, 3)] - [DataRow(typeof(ReadOnlyTexture3D<>), 80, 20, 2, 40, 20, 1)] - [DataRow(typeof(ReadOnlyTexture3D<>), 0, 80, 2, 40, 20, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, -1, 0, 50, 50, 2)] - [DataRow(typeof(ReadWriteTexture3D<>), -1, 0, 0, 50, 50, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 0, -1, 50, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 0, 20, 50, 0)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 0, 1, 20, -1, 2)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 20, 2, 20, 50, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 12, 20, 0, 60, 20, 3)] - [DataRow(typeof(ReadWriteTexture3D<>), 80, 20, 2, 40, 20, 1)] - [DataRow(typeof(ReadWriteTexture3D<>), 0, 80, 2, 40, 20, 1)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture3D<>))] + [Resource(typeof(ReadWriteTexture3D<>))] + [Data(0, -1, 0, 50, 50, 2)] + [Data(-1, 0, 0, 50, 50, 3)] + [Data(12, 0, 0, -1, 50, 1)] + [Data(12, 0, 0, 20, 50, 0)] + [Data(12, 0, 1, 20, -1, 2)] + [Data(12, 20, 2, 20, 50, 1)] + [Data(12, 20, 0, 60, 20, 3)] + [Data(80, 20, 2, 40, 20, 1)] + [Data(0, 80, 2, 40, 20, 1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int z, int width, int height, int depth) + public void GetData_RangeToVoid_Fail(Device device, Type textureType, int x, int y, int z, int width, int height, int depth) { float[] array = Enumerable.Range(0, 64 * 64 * 3).Select(static i => (float)i).ToArray(); - using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, array, 64, 64, 3); + using Texture3D texture = device.Get().AllocateTexture3D(textureType, array, 64, 64, 3); float[] result = new float[array.Length]; texture.CopyTo(result, x, y, z, width, height, depth); } - [TestMethod] - public void Dispatch_ReadOnlyTexture3D() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ReadOnlyTexture3D(Device device) { int[] data = Enumerable.Range(0, 32 * 32 * 3).ToArray(); - using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(data, 32, 32, 3); - using ReadWriteBuffer destination = Gpu.Default.AllocateReadWriteBuffer(data.Length); + using ReadOnlyTexture3D source = device.Get().AllocateReadOnlyTexture3D(data, 32, 32, 3); + using ReadWriteBuffer destination = device.Get().AllocateReadWriteBuffer(data.Length); - Gpu.Default.For(source.Width, source.Height, source.Depth, new ReadOnlyTexture3DKernel(source, destination)); + device.Get().For(source.Width, source.Height, source.Depth, new ReadOnlyTexture3DKernel(source, destination)); int[] result = destination.ToArray(); @@ -199,15 +186,16 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Dispatch_ReadWriteTexture2D() + [CombinatorialTestMethod] + [AllDevices] + public void Dispatch_ReadWriteTexture2D(Device device) { int[] data = Enumerable.Range(0, 32 * 32 * 3).ToArray(); - using ReadWriteTexture3D source = Gpu.Default.AllocateReadWriteTexture3D(data, 32, 32, 3); - using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(32, 32, 3); + using ReadWriteTexture3D source = device.Get().AllocateReadWriteTexture3D(data, 32, 32, 3); + using ReadWriteTexture3D destination = device.Get().AllocateReadWriteTexture3D(32, 32, 3); - Gpu.Default.For(source.Width, source.Height, source.Depth, new ReadWriteTexture3DKernel(source, destination)); + device.Get().For(source.Width, source.Height, source.Depth, new ReadWriteTexture3DKernel(source, destination)); int[] result = new int[data.Length]; diff --git a/tests/ComputeSharp.Tests/TransferBufferTests.cs b/tests/ComputeSharp.Tests/TransferBufferTests.cs index 2367fc40..d1c478de 100644 --- a/tests/ComputeSharp.Tests/TransferBufferTests.cs +++ b/tests/ComputeSharp.Tests/TransferBufferTests.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Runtime.CompilerServices; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.Toolkit.HighPerformance.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -12,12 +13,13 @@ namespace ComputeSharp.Tests [TestCategory("TransferBuffer")] public partial class TransferBufferTests { - [TestMethod] - [DataRow(typeof(UploadBuffer<>))] - [DataRow(typeof(ReadBackBuffer<>))] - public unsafe void Allocate_Uninitialized_Ok(Type bufferType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType) { - using TransferBuffer buffer = Gpu.Default.AllocateTransferBuffer(bufferType, 128); + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, 128); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Length, 128); @@ -25,41 +27,44 @@ namespace ComputeSharp.Tests Assert.AreEqual(buffer.Span.Length, 128); Assert.IsTrue(Unsafe.AreSame(ref buffer.Memory.Span[0], ref buffer.Span[0])); Assert.IsTrue(Unsafe.AreSame(ref *(float*)buffer.Memory.Pin().Pointer, ref buffer.Span[0])); - Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + Assert.AreSame(buffer.GraphicsDevice, device.Get()); } - [TestMethod] - [DataRow(typeof(UploadBuffer<>), -247824)] - [DataRow(typeof(UploadBuffer<>), -1)] - [DataRow(typeof(ReadBackBuffer<>), -247824)] - [DataRow(typeof(ReadBackBuffer<>), -1)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] + [Data(-247824)] + [Data(-1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Type bufferType, int length) + public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int length) { - using TransferBuffer buffer = Gpu.Default.AllocateTransferBuffer(bufferType, length); + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, length); } - [TestMethod] - [DataRow(typeof(UploadBuffer<>))] - [DataRow(typeof(ReadBackBuffer<>))] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadBuffer<>))] + [Resource(typeof(ReadBackBuffer<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Type bufferType) + public void UsageAfterDispose(Device device, Type bufferType) { - using TransferBuffer buffer = Gpu.Default.AllocateTransferBuffer(bufferType, 128); + using TransferBuffer buffer = device.Get().AllocateTransferBuffer(bufferType, 128); buffer.Dispose(); _ = buffer.Span; } - [TestMethod] - public void Allocate_UploadBuffer_Copy_Full() + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_UploadBuffer_Copy_Full(Device device) { - using UploadBuffer uploadBuffer = Gpu.Default.AllocateUploadBuffer(4096); + using UploadBuffer uploadBuffer = device.Get().AllocateUploadBuffer(4096); new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); - using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(uploadBuffer.Length); + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(uploadBuffer.Length); uploadBuffer.CopyTo(readOnlyBuffer); @@ -69,18 +74,19 @@ namespace ComputeSharp.Tests Assert.IsTrue(uploadBuffer.Span.SequenceEqual(result)); } - [TestMethod] - [DataRow(0, 0, 4096)] - [DataRow(128, 0, 2048)] - [DataRow(0, 128, 2048)] - [DataRow(97, 33, 512)] - public void Allocate_UploadBuffer_Copy_Range(int destinationOffset, int bufferOffset, int count) + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 4096)] + [Data(128, 0, 2048)] + [Data(0, 128, 2048)] + [Data(97, 33, 512)] + public void Allocate_UploadBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count) { - using UploadBuffer uploadBuffer = Gpu.Default.AllocateUploadBuffer(4096); + using UploadBuffer uploadBuffer = device.Get().AllocateUploadBuffer(4096); new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); - using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(uploadBuffer.Length); + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(uploadBuffer.Length); uploadBuffer.CopyTo(readOnlyBuffer, destinationOffset, bufferOffset, count); @@ -90,15 +96,16 @@ namespace ComputeSharp.Tests Assert.IsTrue(uploadBuffer.Span.Slice(bufferOffset, count).SequenceEqual(result)); } - [TestMethod] - public void Allocate_ReadBackBuffer_Copy_Full() + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_ReadBackBuffer_Copy_Full(Device device) { int[] source = new int[4096]; new Random(42).NextBytes(source.AsSpan().AsBytes()); - using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(source); - using ReadBackBuffer readBackBuffer = Gpu.Default.AllocateReadBackBuffer(readOnlyBuffer.Length); + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source); + using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(readOnlyBuffer.Length); readOnlyBuffer.CopyTo(readBackBuffer); @@ -106,19 +113,20 @@ namespace ComputeSharp.Tests Assert.IsTrue(source.AsSpan().SequenceEqual(readBackBuffer.Span)); } - [TestMethod] - [DataRow(0, 0, 4096)] - [DataRow(128, 0, 2048)] - [DataRow(0, 128, 2048)] - [DataRow(97, 33, 512)] - public void Allocate_ReadBackBuffer_Copy_Range(int destinationOffset, int bufferOffset, int count) + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 4096)] + [Data(128, 0, 2048)] + [Data(0, 128, 2048)] + [Data(97, 33, 512)] + public void Allocate_ReadBackBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count) { int[] source = new int[4096]; new Random(42).NextBytes(source.AsSpan().AsBytes()); - using ReadOnlyBuffer readOnlyBuffer = Gpu.Default.AllocateReadOnlyBuffer(source); - using ReadBackBuffer readBackBuffer = Gpu.Default.AllocateReadBackBuffer(readOnlyBuffer.Length); + using ReadOnlyBuffer readOnlyBuffer = device.Get().AllocateReadOnlyBuffer(source); + using ReadBackBuffer readBackBuffer = device.Get().AllocateReadBackBuffer(readOnlyBuffer.Length); readOnlyBuffer.CopyTo(readBackBuffer, destinationOffset, bufferOffset, count); diff --git a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs index 6160ea8f..316962af 100644 --- a/tests/ComputeSharp.Tests/TransferTexture2DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture2DTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.Toolkit.HighPerformance.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,60 +12,62 @@ namespace ComputeSharp.Tests [TestCategory("TransferTexture2D")] public partial class TransferTexture2DTests { - [TestMethod] - [DataRow(typeof(UploadTexture2D<>))] - [DataRow(typeof(ReadBackTexture2D<>))] - public unsafe void Allocate_Uninitialized_Ok(Type bufferType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType) { - using TransferTexture2D buffer = Gpu.Default.AllocateTransferTexture2D(bufferType, 128, 256); + using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, 128, 256); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Width, 128); Assert.AreEqual(buffer.Height, 256); Assert.AreEqual(buffer.View.Width, 128); Assert.AreEqual(buffer.View.Height, 256); - Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + Assert.AreSame(buffer.GraphicsDevice, device.Get()); } - [TestMethod] - [DataRow(typeof(UploadTexture2D<>), 128, -14253)] - [DataRow(typeof(UploadTexture2D<>), 128, -1)] - [DataRow(typeof(UploadTexture2D<>), 0, -4314)] - [DataRow(typeof(UploadTexture2D<>), -14, -53)] - [DataRow(typeof(ReadBackTexture2D<>), 128, -14253)] - [DataRow(typeof(ReadBackTexture2D<>), 128, -1)] - [DataRow(typeof(ReadBackTexture2D<>), 0, -4314)] - [DataRow(typeof(ReadBackTexture2D<>), -14, -53)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] + [Data(128, -14253)] + [Data(128, -1)] + [Data(0, -4314)] + [Data(-14, -53)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Type bufferType, int width, int height) + public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height) { - using TransferTexture2D buffer = Gpu.Default.AllocateTransferTexture2D(bufferType, width, height); + using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, width, height); } - [TestMethod] - [DataRow(typeof(UploadTexture2D<>))] - [DataRow(typeof(ReadBackTexture2D<>))] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture2D<>))] + [Resource(typeof(ReadBackTexture2D<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Type bufferType) + public void UsageAfterDispose(Device device, Type bufferType) { - using TransferTexture2D buffer = Gpu.Default.AllocateTransferTexture2D(bufferType, 32, 32); + using TransferTexture2D buffer = device.Get().AllocateTransferTexture2D(bufferType, 32, 32); buffer.Dispose(); _ = buffer.View; } - [TestMethod] - public void Allocate_UploadTexture2D_Copy_Full() + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_UploadTexture2D_Copy_Full(Device device) { - using UploadTexture2D uploadTexture2D = Gpu.Default.AllocateUploadTexture2D(256, 256); + using UploadTexture2D uploadTexture2D = device.Get().AllocateUploadTexture2D(256, 256); for (int i = 0; i < uploadTexture2D.Height; i++) { new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes()); } - using ReadOnlyTexture2D readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(uploadTexture2D.Width, uploadTexture2D.Height); + using ReadOnlyTexture2D readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(uploadTexture2D.Width, uploadTexture2D.Height); uploadTexture2D.CopyTo(readOnlyTexture2D); @@ -79,21 +82,22 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(0, 0, 2048, 2048)] - [DataRow(128, 0, 1813, 2048)] - [DataRow(0, 128, 2000, 944)] - [DataRow(97, 33, 512, 794)] - public void Allocate_UploadTexture2D_Copy_Range(int x, int y, int width, int height) + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 2048, 2048)] + [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) { - using UploadTexture2D uploadTexture2D = Gpu.Default.AllocateUploadTexture2D(2048, 2048); + using UploadTexture2D uploadTexture2D = device.Get().AllocateUploadTexture2D(2048, 2048); for (int i = 0; i < uploadTexture2D.Height; i++) { new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes()); } - using ReadOnlyTexture2D readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(uploadTexture2D.Width, uploadTexture2D.Height); + using ReadOnlyTexture2D readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(uploadTexture2D.Width, uploadTexture2D.Height); uploadTexture2D.CopyTo(readOnlyTexture2D, x, y, width, height); @@ -108,8 +112,9 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Allocate_ReadBackTexture2D_Copy_Full() + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_ReadBackTexture2D_Copy_Full(Device device) { int[,] source = new int[256, 256]; @@ -118,8 +123,8 @@ namespace ComputeSharp.Tests new Random(42).NextBytes(source.GetRowSpan(i).AsBytes()); } - using ReadOnlyTexture2D readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(source); - using ReadBackTexture2D readBackTexture2D = Gpu.Default.AllocateReadBackTexture2D(256, 256); + using ReadOnlyTexture2D readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); + using ReadBackTexture2D readBackTexture2D = device.Get().AllocateReadBackTexture2D(256, 256); readOnlyTexture2D.CopyTo(readBackTexture2D); @@ -132,12 +137,13 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(0, 0, 2048, 2048)] - [DataRow(128, 0, 1813, 2048)] - [DataRow(0, 128, 2000, 944)] - [DataRow(97, 33, 512, 794)] - public void Allocate_ReadBackTexture2D_Copy_Range(int x, int y, int width, int height) + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 2048, 2048)] + [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) { int[,] source = new int[2048, 2048]; @@ -146,8 +152,8 @@ namespace ComputeSharp.Tests new Random(42).NextBytes(source.GetRowSpan(i).AsBytes()); } - using ReadOnlyTexture2D readOnlyTexture2D = Gpu.Default.AllocateReadOnlyTexture2D(source); - using ReadBackTexture2D readBackTexture2D = Gpu.Default.AllocateReadBackTexture2D(2048, 2048); + using ReadOnlyTexture2D readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); + using ReadBackTexture2D readBackTexture2D = device.Get().AllocateReadBackTexture2D(2048, 2048); readOnlyTexture2D.CopyTo(readBackTexture2D, x, y, width, height); diff --git a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs index cede4b3a..edebcc8e 100644 --- a/tests/ComputeSharp.Tests/TransferTexture3DTests.cs +++ b/tests/ComputeSharp.Tests/TransferTexture3DTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using ComputeSharp.Resources; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.Toolkit.HighPerformance.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,12 +12,13 @@ namespace ComputeSharp.Tests [TestCategory("TransferTexture3D")] public partial class TransferTexture3DTests { - [TestMethod] - [DataRow(typeof(UploadTexture3D<>))] - [DataRow(typeof(ReadBackTexture3D<>))] - public unsafe void Allocate_Uninitialized_Ok(Type bufferType) + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] + public unsafe void Allocate_Uninitialized_Ok(Device device, Type bufferType) { - using TransferTexture3D buffer = Gpu.Default.AllocateTransferTexture3D(bufferType, 128, 256, 32); + using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, 128, 256, 32); Assert.IsNotNull(buffer); Assert.AreEqual(buffer.Width, 128); @@ -25,45 +27,44 @@ namespace ComputeSharp.Tests Assert.AreEqual(buffer.View.Width, 128); Assert.AreEqual(buffer.View.Height, 256); Assert.AreEqual(buffer.View.Depth, 32); - Assert.AreSame(buffer.GraphicsDevice, Gpu.Default); + Assert.AreSame(buffer.GraphicsDevice, device.Get()); } - [TestMethod] - [DataRow(typeof(UploadTexture3D<>), 128, -14253, 4)] - [DataRow(typeof(UploadTexture3D<>), 128, -1, 4)] - [DataRow(typeof(UploadTexture3D<>), 0, -4314, 4)] - [DataRow(typeof(UploadTexture3D<>), -14, -53, 4)] - [DataRow(typeof(UploadTexture3D<>), 12, 12, -1)] - [DataRow(typeof(UploadTexture3D<>), 2, 4, ushort.MaxValue + 1)] - [DataRow(typeof(ReadBackTexture3D<>), 128, -14253, 4)] - [DataRow(typeof(ReadBackTexture3D<>), 128, -1, 4)] - [DataRow(typeof(ReadBackTexture3D<>), 0, -4314, 4)] - [DataRow(typeof(ReadBackTexture3D<>), -14, -53, 4)] - [DataRow(typeof(ReadBackTexture3D<>), 12, 12, -1)] - [DataRow(typeof(ReadBackTexture3D<>), 2, 4, ushort.MaxValue + 1)] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] + [Data(128, -14253, 4)] + [Data(128, -1, 4)] + [Data(0, -4314, 4)] + [Data(-14, -53, 4)] + [Data(12, 12, -1)] + [Data(2, 4, ushort.MaxValue + 1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Allocate_Uninitialized_Fail(Type bufferType, int width, int height, int depth) + public void Allocate_Uninitialized_Fail(Device device, Type bufferType, int width, int height, int depth) { - using TransferTexture3D buffer = Gpu.Default.AllocateTransferTexture3D(bufferType, width, height, depth); + using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, width, height, depth); } - [TestMethod] - [DataRow(typeof(UploadTexture3D<>))] - [DataRow(typeof(ReadBackTexture3D<>))] + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(UploadTexture3D<>))] + [Resource(typeof(ReadBackTexture3D<>))] [ExpectedException(typeof(ObjectDisposedException))] - public void UsageAfterDispose(Type bufferType) + public void UsageAfterDispose(Device device, Type bufferType) { - using TransferTexture3D buffer = Gpu.Default.AllocateTransferTexture3D(bufferType, 32, 32, 16); + using TransferTexture3D buffer = device.Get().AllocateTransferTexture3D(bufferType, 32, 32, 16); buffer.Dispose(); _ = buffer.View; } - [TestMethod] - public void Allocate_UploadTexture3D_Copy_Full() + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_UploadTexture3D_Copy_Full(Device device) { - using UploadTexture3D uploadTexture3D = Gpu.Default.AllocateUploadTexture3D(256, 256, 16); + using UploadTexture3D uploadTexture3D = device.Get().AllocateUploadTexture3D(256, 256, 16); for (int i = 0; i < uploadTexture3D.Depth; i++) { @@ -73,7 +74,7 @@ namespace ComputeSharp.Tests } } - using ReadOnlyTexture3D readOnlyTexture3D = Gpu.Default.AllocateReadOnlyTexture3D(uploadTexture3D.Width, uploadTexture3D.Height, uploadTexture3D.Depth); + using ReadOnlyTexture3D readOnlyTexture3D = device.Get().AllocateReadOnlyTexture3D(uploadTexture3D.Width, uploadTexture3D.Height, uploadTexture3D.Depth); uploadTexture3D.CopyTo(readOnlyTexture3D); @@ -95,15 +96,16 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(0, 0, 0, 256, 256, 16)] - [DataRow(128, 0, 0, 72, 256, 16)] - [DataRow(0, 128, 0, 189, 67, 8)] - [DataRow(0, 0, 15, 32, 64, 1)] - [DataRow(97, 33, 4, 128, 99, 7)] - public void Allocate_UploadTexture3D_Copy_Range(int x, int y, int z, int width, int height, int depth) + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 0, 256, 256, 16)] + [Data(128, 0, 0, 72, 256, 16)] + [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) { - using UploadTexture3D uploadTexture3D = Gpu.Default.AllocateUploadTexture3D(256, 256, 16); + using UploadTexture3D uploadTexture3D = device.Get().AllocateUploadTexture3D(256, 256, 16); for (int i = 0; i < uploadTexture3D.Depth; i++) { @@ -113,7 +115,7 @@ namespace ComputeSharp.Tests } } - using ReadOnlyTexture3D readOnlyTexture3D = Gpu.Default.AllocateReadOnlyTexture3D(uploadTexture3D.Width, uploadTexture3D.Height, uploadTexture3D.Depth); + using ReadOnlyTexture3D readOnlyTexture3D = device.Get().AllocateReadOnlyTexture3D(uploadTexture3D.Width, uploadTexture3D.Height, uploadTexture3D.Depth); uploadTexture3D.CopyTo(readOnlyTexture3D, x, y, z, width, height, depth); @@ -131,8 +133,9 @@ namespace ComputeSharp.Tests } } - [TestMethod] - public void Allocate_ReadBackTexture3D_Copy_Full() + [CombinatorialTestMethod] + [AllDevices] + public void Allocate_ReadBackTexture3D_Copy_Full(Device device) { int[,,] source = new int[16, 256, 256]; @@ -144,8 +147,8 @@ namespace ComputeSharp.Tests } } - using ReadOnlyTexture3D readOnlyTexture3D = Gpu.Default.AllocateReadOnlyTexture3D(source); - using ReadBackTexture3D readBackTexture3D = Gpu.Default.AllocateReadBackTexture3D(256, 256, 16); + using ReadOnlyTexture3D readOnlyTexture3D = device.Get().AllocateReadOnlyTexture3D(source); + using ReadBackTexture3D readBackTexture3D = device.Get().AllocateReadBackTexture3D(256, 256, 16); readOnlyTexture3D.CopyTo(readBackTexture3D); @@ -165,13 +168,14 @@ namespace ComputeSharp.Tests } } - [TestMethod] - [DataRow(0, 0, 0, 256, 256, 16)] - [DataRow(128, 0, 0, 72, 256, 16)] - [DataRow(0, 128, 0, 189, 67, 8)] - [DataRow(0, 0, 15, 32, 64, 1)] - [DataRow(97, 33, 4, 128, 99, 7)] - public void Allocate_ReadBackTexture3D_Copy_Range(int x, int y, int z, int width, int height, int depth) + [CombinatorialTestMethod] + [AllDevices] + [Data(0, 0, 0, 256, 256, 16)] + [Data(128, 0, 0, 72, 256, 16)] + [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) { int[,,] source = new int[16, 256, 256]; @@ -183,8 +187,8 @@ namespace ComputeSharp.Tests } } - using ReadOnlyTexture3D readOnlyTexture3D = Gpu.Default.AllocateReadOnlyTexture3D(source); - using ReadBackTexture3D readBackTexture3D = Gpu.Default.AllocateReadBackTexture3D(256, 256, 16); + using ReadOnlyTexture3D readOnlyTexture3D = device.Get().AllocateReadOnlyTexture3D(source); + using ReadBackTexture3D readBackTexture3D = device.Get().AllocateReadBackTexture3D(256, 256, 16); readOnlyTexture3D.CopyTo(readBackTexture3D, x, y, z, width, height, depth);