From 54bdec1792e8689a384fae94c7c598239be8351b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 30 Aug 2022 00:56:53 +0200 Subject: [PATCH 1/3] Use T4 wildcards in ComputeSharp.Tests project --- .../ComputeSharp.Tests.csproj | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index 93ffea0a..93866d1d 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -98,31 +98,21 @@ - - - TextTemplatingFileGenerator - Texture2DTests.Pixels.g.cs - - - TextTemplatingFileGenerator - Texture3DTests.Pixels.g.cs - - - + + - + + %(Filename).g.cs + TextTemplatingFileGenerator + + + $([System.IO.Path]::GetFileNameWithoutExtension('%(Filename)')).tt True True - Texture2DTests.Pixels.tt - - - True - True - Texture3DTests.Pixels.tt From f6af39e36c83e33e8898afef17b11b8996cbc59b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 30 Aug 2022 02:34:17 +0200 Subject: [PATCH 2/3] Add generated tests for all resource dimension accessors --- .../ResourceDimensionsTests.g.cs | 583 ++++++++++++++++++ .../ResourceDimensionsTests.tt | 149 +++++ 2 files changed, 732 insertions(+) create mode 100644 tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs create mode 100644 tests/ComputeSharp.Tests/ResourceDimensionsTests.tt diff --git a/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs b/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs new file mode 100644 index 00000000..df0cb66c --- /dev/null +++ b/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs @@ -0,0 +1,583 @@ +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("ResourceDimensions")] +public partial class ResourceDimensionsTests +{ + [CombinatorialTestMethod] + [AllDevices] + [Data(8)] + [Data(64)] + [Data(128)] + [Data(376)] + public void ConstantBuffer_T1_AsConstantBuffer(Device device, int axis0) + { + using ConstantBuffer resource = device.Get().AllocateConstantBuffer(axis0); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(1); + + device.Get().For(1, new ConstantBuffer_T1_AsConstantBufferShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ConstantBuffer_T1_AsConstantBufferShader : IComputeShader + { + public readonly ConstantBuffer source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Length; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8)] + [Data(64)] + [Data(128)] + [Data(376)] + public void ReadOnlyBuffer_T1_AsReadOnlyBuffer(Device device, int axis0) + { + using ReadOnlyBuffer resource = device.Get().AllocateReadOnlyBuffer(axis0); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(1); + + device.Get().For(1, new ReadOnlyBuffer_T1_AsReadOnlyBufferShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyBuffer_T1_AsReadOnlyBufferShader : IComputeShader + { + public readonly ReadOnlyBuffer source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Length; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8)] + [Data(64)] + [Data(128)] + [Data(376)] + public void ReadWriteBuffer_T1_AsReadWriteBuffer(Device device, int axis0) + { + using ReadWriteBuffer resource = device.Get().AllocateReadWriteBuffer(axis0); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(1); + + device.Get().For(1, new ReadWriteBuffer_T1_AsReadWriteBufferShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteBuffer_T1_AsReadWriteBufferShader : IComputeShader + { + public readonly ReadWriteBuffer source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Length; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadOnlyTexture2D_T1_AsReadOnlyTexture2D(Device device, int axis0, int axis1) + { + using ReadOnlyTexture2D resource = device.Get().AllocateReadOnlyTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ReadOnlyTexture2D_T1_AsReadOnlyTexture2DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyTexture2D_T1_AsReadOnlyTexture2DShader : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadWriteTexture2D_T1_AsReadWriteTexture2D(Device device, int axis0, int axis1) + { + using ReadWriteTexture2D resource = device.Get().AllocateReadWriteTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ReadWriteTexture2D_T1_AsReadWriteTexture2DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture2D_T1_AsReadWriteTexture2DShader : IComputeShader + { + public readonly ReadWriteTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadWriteTexture2D_T1_AsIReadOnlyTexture2D(Device device, int axis0, int axis1) + { + using ReadWriteTexture2D resource = device.Get().AllocateReadWriteTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(resource, ResourceState.ReadOnly); + context.For(1, new ReadWriteTexture2D_T1_AsIReadOnlyTexture2DShader(resource.AsReadOnly(), result)); + } + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture2D_T1_AsIReadOnlyTexture2DShader : IComputeShader + { + public readonly IReadOnlyTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadOnlyTexture2D_T2_AsReadOnlyTexture2D(Device device, int axis0, int axis1) + { + using ReadOnlyTexture2D resource = device.Get().AllocateReadOnlyTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ReadOnlyTexture2D_T2_AsReadOnlyTexture2DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyTexture2D_T2_AsReadOnlyTexture2DShader : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadOnlyTexture2D_T2_AsIReadOnlyNormalizedTexture2D(Device device, int axis0, int axis1) + { + using ReadOnlyTexture2D resource = device.Get().AllocateReadOnlyTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ReadOnlyTexture2D_T2_AsIReadOnlyNormalizedTexture2DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyTexture2D_T2_AsIReadOnlyNormalizedTexture2DShader : IComputeShader + { + public readonly IReadOnlyNormalizedTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadWriteTexture2D_T2_AsReadWriteTexture2D(Device device, int axis0, int axis1) + { + using ReadWriteTexture2D resource = device.Get().AllocateReadWriteTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ReadWriteTexture2D_T2_AsReadWriteTexture2DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture2D_T2_AsReadWriteTexture2DShader : IComputeShader + { + public readonly ReadWriteTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12)] + [Data(64, 13)] + [Data(128, 32)] + [Data(376, 112)] + public void ReadWriteTexture2D_T2_AsIReadWriteNormalizedTexture2D(Device device, int axis0, int axis1) + { + using ReadWriteTexture2D resource = device.Get().AllocateReadWriteTexture2D(axis0, axis1); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ReadWriteTexture2D_T2_AsIReadWriteNormalizedTexture2DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture2D_T2_AsIReadWriteNormalizedTexture2DShader : IComputeShader + { + public readonly IReadWriteNormalizedTexture2D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadOnlyTexture3D_T1_AsReadOnlyTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadOnlyTexture3D resource = device.Get().AllocateReadOnlyTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + device.Get().For(1, new ReadOnlyTexture3D_T1_AsReadOnlyTexture3DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyTexture3D_T1_AsReadOnlyTexture3DShader : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadWriteTexture3D_T1_AsReadWriteTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadWriteTexture3D resource = device.Get().AllocateReadWriteTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + device.Get().For(1, new ReadWriteTexture3D_T1_AsReadWriteTexture3DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture3D_T1_AsReadWriteTexture3DShader : IComputeShader + { + public readonly ReadWriteTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadWriteTexture3D_T1_AsIReadOnlyTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadWriteTexture3D resource = device.Get().AllocateReadWriteTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(resource, ResourceState.ReadOnly); + context.For(1, new ReadWriteTexture3D_T1_AsIReadOnlyTexture3DShader(resource.AsReadOnly(), result)); + } + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture3D_T1_AsIReadOnlyTexture3DShader : IComputeShader + { + public readonly IReadOnlyTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadOnlyTexture3D_T2_AsReadOnlyTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadOnlyTexture3D resource = device.Get().AllocateReadOnlyTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + device.Get().For(1, new ReadOnlyTexture3D_T2_AsReadOnlyTexture3DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyTexture3D_T2_AsReadOnlyTexture3DShader : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadOnlyTexture3D_T2_AsIReadOnlyNormalizedTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadOnlyTexture3D resource = device.Get().AllocateReadOnlyTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + device.Get().For(1, new ReadOnlyTexture3D_T2_AsIReadOnlyNormalizedTexture3DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadOnlyTexture3D_T2_AsIReadOnlyNormalizedTexture3DShader : IComputeShader + { + public readonly IReadOnlyNormalizedTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadWriteTexture3D_T2_AsReadWriteTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadWriteTexture3D resource = device.Get().AllocateReadWriteTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + device.Get().For(1, new ReadWriteTexture3D_T2_AsReadWriteTexture3DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture3D_T2_AsReadWriteTexture3DShader : IComputeShader + { + public readonly ReadWriteTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(8, 12, 3)] + [Data(64, 24, 4)] + [Data(128, 32, 4)] + [Data(376, 64, 3)] + public void ReadWriteTexture3D_T2_AsIReadWriteNormalizedTexture3D(Device device, int axis0, int axis1, int axis2) + { + using ReadWriteTexture3D resource = device.Get().AllocateReadWriteTexture3D(axis0, axis1, axis2); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(3); + + device.Get().For(1, new ReadWriteTexture3D_T2_AsIReadWriteNormalizedTexture3DShader(resource, result)); + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { axis0, axis1, axis2 }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct ReadWriteTexture3D_T2_AsIReadWriteNormalizedTexture3DShader : IComputeShader + { + public readonly IReadWriteNormalizedTexture3D source; + public readonly ReadWriteBuffer result; + + public void Execute() + { + result[0] = source.Width; + result[1] = source.Height; + result[2] = source.Depth; + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt b/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt new file mode 100644 index 00000000..4cd114a0 --- /dev/null +++ b/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt @@ -0,0 +1,149 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text.RegularExpressions" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".g.cs"#> +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("ResourceDimensions")] +public partial class ResourceDimensionsTests +{ +<# +// Map of all resource types with dimensions accessors +var typesMap = new Dictionary +{ + ["ConstantBuffer"] = new[] { "Length" }, + ["ReadOnlyBuffer"] = new[] { "Length" }, + ["ReadWriteBuffer"] = new[] { "Length" }, + ["ReadOnlyTexture2D"] = new[] { "Width", "Height" }, + ["ReadWriteTexture2D"] = new[] { "Width", "Height" }, + ["ReadOnlyTexture2D"] = new[] { "Width", "Height" }, + ["ReadWriteTexture2D"] = new[] { "Width", "Height" }, + ["ReadOnlyTexture3D"] = new[] { "Width", "Height", "Depth" }, + ["ReadWriteTexture3D"] = new[] { "Width", "Height", "Depth" }, + ["ReadOnlyTexture3D"] = new[] { "Width", "Height", "Depth" }, + ["ReadWriteTexture3D"] = new[] { "Width", "Height", "Depth" } +}; + +// Map of all resource types that also have a supported interface +var interfacesMap = new Dictionary +{ + ["ReadWriteTexture2D"] = "IReadOnlyTexture2D", + ["ReadOnlyTexture2D"] = "IReadOnlyNormalizedTexture2D", + ["ReadWriteTexture2D"] = "IReadWriteNormalizedTexture2D", + ["ReadWriteTexture3D"] = "IReadOnlyTexture3D", + ["ReadOnlyTexture3D"] = "IReadOnlyNormalizedTexture3D", + ["ReadWriteTexture3D"] = "IReadWriteNormalizedTexture3D" +}; + +// Map of all resource that need a readonly wrapper +var readonlyWrappersMap = new HashSet +{ + "IReadOnlyTexture2D", + "IReadOnlyTexture3D" +}; + +// Map of data row items to test multiple dimension combinations +var dataRows = new Dictionary +{ + [1] = new[] { new[] { 8 }, new[] { 64 }, new[] { 128 }, new[] { 376 } }, + [2] = new[] { new[] { 8, 12 }, new[] { 64, 13 }, new[] { 128, 32 }, new[] { 376, 112 } }, + [3] = new[] { new[] { 8, 12, 3 }, new[] { 64, 24, 4 }, new[] { 128, 32, 4 }, new[] { 376, 64, 3 } } +}; + +int index = 0; + +foreach (var pair in typesMap) +{ + var numberOfDimensions = pair.Value.Length; + var numberOfTypeParameters = Regex.Match(pair.Key, @"<([\w, ]+)>").Groups[1].Value.Split(',').Length; + var resourceName = Regex.Match(pair.Key, @"\w+").Value; + var shaderResourceTypes = new List { pair.Key }; + + if (interfacesMap.TryGetValue(pair.Key, out var interfaceType)) shaderResourceTypes.Add(interfaceType); + + foreach (var shaderResourceType in shaderResourceTypes) + { + if (index++ != 0) WriteLine(""); + + var shaderResourceName = Regex.Match(shaderResourceType, @"\w+").Value; + var testMethodName = $"{resourceName}_T{numberOfTypeParameters}_As{shaderResourceName}"; +#> + [CombinatorialTestMethod] + [AllDevices] +<# + foreach (var dataRow in dataRows[numberOfDimensions]) + { + WriteLine($" [Data({string.Join(", ", dataRow)})]"); + } + + string parameters = string.Join(", ", Enumerable + .Range(0, numberOfDimensions) + .Select(x => $"int axis{x}")); + + string arguments = string.Join(", ", Enumerable + .Range(0, numberOfDimensions) + .Select(x => $"axis{x}")); + + bool needsReadonlyWrapper = readonlyWrappersMap.Contains(shaderResourceType); +#> + public void <#=testMethodName#>(Device device, <#=parameters#>) + { + using <#=pair.Key#> resource = device.Get().Allocate<#=pair.Key#>(<#=arguments#>); + using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(<#=numberOfDimensions#>); + +<# + if (needsReadonlyWrapper) + { +#> + using (var context = device.Get().CreateComputeContext()) + { + context.Transition(resource, ResourceState.ReadOnly); + context.For(1, new <#=testMethodName#>Shader(resource.AsReadOnly(), result)); + } +<# + } + else + { +#> + device.Get().For(1, new <#=testMethodName#>Shader(resource, result)); +<# + } +#> + + int[] dimensions = result.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { <#=arguments#> }, + actual: dimensions); + } + + [AutoConstructor] + internal readonly partial struct <#=testMethodName#>Shader : IComputeShader + { + public readonly <#=shaderResourceType#> source; + public readonly ReadWriteBuffer result; + + public void Execute() + { +<# + for (int i = 0; i < pair.Value.Length; i++) + { +#> + result[<#=i#>] = source.<#=pair.Value[i]#>; +<# + } +#> + } + } +<# + } +} +#> +} \ No newline at end of file From 040d273f10a32e3ea70e28b0849be5118444c654 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 30 Aug 2022 02:43:56 +0200 Subject: [PATCH 3/3] Remove dimensions test for ConstantBuffer Accessing the dimension from a shader is not supported --- .../ResourceDimensionsTests.g.cs | 32 ------------------- .../ResourceDimensionsTests.tt | 1 - 2 files changed, 33 deletions(-) diff --git a/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs b/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs index df0cb66c..732137ef 100644 --- a/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs +++ b/tests/ComputeSharp.Tests/ResourceDimensionsTests.g.cs @@ -8,38 +8,6 @@ namespace ComputeSharp.Tests; [TestCategory("ResourceDimensions")] public partial class ResourceDimensionsTests { - [CombinatorialTestMethod] - [AllDevices] - [Data(8)] - [Data(64)] - [Data(128)] - [Data(376)] - public void ConstantBuffer_T1_AsConstantBuffer(Device device, int axis0) - { - using ConstantBuffer resource = device.Get().AllocateConstantBuffer(axis0); - using ReadWriteBuffer result = device.Get().AllocateReadWriteBuffer(1); - - device.Get().For(1, new ConstantBuffer_T1_AsConstantBufferShader(resource, result)); - - int[] dimensions = result.ToArray(); - - CollectionAssert.AreEqual( - expected: new[] { axis0 }, - actual: dimensions); - } - - [AutoConstructor] - internal readonly partial struct ConstantBuffer_T1_AsConstantBufferShader : IComputeShader - { - public readonly ConstantBuffer source; - public readonly ReadWriteBuffer result; - - public void Execute() - { - result[0] = source.Length; - } - } - [CombinatorialTestMethod] [AllDevices] [Data(8)] diff --git a/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt b/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt index 4cd114a0..8e3daf32 100644 --- a/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt +++ b/tests/ComputeSharp.Tests/ResourceDimensionsTests.tt @@ -18,7 +18,6 @@ public partial class ResourceDimensionsTests // Map of all resource types with dimensions accessors var typesMap = new Dictionary { - ["ConstantBuffer"] = new[] { "Length" }, ["ReadOnlyBuffer"] = new[] { "Length" }, ["ReadWriteBuffer"] = new[] { "Length" }, ["ReadOnlyTexture2D"] = new[] { "Width", "Height" },