using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text.RegularExpressions;
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 IReadOnlyDictionary KnownSizes = BuildKnownSizesMap();
///
/// Builds the mapping of known type sizes and alignments.
///
private static IReadOnlyDictionary BuildKnownSizesMap()
{
Dictionary knownSizes = new()
{
[typeof(bool).FullName] = (4, 4),
[typeof(int).FullName] = (4, 4),
[typeof(uint).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)
};
foreach (
var type in
from type in Assembly.GetExecutingAssembly().ExportedTypes
where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)")
select type)
{
knownSizes.Add(type.FullName, (type.StructLayoutAttribute.Size, type.StructLayoutAttribute.Pack));
}
return knownSizes;
}
///
/// Gets the size and alignment info for a specified primitive type.
///
/// The type name to get info for.
/// The size and and alignment for the input type.
public static (int Size, int Pack) GetTypeInfo(string typeName)
{
return KnownSizes[typeName];
}
}