diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs new file mode 100644 index 00000000..d7c70573 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Numerics; + +namespace ComputeSharp.SourceGenerators.Mappings +{ + /// + /// A that contains and maps known HLSL types to their alignment sizes. + /// + internal static class HlslKnownSizes + { + /// + /// The mapping of supported known sizes to HLSL type names. + /// + private static readonly IReadOnlyDictionary KnownSizes = new Dictionary + { + [typeof(bool).FullName] = (4, 4), + [typeof(int).FullName] = (4, 4), + [typeof(int).FullName] = (4, 4), + [typeof(float).FullName] = (4, 4), + [typeof(double).FullName] = (8, 8), + + [typeof(Vector2).FullName] = (8, 4), + [typeof(Vector3).FullName] = (12, 4), + [typeof(Vector4).FullName] = (16, 4), + + [typeof(Bool2).FullName] = (8, 4), + [typeof(Bool3).FullName] = (12, 4), + [typeof(Bool4).FullName] = (16, 4), + + [typeof(Int2).FullName] = (8, 4), + [typeof(Int3).FullName] = (12, 4), + [typeof(Int4).FullName] = (16, 4), + + [typeof(UInt2).FullName] = (8, 4), + [typeof(UInt3).FullName] = (12, 4), + [typeof(UInt4).FullName] = (16, 4), + + [typeof(Float2).FullName] = (8, 4), + [typeof(Float3).FullName] = (12, 4), + [typeof(Float4).FullName] = (16, 4), + + [typeof(Double2).FullName] = (16, 8), + [typeof(Double3).FullName] = (24, 8), + [typeof(Double3).FullName] = (32, 8) + }; + + /// + /// Tries to get the mapped HLSL size and alignment for the input type name. + /// + /// The input fully qualified type name. + /// The mapped name, if one is found. + /// The HLSL size and alignment that can be used in an HLSL shader. + [Pure] + public static bool TryGetMappedSize(string name, out (int Size, int Alignment) mapped) + { + return KnownSizes.TryGetValue(name, out mapped); + } + } +}