diff --git a/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude b/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude index 10a1d707..27cda52f 100644 --- a/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude +++ b/src/ComputeSharp.Core/Primitives/MatrixType.ttinclude @@ -1,8 +1,8 @@ <#@include file="..\Intrinsics\Hlsl.MulOverloads.ttinclude" #> +<#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Text.RegularExpressions" #> -<#@ import namespace="System.Collections.Generic" #> <#@ output extension=".g.cs"#> <# // Generates all matrix members and types for a given HLSL type diff --git a/src/ComputeSharp.Core/Primitives/VectorType.Ctors.ttinclude b/src/ComputeSharp.Core/Primitives/VectorType.Ctors.ttinclude new file mode 100644 index 00000000..a7f4f3d3 --- /dev/null +++ b/src/ComputeSharp.Core/Primitives/VectorType.Ctors.ttinclude @@ -0,0 +1,180 @@ +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text.RegularExpressions" #> +<# +// Enumerates all combinations of values that sum up to a given size. +// This method is only supported for valid sizes for HLSL vector types. +string[][] GetConstructorParameterCombinations(int size) +{ + switch (size) + { + case 2: + return new[] { new[] { "T", "T" } }; + case 3: + return new[] + { + new[] { "T", "T", "T" }, + new[] { "T2", "T" }, + new[] { "T1x2", "T" }, + new[] { "T2x1", "T" }, + new[] { "T", "T2" }, + new[] { "T", "T1x2" }, + new[] { "T", "T2x1" } + }; + case 4: + return new[] + { + new[] { "T", "T", "T", "T" }, + new[] { "T2", "T", "T" }, + new[] { "T1x2", "T", "T" }, + new[] { "T2x1", "T", "T" }, + new[] { "T", "T2", "T" }, + new[] { "T", "T1x2", "T" }, + new[] { "T", "T2x1", "T" }, + new[] { "T", "T", "T2" }, + new[] { "T", "T", "T1x2" }, + new[] { "T", "T", "T2x1" }, + new[] { "T2", "T2" }, + new[] { "T2", "T1x2" }, + new[] { "T2", "T2x1" }, + new[] { "T1x2", "T2" }, + new[] { "T1x2", "T1x2" }, + new[] { "T1x2", "T2x1" }, + new[] { "T2x1", "T2" }, + new[] { "T2x1", "T1x2" }, + new[] { "T2x1", "T2x1" }, + new[] { "T3", "T" }, + new[] { "T1x3", "T" }, + new[] { "T3x1", "T" }, + new[] { "T", "T3" }, + new[] { "T", "T1x3" }, + new[] { "T", "T3x1" } + }; + default: + throw new ArgumentOutOfRangeException("size"); + } +} + +// Gets the size of the parameters for a given constructor +int[] GetConstructorParameterSizes(string[] parameters) +{ + return parameters.Select(p => + { + if (p == "T3" || p == "T1x3" || p == "T3x1") return 3; + if (p == "T2" || p == "T1x2" || p == "T2x1") return 2; + return 1; + }).ToArray(); +} + +// Gets the sequence of constructor parameter names for a given sequence of sizes +string[] GetConstructorParameterNames(int[] parameterSizes) +{ + List parameterNames = new List(); + int offset = 0; + + foreach (int parameterSize in parameterSizes) + { + parameterNames.Add("xyzw".Substring(offset, parameterSize)); + + offset += parameterSize; + } + + return parameterNames.ToArray(); +} + +// Get the ordinal name for a given index +string GetOrdinalName(int index) +{ + switch (index) + { + case 0: return "first"; + case 1: return "second"; + case 2: return "third"; + default: return "fourth"; + } +} + +// Generates all constructor declarations for a specified type +void GenerateConstructors(string typeName, int elementSize) +{ + string elementTypeName = Regex.Match(typeName, @"^[A-Za-z]+").Value; + string hlslElementTypeName = elementTypeName.ToLower(); + + foreach (string[] parameters in GetConstructorParameterCombinations(elementSize)) + { + int[] parameterSizes = GetConstructorParameterSizes(parameters); + string[] parameterNames = GetConstructorParameterNames(parameterSizes); + + WriteLine("/// "); + WriteLine($"/// Creates a new instance with the specified parameters."); + WriteLine("/// "); + + int inputOffset = 0; + + // Declare all constructor parameters + foreach (string parameterName in parameterNames) + { + if (parameterName.Length == 1) + { + WriteLine($"/// The value to assign to the {GetOrdinalName(inputOffset)} vector component."); + } + else + { + string ordinals = string.Join(", ", Enumerable.Range(0, inputOffset + parameterName.Length - 1).Select(GetOrdinalName)); + + ordinals += $" and {GetOrdinalName(inputOffset + parameterName.Length - 1)}"; + + WriteLine($"/// The value to assign to the {ordinals} vector components."); + } + + inputOffset += parameterName.Length; + } + + Write($"public {typeName}("); + + // Write all the declared parameters for the current constructor + Write(string.Join(", ", parameterNames.Select((name, index) => + { + return name.Length == 1 + ? $"{hlslElementTypeName} {name}" + : $"{parameters[index].Replace("T", elementTypeName)} {name}"; + }))); + + WriteLine(")"); + WriteLine("{"); + PushIndent(" "); + + inputOffset = 0; + + // Write all constructor statements + for (int i = 0; i < parameterSizes.Length; i++) + { + for (int j = 0; j < parameterSizes[i]; j++) + { + Write($"this.{"xyzw"[inputOffset++]} = "); + + if (parameters[i].EndsWith("x1")) + { + WriteLine($"{parameterNames[i]}.M{j + 1}1;"); + } + else if (parameters[i].Contains('x')) + { + WriteLine($"{parameterNames[i]}.M1{j + 1};"); + } + else if (parameterSizes[i] > 1) + { + WriteLine($"{parameterNames[i]}.{char.ToUpperInvariant("xyzw"[j])};"); + } + else + { + WriteLine($"{parameterNames[i]};"); + } + } + } + + PopIndent(); + WriteLine("}"); + WriteLine(""); + } +} +#> \ No newline at end of file diff --git a/src/ComputeSharp.Core/Primitives/VectorType.ttinclude b/src/ComputeSharp.Core/Primitives/VectorType.ttinclude index 0bf6f7c2..e131360e 100644 --- a/src/ComputeSharp.Core/Primitives/VectorType.ttinclude +++ b/src/ComputeSharp.Core/Primitives/VectorType.ttinclude @@ -1,8 +1,9 @@ <#@include file="..\Intrinsics\Hlsl.MulOverloads.ttinclude" #> +<#@include file="VectorType.Ctors.ttinclude" #> +<#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Text.RegularExpressions" #> -<#@ import namespace="System.Collections.Generic" #> <#@ output extension=".g.cs"#> <# IEnumerable EnumeratePropertyNames(string characters) @@ -96,6 +97,9 @@ public unsafe partial struct <#=typeName#> WriteLine(""); } + // Generate the constructors + GenerateConstructors(typeName, i); + PopIndent(); #> ///