Refactored code to use file scoped using declarations
このコミットが含まれているのは:
1 親
52ec5a79b0
コミット
d5e3114f7c
275ファイル変更
+91970
-92271
検索条件に一致するファイルはありません
@@ -4,70 +4,69 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Core.Intrinsics.Attributes;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
namespace ComputeSharp.SourceGenerators.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL identifier names valid HLSL names.
|
||||
/// </summary>
|
||||
internal static class HlslKnownKeywords
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL identifier names valid HLSL names.
|
||||
/// The mapping of known HLSL keywords.
|
||||
/// </summary>
|
||||
internal static class HlslKnownKeywords
|
||||
private static readonly IReadOnlyCollection<string> KnownKeywords = BuildKnownKeywordsMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of all known HLSL keywords.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownKeywordsMap()
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of known HLSL keywords.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownKeywords = BuildKnownKeywordsMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of all known HLSL keywords.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownKeywordsMap()
|
||||
// HLSL keywords
|
||||
HashSet<string> knownKeywords = new(new[]
|
||||
{
|
||||
// HLSL keywords
|
||||
HashSet<string> knownKeywords = new(new[]
|
||||
{
|
||||
"asm", "asm_fragment", "cbuffer", "centroid", "column_major",
|
||||
"compile", "discard", "dword", "export", "fxgroup", "groupshared",
|
||||
"half", "inline", "inout", "line", "lineadj", "linear", "matrix",
|
||||
"nointerpolation", "noperspective", "NULL", "packoffset", "pass",
|
||||
"pixelfragment", "point", "precise", "register", "row_major", "sample",
|
||||
"sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer",
|
||||
"technique", "typedef", "triangle", "triangleadj", "uniform", "unorm",
|
||||
"unsigned", "vector", "vertexfragment", "zero"
|
||||
});
|
||||
"asm", "asm_fragment", "cbuffer", "centroid", "column_major",
|
||||
"compile", "discard", "dword", "export", "fxgroup", "groupshared",
|
||||
"half", "inline", "inout", "line", "lineadj", "linear", "matrix",
|
||||
"nointerpolation", "noperspective", "NULL", "packoffset", "pass",
|
||||
"pixelfragment", "point", "precise", "register", "row_major", "sample",
|
||||
"sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer",
|
||||
"technique", "typedef", "triangle", "triangleadj", "uniform", "unorm",
|
||||
"unsigned", "vector", "vertexfragment", "zero"
|
||||
});
|
||||
|
||||
// Dispatch type names
|
||||
knownKeywords.Add(nameof(ThreadIds));
|
||||
knownKeywords.Add(nameof(GroupIds));
|
||||
knownKeywords.Add(nameof(GroupSize));
|
||||
knownKeywords.Add(nameof(GridIds));
|
||||
// Dispatch type names
|
||||
knownKeywords.Add(nameof(ThreadIds));
|
||||
knownKeywords.Add(nameof(GroupIds));
|
||||
knownKeywords.Add(nameof(GroupSize));
|
||||
knownKeywords.Add(nameof(GridIds));
|
||||
|
||||
// HLSL intrinsics method names
|
||||
foreach (var method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
string name = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
// HLSL intrinsics method names
|
||||
foreach (var method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
string name = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
|
||||
knownKeywords.Add(name);
|
||||
}
|
||||
|
||||
return knownKeywords;
|
||||
knownKeywords.Add(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible identifier name for the input identifier name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input identifier name.</param>
|
||||
/// <param name="mapped">The mapped identifier name, if a replacement is needed.</param>
|
||||
/// <returns>The HLSL-compatible identifier name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
mapped = KnownKeywords.Contains(name) switch
|
||||
{
|
||||
true => $"__reserved__{name}",
|
||||
false => null
|
||||
};
|
||||
return knownKeywords;
|
||||
}
|
||||
|
||||
return mapped is not null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible identifier name for the input identifier name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input identifier name.</param>
|
||||
/// <param name="mapped">The mapped identifier name, if a replacement is needed.</param>
|
||||
/// <returns>The HLSL-compatible identifier name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
mapped = KnownKeywords.Contains(name) switch
|
||||
{
|
||||
true => $"__reserved__{name}",
|
||||
false => null
|
||||
};
|
||||
|
||||
return mapped is not null;
|
||||
}
|
||||
}
|
||||
@@ -7,434 +7,433 @@ using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
namespace ComputeSharp.SourceGenerators.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL member names to common .NET members.
|
||||
/// </summary>
|
||||
internal static class HlslKnownMembers
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL member names to common .NET members.
|
||||
/// The mapping of supported known indexers to HLSL resource type names.
|
||||
/// </summary>
|
||||
internal static class HlslKnownMembers
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownResourceIndexers = new Dictionary<string, string>
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known indexers to HLSL resource type names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownResourceIndexers = new Dictionary<string, string>
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadWriteTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.IReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.ReadWriteTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.IReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.IReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known samplers to HLSL resource type names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string?> KnownResourceSamplers = new Dictionary<string, string?>
|
||||
{
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(float).FullName}, {typeof(float).FullName}]"] = "float2",
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(Float2).FullName}]"] = null,
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(float).FullName}, {typeof(float).FullName}]"] = "float2",
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(Float2).FullName}]"] = null,
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName}]"] = "float3",
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(Float3).FullName}]"] = null,
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName}]"] = "float3",
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(Float3).FullName}]"] = null
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known size accessors for HLSL resource types.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, (int Rank, int Axis)> KnownSizeAccessors = new Dictionary<string, (int, int)>
|
||||
{
|
||||
["ComputeSharp.Resources.Buffer`1.Length"] = (2, 0),
|
||||
["ComputeSharp.Resources.Texture2D`1.Width"] = (2, 0),
|
||||
["ComputeSharp.Resources.Texture2D`1.Height"] = (2, 1),
|
||||
["ComputeSharp.Resources.Texture3D`1.Width"] = (3, 0),
|
||||
["ComputeSharp.Resources.Texture3D`1.Height"] = (3, 1),
|
||||
["ComputeSharp.Resources.Texture3D`1.Depth"] = (3, 2),
|
||||
["ComputeSharp.IReadOnlyTexture2D`1.Width"] = (2, 0),
|
||||
["ComputeSharp.IReadOnlyTexture2D`1.Height"] = (2, 1),
|
||||
["ComputeSharp.IReadOnlyTexture3D`1.Width"] = (3, 0),
|
||||
["ComputeSharp.IReadOnlyTexture3D`1.Height"] = (3, 1),
|
||||
["ComputeSharp.IReadOnlyTexture3D`1.Depth"] = (3, 2),
|
||||
["ComputeSharp.IReadWriteTexture2D`1.Width"] = (2, 0),
|
||||
["ComputeSharp.IReadWriteTexture2D`1.Height"] = (2, 1),
|
||||
["ComputeSharp.IReadWriteTexture3D`1.Width"] = (3, 0),
|
||||
["ComputeSharp.IReadWriteTexture3D`1.Height"] = (3, 1),
|
||||
["ComputeSharp.IReadWriteTexture3D`1.Depth"] = (3, 2)
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownMembers = BuildKnownMembersMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownMembersMap()
|
||||
{
|
||||
Dictionary<string, string> knownMembers = new()
|
||||
{
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadWriteTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.IReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2",
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.ReadWriteTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.IReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3",
|
||||
[$"ComputeSharp.IReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3"
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.X)}"] = "x",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Y)}"] = "y",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Zero)}"] = "(float2)0",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.One)}"] = "float2(1.0f, 1.0f)",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.UnitX)}"] = "float2(1.0f, 0.0f)",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.UnitY)}"] = "float2(0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.X)}"] = "x",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Y)}"] = "y",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Z)}"] = "z",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Zero)}"] = "(float3)0",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.One)}"] = "float3(1.0f, 1.0f, 1.0f)",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitX)}"] = "float3(1.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitY)}"] = "float3(0.0f, 1.0f, 0.0f)",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitZ)}"] = "float3(0.0f, 0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.X)}"] = "x",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Y)}"] = "y",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Z)}"] = "z",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.W)}"] = "w",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Zero)}"] = "(float4)0",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.One)}"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.False)}"] = "(bool2)0",
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.True)}"] = "bool2(true, true)",
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.TrueX)}"] = "bool2(true, false)",
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.TrueY)}"] = "bool2(false, true)",
|
||||
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.False)}"] = "(bool3)0",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.True)}"] = "bool3(true, true, true)",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueX)}"] = "bool3(true, false, false)",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueY)}"] = "bool3(false, true, false)",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueZ)}"] = "bool3(false, false, true)",
|
||||
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.False)}"] = "(bool4)0",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.True)}"] = "bool4(true, true, true, true)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueX)}"] = "bool4(true, false, false, false)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueY)}"] = "bool4(false, true, false, false)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueZ)}"] = "bool4(false, false, true, false)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueW)}"] = "bool4(false, false, false, true)",
|
||||
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.Zero)}"] = "(int2)0",
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.One)}"] = "int2(1, 1)",
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.UnitX)}"] = "int2(1, 0)",
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.UnitY)}"] = "int2(0, 1)",
|
||||
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.Zero)}"] = "(int3)0",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.One)}"] = "int3(1, 1, 1)",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.UnitX)}"] = "int3(1, 0, 0)",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.UnitY)}"] = "int3(0, 1, 0)",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.UnitZ)}"] = "int3(0, 0, 1)",
|
||||
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.Zero)}"] = "(int4)0",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.One)}"] = "int4(1, 1, 1, 1)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitX)}"] = "int4(1, 0, 0, 0)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitY)}"] = "int4(0, 1, 0, 0)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitZ)}"] = "int4(0, 0, 1, 0)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitW)}"] = "int4(0, 0, 0, 1)",
|
||||
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.Zero)}"] = "(uint2)0",
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.One)}"] = "uint2(1u, 1u)",
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.UnitX)}"] = "uint2(1u, 0u)",
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.UnitY)}"] = "uint2(0u, 1u)",
|
||||
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.Zero)}"] = "(uint3)0",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.One)}"] = "uint3(1u, 1u, 1u)",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitX)}"] = "uint3(1u, 0u, 0u)",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitY)}"] = "uint3(0u, 1u, 0u)",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitZ)}"] = "uint3(0u, 0u, 1u)",
|
||||
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.Zero)}"] = "(uint4)0",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.One)}"] = "uint4(1u, 1u, 1u, 1u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitX)}"] = "uint4(1u, 0u, 0u, 0u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitY)}"] = "uint4(0u, 1u, 0u, 0u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitZ)}"] = "uint4(0u, 0u, 1u, 0u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitW)}"] = "uint4(0u, 0u, 0u, 1u)",
|
||||
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.Zero)}"] = "(float2)0",
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.One)}"] = "float2(1.0f, 1.0f)",
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.UnitX)}"] = "float2(1.0f, 0.0f)",
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.UnitY)}"] = "float2(0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.Zero)}"] = "(float3)0",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.One)}"] = "float3(1.0f, 1.0f, 1.0f)",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.UnitX)}"] = "float3(1.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.UnitY)}"] = "float3(0.0f, 1.0f, 0.0f)",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.UnitZ)}"] = "float3(0.0f, 0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.Zero)}"] = "(float4)0",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.One)}"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitX)}"] = "float4(1.0f, 0.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitY)}"] = "float4(0.0f, 1.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitZ)}"] = "float4(0.0f, 0.0f, 1.0f, 0.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitW)}"] = "float4(0.0f, 0.0f, 0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.Zero)}"] = "(double2)0",
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.One)}"] = "double2(1.0, 1.0)",
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.UnitX)}"] = "double2(1.0, 0.0)",
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.UnitY)}"] = "double2(0.0, 1.0)",
|
||||
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.Zero)}"] = "(double3)0",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.One)}"] = "double3(1.0, 1.0, 1.0)",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.UnitX)}"] = "double3(1.0, 0.0, 0.0)",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.UnitY)}"] = "double3(0.0, 1.0, 0.0)",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.UnitZ)}"] = "double3(0.0, 0.0, 1.0)",
|
||||
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.Zero)}"] = "(double4)0",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.One)}"] = "double4(1.0, 1.0, 1.0, 1.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitX)}"] = "double4(1.0, 0.0, 0.0, 0.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitY)}"] = "double4(0.0, 1.0, 0.0, 0.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitZ)}"] = "double4(0.0, 0.0, 1.0, 0.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitW)}"] = "double4(0.0, 0.0, 0.0, 1.0)"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known samplers to HLSL resource type names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string?> KnownResourceSamplers = new Dictionary<string, string?>
|
||||
// Programmatically load mappings for the instance members of the HLSL vector types
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.KnownVectorTypes
|
||||
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
select (Type: type, Property: property))
|
||||
{
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(float).FullName}, {typeof(float).FullName}]"] = "float2",
|
||||
[$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(Float2).FullName}]"] = null,
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(float).FullName}, {typeof(float).FullName}]"] = "float2",
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(Float2).FullName}]"] = null,
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName}]"] = "float3",
|
||||
[$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(Float3).FullName}]"] = null,
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(float).FullName}, {typeof(float).FullName}, {typeof(float).FullName}]"] = "float3",
|
||||
[$"ComputeSharp.IReadOnlyTexture2D`1.this[{typeof(Float3).FullName}]"] = null
|
||||
};
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known size accessors for HLSL resource types.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, (int Rank, int Axis)> KnownSizeAccessors = new Dictionary<string, (int, int)>
|
||||
// Load mappings for the matrix properties as well
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.KnownMatrixTypes
|
||||
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where Regex.IsMatch(property.Name, "^M[1-4]{2}$")
|
||||
select (Type: type, Property: property))
|
||||
{
|
||||
["ComputeSharp.Resources.Buffer`1.Length"] = (2, 0),
|
||||
["ComputeSharp.Resources.Texture2D`1.Width"] = (2, 0),
|
||||
["ComputeSharp.Resources.Texture2D`1.Height"] = (2, 1),
|
||||
["ComputeSharp.Resources.Texture3D`1.Width"] = (3, 0),
|
||||
["ComputeSharp.Resources.Texture3D`1.Height"] = (3, 1),
|
||||
["ComputeSharp.Resources.Texture3D`1.Depth"] = (3, 2),
|
||||
["ComputeSharp.IReadOnlyTexture2D`1.Width"] = (2, 0),
|
||||
["ComputeSharp.IReadOnlyTexture2D`1.Height"] = (2, 1),
|
||||
["ComputeSharp.IReadOnlyTexture3D`1.Width"] = (3, 0),
|
||||
["ComputeSharp.IReadOnlyTexture3D`1.Height"] = (3, 1),
|
||||
["ComputeSharp.IReadOnlyTexture3D`1.Depth"] = (3, 2),
|
||||
["ComputeSharp.IReadWriteTexture2D`1.Width"] = (2, 0),
|
||||
["ComputeSharp.IReadWriteTexture2D`1.Height"] = (2, 1),
|
||||
["ComputeSharp.IReadWriteTexture3D`1.Width"] = (3, 0),
|
||||
["ComputeSharp.IReadWriteTexture3D`1.Height"] = (3, 1),
|
||||
["ComputeSharp.IReadWriteTexture3D`1.Depth"] = (3, 2)
|
||||
};
|
||||
char
|
||||
row = (char)(item.Property.Name[1] - 1),
|
||||
column = (char)(item.Property.Name[2] - 1);
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownMembers = BuildKnownMembersMap();
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"_m{row}{column}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownMembersMap()
|
||||
// Store GroupIds.Index for a quicker comparison afterwards
|
||||
PropertyInfo groupindexProperty = typeof(GroupIds).GetProperty(nameof(GroupIds.Index), BindingFlags.Static | BindingFlags.Public);
|
||||
|
||||
// Programmatically load mappings for the dispatch types
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.HlslDispatchTypes
|
||||
from property in type.GetProperties(BindingFlags.Static | BindingFlags.Public)
|
||||
select (Type: type, Property: property))
|
||||
{
|
||||
Dictionary<string, string> knownMembers = new()
|
||||
if (item.Property == groupindexProperty)
|
||||
{
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.X)}"] = "x",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Y)}"] = "y",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Zero)}"] = "(float2)0",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.One)}"] = "float2(1.0f, 1.0f)",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.UnitX)}"] = "float2(1.0f, 0.0f)",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.UnitY)}"] = "float2(0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.X)}"] = "x",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Y)}"] = "y",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Z)}"] = "z",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Zero)}"] = "(float3)0",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.One)}"] = "float3(1.0f, 1.0f, 1.0f)",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitX)}"] = "float3(1.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitY)}"] = "float3(0.0f, 1.0f, 0.0f)",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.UnitZ)}"] = "float3(0.0f, 0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.X)}"] = "x",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Y)}"] = "y",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Z)}"] = "z",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.W)}"] = "w",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Zero)}"] = "(float4)0",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.One)}"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.False)}"] = "(bool2)0",
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.True)}"] = "bool2(true, true)",
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.TrueX)}"] = "bool2(true, false)",
|
||||
[$"{typeof(Bool2).FullName}.{nameof(Bool2.TrueY)}"] = "bool2(false, true)",
|
||||
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.False)}"] = "(bool3)0",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.True)}"] = "bool3(true, true, true)",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueX)}"] = "bool3(true, false, false)",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueY)}"] = "bool3(false, true, false)",
|
||||
[$"{typeof(Bool3).FullName}.{nameof(Bool3.TrueZ)}"] = "bool3(false, false, true)",
|
||||
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.False)}"] = "(bool4)0",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.True)}"] = "bool4(true, true, true, true)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueX)}"] = "bool4(true, false, false, false)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueY)}"] = "bool4(false, true, false, false)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueZ)}"] = "bool4(false, false, true, false)",
|
||||
[$"{typeof(Bool4).FullName}.{nameof(Bool4.TrueW)}"] = "bool4(false, false, false, true)",
|
||||
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.Zero)}"] = "(int2)0",
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.One)}"] = "int2(1, 1)",
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.UnitX)}"] = "int2(1, 0)",
|
||||
[$"{typeof(Int2).FullName}.{nameof(Int2.UnitY)}"] = "int2(0, 1)",
|
||||
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.Zero)}"] = "(int3)0",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.One)}"] = "int3(1, 1, 1)",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.UnitX)}"] = "int3(1, 0, 0)",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.UnitY)}"] = "int3(0, 1, 0)",
|
||||
[$"{typeof(Int3).FullName}.{nameof(Int3.UnitZ)}"] = "int3(0, 0, 1)",
|
||||
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.Zero)}"] = "(int4)0",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.One)}"] = "int4(1, 1, 1, 1)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitX)}"] = "int4(1, 0, 0, 0)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitY)}"] = "int4(0, 1, 0, 0)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitZ)}"] = "int4(0, 0, 1, 0)",
|
||||
[$"{typeof(Int4).FullName}.{nameof(Int4.UnitW)}"] = "int4(0, 0, 0, 1)",
|
||||
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.Zero)}"] = "(uint2)0",
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.One)}"] = "uint2(1u, 1u)",
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.UnitX)}"] = "uint2(1u, 0u)",
|
||||
[$"{typeof(UInt2).FullName}.{nameof(UInt2.UnitY)}"] = "uint2(0u, 1u)",
|
||||
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.Zero)}"] = "(uint3)0",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.One)}"] = "uint3(1u, 1u, 1u)",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitX)}"] = "uint3(1u, 0u, 0u)",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitY)}"] = "uint3(0u, 1u, 0u)",
|
||||
[$"{typeof(UInt3).FullName}.{nameof(UInt3.UnitZ)}"] = "uint3(0u, 0u, 1u)",
|
||||
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.Zero)}"] = "(uint4)0",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.One)}"] = "uint4(1u, 1u, 1u, 1u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitX)}"] = "uint4(1u, 0u, 0u, 0u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitY)}"] = "uint4(0u, 1u, 0u, 0u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitZ)}"] = "uint4(0u, 0u, 1u, 0u)",
|
||||
[$"{typeof(UInt4).FullName}.{nameof(UInt4.UnitW)}"] = "uint4(0u, 0u, 0u, 1u)",
|
||||
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.Zero)}"] = "(float2)0",
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.One)}"] = "float2(1.0f, 1.0f)",
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.UnitX)}"] = "float2(1.0f, 0.0f)",
|
||||
[$"{typeof(Float2).FullName}.{nameof(Float2.UnitY)}"] = "float2(0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.Zero)}"] = "(float3)0",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.One)}"] = "float3(1.0f, 1.0f, 1.0f)",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.UnitX)}"] = "float3(1.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.UnitY)}"] = "float3(0.0f, 1.0f, 0.0f)",
|
||||
[$"{typeof(Float3).FullName}.{nameof(Float3.UnitZ)}"] = "float3(0.0f, 0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.Zero)}"] = "(float4)0",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.One)}"] = "float4(1.0f, 1.0f, 1.0f, 1.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitX)}"] = "float4(1.0f, 0.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitY)}"] = "float4(0.0f, 1.0f, 0.0f, 0.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitZ)}"] = "float4(0.0f, 0.0f, 1.0f, 0.0f)",
|
||||
[$"{typeof(Float4).FullName}.{nameof(Float4.UnitW)}"] = "float4(0.0f, 0.0f, 0.0f, 1.0f)",
|
||||
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.Zero)}"] = "(double2)0",
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.One)}"] = "double2(1.0, 1.0)",
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.UnitX)}"] = "double2(1.0, 0.0)",
|
||||
[$"{typeof(Double2).FullName}.{nameof(Double2.UnitY)}"] = "double2(0.0, 1.0)",
|
||||
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.Zero)}"] = "(double3)0",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.One)}"] = "double3(1.0, 1.0, 1.0)",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.UnitX)}"] = "double3(1.0, 0.0, 0.0)",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.UnitY)}"] = "double3(0.0, 1.0, 0.0)",
|
||||
[$"{typeof(Double3).FullName}.{nameof(Double3.UnitZ)}"] = "double3(0.0, 0.0, 1.0)",
|
||||
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.Zero)}"] = "(double4)0",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.One)}"] = "double4(1.0, 1.0, 1.0, 1.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitX)}"] = "double4(1.0, 0.0, 0.0, 0.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitY)}"] = "double4(0.0, 1.0, 0.0, 0.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitZ)}"] = "double4(0.0, 0.0, 1.0, 0.0)",
|
||||
[$"{typeof(Double4).FullName}.{nameof(Double4.UnitW)}"] = "double4(0.0, 0.0, 0.0, 1.0)"
|
||||
};
|
||||
|
||||
// Programmatically load mappings for the instance members of the HLSL vector types
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.KnownVectorTypes
|
||||
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
select (Type: type, Property: property))
|
||||
{
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Property.Name.ToLower()}");
|
||||
// The thread group index is a standalone parameter, so if this property is used, we
|
||||
// just map the access directly to that implicit and hidden parameter name instead.
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"__{nameof(GroupIds)}__get_Index");
|
||||
}
|
||||
|
||||
// Load mappings for the matrix properties as well
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.KnownMatrixTypes
|
||||
from property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
where Regex.IsMatch(property.Name, "^M[1-4]{2}$")
|
||||
select (Type: type, Property: property))
|
||||
else
|
||||
{
|
||||
char
|
||||
row = (char)(item.Property.Name[1] - 1),
|
||||
column = (char)(item.Property.Name[2] - 1);
|
||||
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"_m{row}{column}");
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Type.Name}.{item.Property.Name.ToLower()}");
|
||||
}
|
||||
}
|
||||
|
||||
// Store GroupIds.Index for a quicker comparison afterwards
|
||||
PropertyInfo groupindexProperty = typeof(GroupIds).GetProperty(nameof(GroupIds.Index), BindingFlags.Static | BindingFlags.Public);
|
||||
// Programmatically load mappings for the normalized thread ids
|
||||
foreach (var property in typeof(ThreadIds.Normalized).GetProperties(BindingFlags.Static | BindingFlags.Public))
|
||||
{
|
||||
string key = $"{typeof(ThreadIds).FullName}{Type.Delimiter}{typeof(ThreadIds.Normalized).Name}{Type.Delimiter}{property.Name}";
|
||||
|
||||
// Programmatically load mappings for the dispatch types
|
||||
foreach (var item in
|
||||
from type in HlslKnownTypes.HlslDispatchTypes
|
||||
from property in type.GetProperties(BindingFlags.Static | BindingFlags.Public)
|
||||
select (Type: type, Property: property))
|
||||
switch (property.Name)
|
||||
{
|
||||
if (item.Property == groupindexProperty)
|
||||
case string name when name.Length == 1:
|
||||
knownMembers.Add(key, $"{typeof(ThreadIds).Name}.{char.ToLower(name[0])} / (float)__{char.ToLower(name[0])}");
|
||||
break;
|
||||
case string name when name.Length == 2:
|
||||
{
|
||||
// The thread group index is a standalone parameter, so if this property is used, we
|
||||
// just map the access directly to that implicit and hidden parameter name instead.
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"__{nameof(GroupIds)}__get_Index");
|
||||
string
|
||||
numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])})",
|
||||
denominator = $"float2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})";
|
||||
|
||||
knownMembers.Add(key, $"{numerator} / {denominator}");
|
||||
break;
|
||||
}
|
||||
else
|
||||
case string name when name.Length == 3:
|
||||
{
|
||||
knownMembers.Add($"{item.Type.FullName}{Type.Delimiter}{item.Property.Name}", $"{item.Type.Name}.{item.Property.Name.ToLower()}");
|
||||
string
|
||||
numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])}, {typeof(ThreadIds).Name}.{char.ToLower(name[2])})",
|
||||
denominator = $"float3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})";
|
||||
|
||||
knownMembers.Add(key, $"{numerator} / {denominator}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Programmatically load mappings for the normalized thread ids
|
||||
foreach (var property in typeof(ThreadIds.Normalized).GetProperties(BindingFlags.Static | BindingFlags.Public))
|
||||
// Programmatically load mappings for the group size
|
||||
foreach (var property in typeof(GroupSize).GetProperties(BindingFlags.Static | BindingFlags.Public))
|
||||
{
|
||||
string key = $"{typeof(GroupSize).FullName}{Type.Delimiter}{property.Name}";
|
||||
|
||||
switch (property.Name)
|
||||
{
|
||||
string key = $"{typeof(ThreadIds).FullName}{Type.Delimiter}{typeof(ThreadIds.Normalized).Name}{Type.Delimiter}{property.Name}";
|
||||
|
||||
switch (property.Name)
|
||||
{
|
||||
case string name when name.Length == 1:
|
||||
knownMembers.Add(key, $"{typeof(ThreadIds).Name}.{char.ToLower(name[0])} / (float)__{char.ToLower(name[0])}");
|
||||
break;
|
||||
case string name when name.Length == 2:
|
||||
{
|
||||
string
|
||||
numerator = $"float2({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])})",
|
||||
denominator = $"float2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})";
|
||||
|
||||
knownMembers.Add(key, $"{numerator} / {denominator}");
|
||||
break;
|
||||
}
|
||||
case string name when name.Length == 3:
|
||||
{
|
||||
string
|
||||
numerator = $"float3({typeof(ThreadIds).Name}.{char.ToLower(name[0])}, {typeof(ThreadIds).Name}.{char.ToLower(name[1])}, {typeof(ThreadIds).Name}.{char.ToLower(name[2])})",
|
||||
denominator = $"float3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})";
|
||||
|
||||
knownMembers.Add(key, $"{numerator} / {denominator}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
case nameof(GroupSize.Count):
|
||||
knownMembers.Add(key, "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z");
|
||||
break;
|
||||
case string name when name.Length == 1:
|
||||
knownMembers.Add(key, $"__GroupSize__get_{name}");
|
||||
break;
|
||||
case string name when name.Length == 2:
|
||||
knownMembers.Add(key, $"int2(__GroupSize__get_{name[0]}, __GroupSize__get_{name[1]})");
|
||||
break;
|
||||
case string name when name.Length == 3:
|
||||
knownMembers.Add(key, $"int3(__GroupSize__get_{name[0]}, __GroupSize__get_{name[1]}, __GroupSize__get_{name[2]})");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Programmatically load mappings for the group size
|
||||
foreach (var property in typeof(GroupSize).GetProperties(BindingFlags.Static | BindingFlags.Public))
|
||||
// Programmatically load mappings for the dispatch size
|
||||
foreach (var property in typeof(DispatchSize).GetProperties(BindingFlags.Static | BindingFlags.Public))
|
||||
{
|
||||
string key = $"{typeof(DispatchSize).FullName}{Type.Delimiter}{property.Name}";
|
||||
|
||||
switch (property.Name)
|
||||
{
|
||||
string key = $"{typeof(GroupSize).FullName}{Type.Delimiter}{property.Name}";
|
||||
|
||||
switch (property.Name)
|
||||
{
|
||||
case nameof(GroupSize.Count):
|
||||
knownMembers.Add(key, "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z");
|
||||
break;
|
||||
case string name when name.Length == 1:
|
||||
knownMembers.Add(key, $"__GroupSize__get_{name}");
|
||||
break;
|
||||
case string name when name.Length == 2:
|
||||
knownMembers.Add(key, $"int2(__GroupSize__get_{name[0]}, __GroupSize__get_{name[1]})");
|
||||
break;
|
||||
case string name when name.Length == 3:
|
||||
knownMembers.Add(key, $"int3(__GroupSize__get_{name[0]}, __GroupSize__get_{name[1]}, __GroupSize__get_{name[2]})");
|
||||
break;
|
||||
}
|
||||
case nameof(DispatchSize.Count):
|
||||
knownMembers.Add(key, "__x * __y * __z");
|
||||
break;
|
||||
case string name when name.Length == 1:
|
||||
knownMembers.Add(key, $"__{char.ToLower(name[0])}");
|
||||
break;
|
||||
case string name when name.Length == 2:
|
||||
knownMembers.Add(key, $"int2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})");
|
||||
break;
|
||||
case string name when name.Length == 3:
|
||||
knownMembers.Add(key, $"int3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})");
|
||||
break;
|
||||
}
|
||||
|
||||
// Programmatically load mappings for the dispatch size
|
||||
foreach (var property in typeof(DispatchSize).GetProperties(BindingFlags.Static | BindingFlags.Public))
|
||||
{
|
||||
string key = $"{typeof(DispatchSize).FullName}{Type.Delimiter}{property.Name}";
|
||||
|
||||
switch (property.Name)
|
||||
{
|
||||
case nameof(DispatchSize.Count):
|
||||
knownMembers.Add(key, "__x * __y * __z");
|
||||
break;
|
||||
case string name when name.Length == 1:
|
||||
knownMembers.Add(key, $"__{char.ToLower(name[0])}");
|
||||
break;
|
||||
case string name when name.Length == 2:
|
||||
knownMembers.Add(key, $"int2(__{char.ToLower(name[0])}, __{char.ToLower(name[1])})");
|
||||
break;
|
||||
case string name when name.Length == 3:
|
||||
knownMembers.Add(key, $"int3(__{char.ToLower(name[0])}, __{char.ToLower(name[1])}, __{char.ToLower(name[2])})");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return knownMembers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownMatrixIndexers = BuildKnownMatrixIndexers();
|
||||
return knownMembers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of swizzled matrix indexer properties.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownMatrixIndexers()
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownMatrixIndexers = BuildKnownMatrixIndexers();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of swizzled matrix indexer properties.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownMatrixIndexers()
|
||||
{
|
||||
return (
|
||||
from type in Assembly.GetExecutingAssembly().ExportedTypes
|
||||
where Regex.IsMatch(type.FullName, @"ComputeSharp\.(Bool|Double|Float|Int|UInt)[1-4]x[1-4]")
|
||||
from property in type.GetProperties()
|
||||
let indices = property.GetIndexParameters()
|
||||
where indices.Length > 0 &&
|
||||
indices.All(static index => index.ParameterType == typeof(MatrixIndex))
|
||||
let metadataName = $"{type.FullName}.this[{string.Join(", ", indices.Select(index => index.ParameterType))}]"
|
||||
select metadataName).ToImmutableHashSet();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownMatrixIndices = BuildKnownMatrixIndices();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of swizzled matrix indices.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownMatrixIndices()
|
||||
{
|
||||
return (
|
||||
from name in Enum.GetNames(typeof(MatrixIndex))
|
||||
select $"{typeof(MatrixIndex).FullName}.{name}").ToImmutableHashSet();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given property fullname matches a matrix swizzled indexer.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled indexer.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixIndexer(string name)
|
||||
{
|
||||
return KnownMatrixIndexers.Contains(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given property fullname matches a matrix swizzled index.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled index.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixIndex(string name)
|
||||
{
|
||||
return KnownMatrixIndices.Contains(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible member name for the input member name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <param name="mapped">The mapped name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible member name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
return KnownMembers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible indexer resource type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedResourceIndexerTypeName(string name, out string? mapped)
|
||||
{
|
||||
return KnownResourceIndexers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible sampler resource type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given sampler.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedResourceSamplerAccessType(string name, out string? mapped)
|
||||
{
|
||||
return KnownResourceSamplers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetAccessorRankAndAxis(string name, out int rank, out int axis)
|
||||
{
|
||||
if (KnownSizeAccessors.TryGetValue(name, out var info))
|
||||
{
|
||||
return (
|
||||
from type in Assembly.GetExecutingAssembly().ExportedTypes
|
||||
where Regex.IsMatch(type.FullName, @"ComputeSharp\.(Bool|Double|Float|Int|UInt)[1-4]x[1-4]")
|
||||
from property in type.GetProperties()
|
||||
let indices = property.GetIndexParameters()
|
||||
where indices.Length > 0 &&
|
||||
indices.All(static index => index.ParameterType == typeof(MatrixIndex))
|
||||
let metadataName = $"{type.FullName}.this[{string.Join(", ", indices.Select(index => index.ParameterType))}]"
|
||||
select metadataName).ToImmutableHashSet();
|
||||
(rank, axis) = info;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known members to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyCollection<string> KnownMatrixIndices = BuildKnownMatrixIndices();
|
||||
(rank, axis) = default((int, int));
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of swizzled matrix indices.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyCollection<string> BuildKnownMatrixIndices()
|
||||
{
|
||||
return (
|
||||
from name in Enum.GetNames(typeof(MatrixIndex))
|
||||
select $"{typeof(MatrixIndex).FullName}.{name}").ToImmutableHashSet();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given property fullname matches a matrix swizzled indexer.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled indexer.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixIndexer(string name)
|
||||
{
|
||||
return KnownMatrixIndexers.Contains(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given property fullname matches a matrix swizzled index.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <returns>Whether or not <paramref name="name"/> is a matrix swizzled index.</returns>
|
||||
[Pure]
|
||||
public static bool IsKnownMatrixIndex(string name)
|
||||
{
|
||||
return KnownMatrixIndices.Contains(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible member name for the input member name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified member name.</param>
|
||||
/// <param name="mapped">The mapped name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible member name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
return KnownMembers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible indexer resource type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedResourceIndexerTypeName(string name, out string? mapped)
|
||||
{
|
||||
return KnownResourceIndexers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible sampler resource type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given sampler.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedResourceSamplerAccessType(string name, out string? mapped)
|
||||
{
|
||||
return KnownResourceSamplers.TryGetValue(name, out mapped);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible indexer vector type name for the input indexer name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified indexer name.</param>
|
||||
/// <param name="mapped">The mapped type name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader for the given indexer.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetAccessorRankAndAxis(string name, out int rank, out int axis)
|
||||
{
|
||||
if (KnownSizeAccessors.TryGetValue(name, out var info))
|
||||
{
|
||||
(rank, axis) = info;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
(rank, axis) = default((int, int));
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
@@ -6,152 +6,151 @@ using System.Numerics;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Core.Intrinsics.Attributes;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
namespace ComputeSharp.SourceGenerators.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL method names to common .NET methods.
|
||||
/// </summary>
|
||||
internal static class HlslKnownMethods
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL method names to common .NET methods.
|
||||
/// The mapping of supported known methods to HLSL names.
|
||||
/// </summary>
|
||||
internal static class HlslKnownMethods
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownMethods = BuildKnownMethodsMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known methods to HLSL names.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownMethodsMap()
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known methods to HLSL names.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownMethods = BuildKnownMethodsMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of supported known methods to HLSL names.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownMethodsMap()
|
||||
Dictionary<string, string> knownMethods = new()
|
||||
{
|
||||
Dictionary<string, string> knownMethods = new()
|
||||
{
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Abs)}"] = "abs",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Max)}"] = "max",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Min)}"] = "min",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Pow)}"] = "pow",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sin)}"] = "sin",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sinh)}"] = "sinh",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Asin)}"] = "asin",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Cos)}"] = "cos",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Cosh)}"] = "cosh",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Acos)}"] = "acos",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Tan)}"] = "tan",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Tanh)}"] = "tanh",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Atan)}"] = "atan",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Atan2)}"] = "atan2",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Ceiling)}"] = "ceil",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Floor)}"] = "floor",
|
||||
[$"{typeof(Math).FullName}.Clamp"] = "clamp",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Exp)}"] = "exp",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Log)}"] = "log",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Log10)}"] = "log10",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Max)}"] = "max",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Min)}"] = "min",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Round)}"] = "round",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sqrt)}"] = "sqrt",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sign)}"] = "sign",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Truncate)}"] = "trunc",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Abs)}"] = "abs",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Max)}"] = "max",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Min)}"] = "min",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Pow)}"] = "pow",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sin)}"] = "sin",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sinh)}"] = "sinh",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Asin)}"] = "asin",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Cos)}"] = "cos",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Cosh)}"] = "cosh",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Acos)}"] = "acos",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Tan)}"] = "tan",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Tanh)}"] = "tanh",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Atan)}"] = "atan",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Atan2)}"] = "atan2",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Ceiling)}"] = "ceil",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Floor)}"] = "floor",
|
||||
[$"{typeof(Math).FullName}.Clamp"] = "clamp",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Exp)}"] = "exp",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Log)}"] = "log",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Log10)}"] = "log10",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Max)}"] = "max",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Min)}"] = "min",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Round)}"] = "round",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sqrt)}"] = "sqrt",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Sign)}"] = "sign",
|
||||
[$"{typeof(Math).FullName}.{nameof(Math.Truncate)}"] = "trunc",
|
||||
|
||||
["System.MathF.Abs"] = "abs",
|
||||
["System.MathF.Max"] = "max",
|
||||
["System.MathF.Min"] = "min",
|
||||
["System.MathF.Pow"] = "pow",
|
||||
["System.MathF.Sin"] = "sin",
|
||||
["System.MathF.Sinh"] = "sinh",
|
||||
["System.MathF.Asin"] = "asin",
|
||||
["System.MathF.Cos"] = "cos",
|
||||
["System.MathF.Cosh"] = "cosh",
|
||||
["System.MathF.Acos"] = "acos",
|
||||
["System.MathF.Tan"] = "tan",
|
||||
["System.MathF.Tanh"] = "tanh",
|
||||
["System.MathF.Atan"] = "atan",
|
||||
["System.MathF.Atan2"] = "atan2",
|
||||
["System.MathF.Ceiling"] = "ceil",
|
||||
["System.MathF.Floor"] = "floor",
|
||||
["System.MathF.Clamp"] = "clamp",
|
||||
["System.MathF.Exp"] = "exp",
|
||||
["System.MathF.Log"] = "log",
|
||||
["System.MathF.Log10"] = "log10",
|
||||
["System.MathF.Max"] = "max",
|
||||
["System.MathF.Min"] = "min",
|
||||
["System.MathF.Round"] = "round",
|
||||
["System.MathF.Sqrt"] = "sqrt",
|
||||
["System.MathF.Sign"] = "sign",
|
||||
["System.MathF.Truncate"] = "trunc",
|
||||
["System.MathF.Abs"] = "abs",
|
||||
["System.MathF.Max"] = "max",
|
||||
["System.MathF.Min"] = "min",
|
||||
["System.MathF.Pow"] = "pow",
|
||||
["System.MathF.Sin"] = "sin",
|
||||
["System.MathF.Sinh"] = "sinh",
|
||||
["System.MathF.Asin"] = "asin",
|
||||
["System.MathF.Cos"] = "cos",
|
||||
["System.MathF.Cosh"] = "cosh",
|
||||
["System.MathF.Acos"] = "acos",
|
||||
["System.MathF.Tan"] = "tan",
|
||||
["System.MathF.Tanh"] = "tanh",
|
||||
["System.MathF.Atan"] = "atan",
|
||||
["System.MathF.Atan2"] = "atan2",
|
||||
["System.MathF.Ceiling"] = "ceil",
|
||||
["System.MathF.Floor"] = "floor",
|
||||
["System.MathF.Clamp"] = "clamp",
|
||||
["System.MathF.Exp"] = "exp",
|
||||
["System.MathF.Log"] = "log",
|
||||
["System.MathF.Log10"] = "log10",
|
||||
["System.MathF.Max"] = "max",
|
||||
["System.MathF.Min"] = "min",
|
||||
["System.MathF.Round"] = "round",
|
||||
["System.MathF.Sqrt"] = "sqrt",
|
||||
["System.MathF.Sign"] = "sign",
|
||||
["System.MathF.Truncate"] = "trunc",
|
||||
|
||||
[$"{typeof(float).FullName}.IsFinite"] = "isfinite",
|
||||
[$"{typeof(float).FullName}.{nameof(float.IsInfinity)}"] = "isinf",
|
||||
[$"{typeof(float).FullName}.{nameof(float.IsNaN)}"] = "isnan",
|
||||
[$"{typeof(float).FullName}.IsFinite"] = "isfinite",
|
||||
[$"{typeof(float).FullName}.{nameof(float.IsInfinity)}"] = "isinf",
|
||||
[$"{typeof(float).FullName}.{nameof(float.IsNaN)}"] = "isnan",
|
||||
|
||||
[$"{typeof(double).FullName}.IsFinite"] = "isfinite",
|
||||
[$"{typeof(double).FullName}.{nameof(double.IsInfinity)}"] = "isinf",
|
||||
[$"{typeof(double).FullName}.{nameof(double.IsNaN)}"] = "isnan",
|
||||
[$"{typeof(double).FullName}.IsFinite"] = "isfinite",
|
||||
[$"{typeof(double).FullName}.{nameof(double.IsInfinity)}"] = "isinf",
|
||||
[$"{typeof(double).FullName}.{nameof(double.IsNaN)}"] = "isnan",
|
||||
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Abs)}"] = "abs",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Clamp)}"] = "clamp",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Distance)}"] = "distance",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Dot)}"] = "dot",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Lerp)}"] = "lerp",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Max)}"] = "max",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Min)}"] = "min",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Reflect)}"] = "reflect",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Transform)}"] = "mul",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.TransformNormal)}"] = "mul",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Normalize)}"] = "normalize",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Abs)}"] = "abs",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Clamp)}"] = "clamp",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Distance)}"] = "distance",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Dot)}"] = "dot",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Lerp)}"] = "lerp",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Max)}"] = "max",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Min)}"] = "min",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Reflect)}"] = "reflect",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Transform)}"] = "mul",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.TransformNormal)}"] = "mul",
|
||||
[$"{typeof(Vector2).FullName}.{nameof(Vector2.Normalize)}"] = "normalize",
|
||||
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Abs)}"] = "abs",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Clamp)}"] = "clamp",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Cross)}"] = "cross",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Distance)}"] = "distance",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Dot)}"] = "dot",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Lerp)}"] = "lerp",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Max)}"] = "max",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Min)}"] = "min",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Reflect)}"] = "reflect",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Transform)}"] = "mul",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.TransformNormal)}"] = "mul",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Normalize)}"] = "normalize",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Abs)}"] = "abs",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Clamp)}"] = "clamp",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Cross)}"] = "cross",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Distance)}"] = "distance",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Dot)}"] = "dot",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Lerp)}"] = "lerp",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Max)}"] = "max",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Min)}"] = "min",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Reflect)}"] = "reflect",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Transform)}"] = "mul",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.TransformNormal)}"] = "mul",
|
||||
[$"{typeof(Vector3).FullName}.{nameof(Vector3.Normalize)}"] = "normalize",
|
||||
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Abs)}"] = "abs",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Clamp)}"] = "clamp",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Distance)}"] = "distance",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Dot)}"] = "dot",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Lerp)}"] = "lerp",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Max)}"] = "max",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Min)}"] = "min",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Transform)}"] = "mul",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Normalize)}"] = "normalize"
|
||||
};
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Abs)}"] = "abs",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Clamp)}"] = "clamp",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Distance)}"] = "distance",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Dot)}"] = "dot",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Lerp)}"] = "lerp",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Max)}"] = "max",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Min)}"] = "min",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Transform)}"] = "mul",
|
||||
[$"{typeof(Vector4).FullName}.{nameof(Vector4.Normalize)}"] = "normalize"
|
||||
};
|
||||
|
||||
// Programmatically load mappings from the Hlsl class as well
|
||||
foreach (var method in
|
||||
from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)
|
||||
group method by method.Name
|
||||
into groups
|
||||
select groups.First())
|
||||
{
|
||||
// Check whether the current method should be translated with the original name
|
||||
// or with the lowercase version. This is needed because all C# methods are exposed
|
||||
// with the upper camel case format, while HLSL intrinsics use multiple different formats.
|
||||
string hlslName = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
// Programmatically load mappings from the Hlsl class as well
|
||||
foreach (var method in
|
||||
from method in typeof(Hlsl).GetMethods(BindingFlags.Public | BindingFlags.Static)
|
||||
group method by method.Name
|
||||
into groups
|
||||
select groups.First())
|
||||
{
|
||||
// Check whether the current method should be translated with the original name
|
||||
// or with the lowercase version. This is needed because all C# methods are exposed
|
||||
// with the upper camel case format, while HLSL intrinsics use multiple different formats.
|
||||
string hlslName = method.GetCustomAttribute<HlslIntrinsicNameAttribute>()?.Name ?? method.Name;
|
||||
|
||||
knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName);
|
||||
}
|
||||
|
||||
return knownMethods;
|
||||
knownMethods.Add($"{typeof(Hlsl).FullName}{Type.Delimiter}{method.Name}", hlslName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible method name for the input method name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified method name.</param>
|
||||
/// <param name="mapped">The mapped name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible member name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
return KnownMethods.TryGetValue(name, out mapped);
|
||||
}
|
||||
return knownMethods;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the mapped HLSL-compatible method name for the input method name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input fully qualified method name.</param>
|
||||
/// <param name="mapped">The mapped name, if one is found.</param>
|
||||
/// <returns>The HLSL-compatible member name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static bool TryGetMappedName(string name, out string? mapped)
|
||||
{
|
||||
return KnownMethods.TryGetValue(name, out mapped);
|
||||
}
|
||||
}
|
||||
@@ -5,58 +5,57 @@ using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
namespace ComputeSharp.SourceGenerators.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL types to their alignment sizes.
|
||||
/// </summary>
|
||||
internal static class HlslKnownSizes
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL types to their alignment sizes.
|
||||
/// The mapping of supported known sizes to HLSL type names.
|
||||
/// </summary>
|
||||
internal static class HlslKnownSizes
|
||||
private static IReadOnlyDictionary<string, (int, int)> KnownSizes = BuildKnownSizesMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of known type sizes and alignments.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, (int, int)> BuildKnownSizesMap()
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of supported known sizes to HLSL type names.
|
||||
/// </summary>
|
||||
private static IReadOnlyDictionary<string, (int, int)> KnownSizes = BuildKnownSizesMap();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of known type sizes and alignments.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, (int, int)> BuildKnownSizesMap()
|
||||
Dictionary<string, (int, int)> knownSizes = new()
|
||||
{
|
||||
Dictionary<string, (int, int)> 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(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)
|
||||
};
|
||||
[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;
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size and alignment info for a specified primitive type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The type name to get info for.</param>
|
||||
/// <returns>The size and and alignment for the input type.</returns>
|
||||
[Pure]
|
||||
public static (int Size, int Pack) GetTypeInfo(string typeName)
|
||||
{
|
||||
return KnownSizes[typeName];
|
||||
}
|
||||
return knownSizes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size and alignment info for a specified primitive type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The type name to get info for.</param>
|
||||
/// <returns>The size and and alignment for the input type.</returns>
|
||||
[Pure]
|
||||
public static (int Size, int Pack) GetTypeInfo(string typeName)
|
||||
{
|
||||
return KnownSizes[typeName];
|
||||
}
|
||||
}
|
||||
@@ -9,301 +9,253 @@ using Microsoft.CodeAnalysis;
|
||||
|
||||
#pragma warning disable RS1024
|
||||
|
||||
namespace ComputeSharp.SourceGenerators.Mappings
|
||||
namespace ComputeSharp.SourceGenerators.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL type names to common .NET types.
|
||||
/// </summary>
|
||||
internal static class HlslKnownTypes
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains and maps known HLSL type names to common .NET types.
|
||||
/// Gets the known HLSL dispatch types.
|
||||
/// </summary>
|
||||
internal static class HlslKnownTypes
|
||||
public static IReadOnlyCollection<Type> HlslDispatchTypes { get; } = new[]
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the known HLSL dispatch types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> HlslDispatchTypes { get; } = new[]
|
||||
typeof(ThreadIds),
|
||||
typeof(GroupIds),
|
||||
typeof(GridIds)
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the set of HLSL vector types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> KnownVectorTypes { get; } = new[]
|
||||
{
|
||||
typeof(Bool2), typeof(Bool3), typeof(Bool4),
|
||||
typeof(Int2), typeof(Int3), typeof(Int4),
|
||||
typeof(UInt2), typeof(UInt3), typeof(UInt4),
|
||||
typeof(Float2), typeof(Float3), typeof(Float4),
|
||||
typeof(Double2), typeof(Double3), typeof(Double4)
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the set of HLSL matrix types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> KnownMatrixTypes { get; } = new[]
|
||||
{
|
||||
typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4),
|
||||
typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4),
|
||||
typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4),
|
||||
typeof(Bool4x1), typeof(Bool4x2), typeof(Bool4x3), typeof(Bool4x4),
|
||||
typeof(Int1x1), typeof(Int1x2), typeof(Int1x3), typeof(Int1x4),
|
||||
typeof(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4),
|
||||
typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4),
|
||||
typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4),
|
||||
typeof(UInt1x1), typeof(UInt1x2), typeof(UInt1x3), typeof(UInt1x4),
|
||||
typeof(UInt2x1), typeof(UInt2x2), typeof(UInt2x3), typeof(UInt2x4),
|
||||
typeof(UInt3x1), typeof(UInt3x2), typeof(UInt3x3), typeof(UInt3x4),
|
||||
typeof(UInt4x1), typeof(UInt4x2), typeof(UInt4x3), typeof(UInt4x4),
|
||||
typeof(Float1x1), typeof(Float1x2), typeof(Float1x3), typeof(Float1x4),
|
||||
typeof(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4),
|
||||
typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4),
|
||||
typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4),
|
||||
typeof(Double1x1), typeof(Double1x2), typeof(Double1x3), typeof(Double1x4),
|
||||
typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4),
|
||||
typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4),
|
||||
typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4)
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known types to HLSL types.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownHlslTypes = BuildKnownHlslTypes();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of known primitive types.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownHlslTypes()
|
||||
{
|
||||
Dictionary<string, string> knownTypes = new()
|
||||
{
|
||||
typeof(ThreadIds),
|
||||
typeof(GroupIds),
|
||||
typeof(GridIds)
|
||||
[typeof(bool).FullName] = "bool",
|
||||
[typeof(int).FullName] = "int",
|
||||
[typeof(uint).FullName] = "uint",
|
||||
[typeof(float).FullName] = "float",
|
||||
[typeof(Vector2).FullName] = "float2",
|
||||
[typeof(Vector3).FullName] = "float3",
|
||||
[typeof(Vector4).FullName] = "float4",
|
||||
[typeof(double).FullName] = "double"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the set of HLSL vector types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> KnownVectorTypes { get; } = new[]
|
||||
// Add all the vector types
|
||||
foreach (var type in KnownVectorTypes)
|
||||
{
|
||||
typeof(Bool2), typeof(Bool3), typeof(Bool4),
|
||||
typeof(Int2), typeof(Int3), typeof(Int4),
|
||||
typeof(UInt2), typeof(UInt3), typeof(UInt4),
|
||||
typeof(Float2), typeof(Float3), typeof(Float4),
|
||||
typeof(Double2), typeof(Double3), typeof(Double4)
|
||||
knownTypes.Add(type.FullName, type.Name.ToLower());
|
||||
}
|
||||
|
||||
// Add all the matrix types
|
||||
foreach (var type in KnownMatrixTypes)
|
||||
{
|
||||
knownTypes.Add(type.FullName, type.Name.ToLower());
|
||||
}
|
||||
|
||||
return knownTypes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a constant buffer type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a constant buffer type.</returns>
|
||||
public static bool IsConstantBufferType(string typeName)
|
||||
{
|
||||
return typeName == "ComputeSharp.ConstantBuffer`1";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a structured buffer type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a structured buffer type.</returns>
|
||||
public static bool IsStructuredBufferType(string typeName)
|
||||
{
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ConstantBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyBuffer`1" or
|
||||
"ComputeSharp.ReadWriteBuffer`1" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the set of HLSL matrix types.
|
||||
/// </summary>
|
||||
public static IReadOnlyCollection<Type> KnownMatrixTypes { get; } = new[]
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a readonly typed resource type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a readonly typed resource type.</returns>
|
||||
public static bool IsReadOnlyTypedResourceType(string typeName)
|
||||
{
|
||||
return typeName switch
|
||||
{
|
||||
typeof(Bool1x1), typeof(Bool1x2), typeof(Bool1x3), typeof(Bool1x4),
|
||||
typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4),
|
||||
typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4),
|
||||
typeof(Bool4x1), typeof(Bool4x2), typeof(Bool4x3), typeof(Bool4x4),
|
||||
typeof(Int1x1), typeof(Int1x2), typeof(Int1x3), typeof(Int1x4),
|
||||
typeof(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4),
|
||||
typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4),
|
||||
typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4),
|
||||
typeof(UInt1x1), typeof(UInt1x2), typeof(UInt1x3), typeof(UInt1x4),
|
||||
typeof(UInt2x1), typeof(UInt2x2), typeof(UInt2x3), typeof(UInt2x4),
|
||||
typeof(UInt3x1), typeof(UInt3x2), typeof(UInt3x3), typeof(UInt3x4),
|
||||
typeof(UInt4x1), typeof(UInt4x2), typeof(UInt4x3), typeof(UInt4x4),
|
||||
typeof(Float1x1), typeof(Float1x2), typeof(Float1x3), typeof(Float1x4),
|
||||
typeof(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4),
|
||||
typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4),
|
||||
typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4),
|
||||
typeof(Double1x1), typeof(Double1x2), typeof(Double1x3), typeof(Double1x4),
|
||||
typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4),
|
||||
typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4),
|
||||
typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4)
|
||||
"ComputeSharp.ReadOnlyBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`2" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`2" or
|
||||
"ComputeSharp.IReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.IReadOnlyTexture3D`1" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The mapping of supported known types to HLSL types.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyDictionary<string, string> KnownHlslTypes = BuildKnownHlslTypes();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the mapping of known primitive types.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private static IReadOnlyDictionary<string, string> BuildKnownHlslTypes()
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a typed resource type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
|
||||
public static bool IsTypedResourceType(string typeName)
|
||||
{
|
||||
return typeName switch
|
||||
{
|
||||
Dictionary<string, string> knownTypes = new()
|
||||
{
|
||||
[typeof(bool).FullName] = "bool",
|
||||
[typeof(int).FullName] = "int",
|
||||
[typeof(uint).FullName] = "uint",
|
||||
[typeof(float).FullName] = "float",
|
||||
[typeof(Vector2).FullName] = "float2",
|
||||
[typeof(Vector3).FullName] = "float3",
|
||||
[typeof(Vector4).FullName] = "float4",
|
||||
[typeof(double).FullName] = "double"
|
||||
};
|
||||
"ComputeSharp.ConstantBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyBuffer`1" or
|
||||
"ComputeSharp.ReadWriteBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`2" or
|
||||
"ComputeSharp.ReadWriteTexture2D`1" or
|
||||
"ComputeSharp.ReadWriteTexture2D`2" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`2" or
|
||||
"ComputeSharp.ReadWriteTexture3D`1" or
|
||||
"ComputeSharp.ReadWriteTexture3D`2" or
|
||||
"ComputeSharp.IReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.IReadWriteTexture2D`1" or
|
||||
"ComputeSharp.IReadOnlyTexture3D`1" or
|
||||
"ComputeSharp.IReadWriteTexture3D`1" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
// Add all the vector types
|
||||
foreach (var type in KnownVectorTypes)
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a known HLSL primitive type (scalar, vector or matrix).
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a scalar, vector or matrix type.</returns>
|
||||
public static bool IsKnownHlslType(string typeName)
|
||||
{
|
||||
return KnownHlslTypes.ContainsKey(typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a vector type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a vector type.</returns>
|
||||
public static bool IsVectorType(string typeName)
|
||||
{
|
||||
return KnownVectorTypes.Any(type => type.FullName == typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a matrix type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a matrix type.</returns>
|
||||
public static bool IsMatrixType(string typeName)
|
||||
{
|
||||
return KnownMatrixTypes.Any(type => type.FullName == typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name is a non linear matrix type.
|
||||
/// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <param name="elementName">The element name of the matrix type.</param>
|
||||
/// <param name="rows">The number of rows in the matrix type.</param>
|
||||
/// <param name="columns">The number of columns in the matrix type.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a non linear matrix type.</returns>
|
||||
public static bool IsNonLinearMatrixType(string typeName, out string? elementName, out int rows, out int columns)
|
||||
{
|
||||
if (KnownMatrixTypes.Any(type => type.FullName == typeName))
|
||||
{
|
||||
var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
knownTypes.Add(type.FullName, type.Name.ToLower());
|
||||
elementName = match.Groups[1].Value;
|
||||
rows = int.Parse(match.Groups[2].Value);
|
||||
columns = int.Parse(match.Groups[3].Value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Add all the matrix types
|
||||
foreach (var type in KnownMatrixTypes)
|
||||
{
|
||||
knownTypes.Add(type.FullName, type.Name.ToLower());
|
||||
}
|
||||
|
||||
return knownTypes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a constant buffer type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a constant buffer type.</returns>
|
||||
public static bool IsConstantBufferType(string typeName)
|
||||
elementName = null;
|
||||
rows = columns = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type symbol.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The input type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static string GetMappedName(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
// Delegate types just return an empty string, as they're not actually
|
||||
// used in the generated shaders, but just mapped to a function at runtime.
|
||||
if (typeSymbol.TypeKind == TypeKind.Delegate) return "";
|
||||
|
||||
string typeName = typeSymbol.GetFullMetadataName();
|
||||
|
||||
// Special case for the resource types
|
||||
if (IsTypedResourceType(typeName))
|
||||
{
|
||||
return typeName == "ComputeSharp.ConstantBuffer`1";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a structured buffer type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a structured buffer type.</returns>
|
||||
public static bool IsStructuredBufferType(string typeName)
|
||||
{
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ConstantBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyBuffer`1" or
|
||||
"ComputeSharp.ReadWriteBuffer`1" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a readonly typed resource type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a readonly typed resource type.</returns>
|
||||
public static bool IsReadOnlyTypedResourceType(string typeName)
|
||||
{
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ReadOnlyBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`2" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`2" or
|
||||
"ComputeSharp.IReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.IReadOnlyTexture3D`1" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a typed resource type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
|
||||
public static bool IsTypedResourceType(string typeName)
|
||||
{
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ConstantBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyBuffer`1" or
|
||||
"ComputeSharp.ReadWriteBuffer`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture2D`2" or
|
||||
"ComputeSharp.ReadWriteTexture2D`1" or
|
||||
"ComputeSharp.ReadWriteTexture2D`2" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`1" or
|
||||
"ComputeSharp.ReadOnlyTexture3D`2" or
|
||||
"ComputeSharp.ReadWriteTexture3D`1" or
|
||||
"ComputeSharp.ReadWriteTexture3D`2" or
|
||||
"ComputeSharp.IReadOnlyTexture2D`1" or
|
||||
"ComputeSharp.IReadWriteTexture2D`1" or
|
||||
"ComputeSharp.IReadOnlyTexture3D`1" or
|
||||
"ComputeSharp.IReadWriteTexture3D`1" => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a known HLSL primitive type (scalar, vector or matrix).
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a scalar, vector or matrix type.</returns>
|
||||
public static bool IsKnownHlslType(string typeName)
|
||||
{
|
||||
return KnownHlslTypes.ContainsKey(typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a vector type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a vector type.</returns>
|
||||
public static bool IsVectorType(string typeName)
|
||||
{
|
||||
return KnownVectorTypes.Any(type => type.FullName == typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name matches a matrix type.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a matrix type.</returns>
|
||||
public static bool IsMatrixType(string typeName)
|
||||
{
|
||||
return KnownMatrixTypes.Any(type => type.FullName == typeName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type name is a non linear matrix type.
|
||||
/// That is, a matrix type with more than a single row (which affects the constant buffer alignment).
|
||||
/// </summary>
|
||||
/// <param name="typeName">The input type name to check.</param>
|
||||
/// <param name="elementName">The element name of the matrix type.</param>
|
||||
/// <param name="rows">The number of rows in the matrix type.</param>
|
||||
/// <param name="columns">The number of columns in the matrix type.</param>
|
||||
/// <returns>Whether or not <paramref name="typeName"/> represents a non linear matrix type.</returns>
|
||||
public static bool IsNonLinearMatrixType(string typeName, out string? elementName, out int rows, out int columns)
|
||||
{
|
||||
if (KnownMatrixTypes.Any(type => type.FullName == typeName))
|
||||
{
|
||||
var match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$");
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
elementName = match.Groups[1].Value;
|
||||
rows = int.Parse(match.Groups[2].Value);
|
||||
columns = int.Parse(match.Groups[3].Value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
elementName = null;
|
||||
rows = columns = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type symbol.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The input type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static string GetMappedName(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
// Delegate types just return an empty string, as they're not actually
|
||||
// used in the generated shaders, but just mapped to a function at runtime.
|
||||
if (typeSymbol.TypeKind == TypeKind.Delegate) return "";
|
||||
|
||||
string typeName = typeSymbol.GetFullMetadataName();
|
||||
|
||||
// Special case for the resource types
|
||||
if (IsTypedResourceType(typeName))
|
||||
{
|
||||
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullMetadataName();
|
||||
|
||||
// If the current type is a custom type, format it as needed
|
||||
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType))
|
||||
{
|
||||
mappedElementType = genericArgumentName.ToHlslIdentifierName();
|
||||
}
|
||||
|
||||
// Construct the HLSL type name
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ConstantBuffer`1" => mappedElementType,
|
||||
"ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture2D`2" => $"Texture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture2D`2" => $"RWTexture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture3D`2" => $"Texture3D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture3D`2" => $"RWTexture3D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadOnlyTexture2D`1" => $"Texture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadWriteTexture2D`1" => $"RWTexture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadOnlyTexture3D`1" => $"Texture3D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadWriteTexture3D`1" => $"RWTexture3D<unorm {mappedElementType}>",
|
||||
_ => throw new ArgumentException()
|
||||
};
|
||||
}
|
||||
|
||||
// The captured field is of an HLSL primitive type
|
||||
if (KnownHlslTypes.TryGetValue(typeName, out string? mappedType))
|
||||
{
|
||||
return mappedType;
|
||||
}
|
||||
|
||||
// The captured field is of a custom struct type
|
||||
return typeName.ToHlslIdentifierName();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the output texture of a pixel shader.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The pixel shader type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static string GetMappedNameForPixelShaderType(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.First()).GetFullMetadataName();
|
||||
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullMetadataName();
|
||||
|
||||
// If the current type is a custom type, format it as needed
|
||||
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType))
|
||||
@@ -312,124 +264,171 @@ namespace ComputeSharp.SourceGenerators.Mappings
|
||||
}
|
||||
|
||||
// Construct the HLSL type name
|
||||
return $"RWTexture2D<unorm {mappedElementType}>";
|
||||
return typeName switch
|
||||
{
|
||||
"ComputeSharp.ConstantBuffer`1" => mappedElementType,
|
||||
"ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture2D`2" => $"Texture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture2D`2" => $"RWTexture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadOnlyTexture3D`2" => $"Texture3D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mappedElementType}>",
|
||||
"ComputeSharp.ReadWriteTexture3D`2" => $"RWTexture3D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadOnlyTexture2D`1" => $"Texture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadWriteTexture2D`1" => $"RWTexture2D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadOnlyTexture3D`1" => $"Texture3D<unorm {mappedElementType}>",
|
||||
"ComputeSharp.IReadWriteTexture3D`1" => $"RWTexture3D<unorm {mappedElementType}>",
|
||||
_ => throw new ArgumentException()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input element type symbol.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The input type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static string GetMappedElementName(IArrayTypeSymbol typeSymbol)
|
||||
// The captured field is of an HLSL primitive type
|
||||
if (KnownHlslTypes.TryGetValue(typeName, out string? mappedType))
|
||||
{
|
||||
string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName();
|
||||
return mappedType;
|
||||
}
|
||||
|
||||
if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
|
||||
// The captured field is of a custom struct type
|
||||
return typeName.ToHlslIdentifierName();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the output texture of a pixel shader.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The pixel shader type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static string GetMappedNameForPixelShaderType(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.First()).GetFullMetadataName();
|
||||
|
||||
// If the current type is a custom type, format it as needed
|
||||
if (!KnownHlslTypes.TryGetValue(genericArgumentName, out string? mappedElementType))
|
||||
{
|
||||
mappedElementType = genericArgumentName.ToHlslIdentifierName();
|
||||
}
|
||||
|
||||
// Construct the HLSL type name
|
||||
return $"RWTexture2D<unorm {mappedElementType}>";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input element type symbol.
|
||||
/// </summary>
|
||||
/// <param name="typeSymbol">The input type to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
[Pure]
|
||||
public static string GetMappedElementName(IArrayTypeSymbol typeSymbol)
|
||||
{
|
||||
string elementTypeName = ((INamedTypeSymbol)typeSymbol.ElementType).GetFullMetadataName();
|
||||
|
||||
if (KnownHlslTypes.TryGetValue(elementTypeName, out string? mapped))
|
||||
{
|
||||
return mapped;
|
||||
}
|
||||
|
||||
return elementTypeName.ToHlslIdentifierName();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input type name to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
public static bool TryGetMappedName(string originalName, out string? mappedName)
|
||||
{
|
||||
return KnownHlslTypes.TryGetValue(originalName, out mappedName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sequence of unique custom types from a collection of discovered types.
|
||||
/// </summary>
|
||||
/// <param name="discoveredTypes">The input collection of discovered types.</param>
|
||||
/// <returns>The list of unique custom types.</returns>
|
||||
public static IEnumerable<INamedTypeSymbol> GetCustomTypes(IEnumerable<INamedTypeSymbol> discoveredTypes)
|
||||
{
|
||||
// Local function to recursively gather nested types
|
||||
static void ExploreTypes(INamedTypeSymbol type, HashSet<INamedTypeSymbol> customTypes)
|
||||
{
|
||||
if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
|
||||
|
||||
if (!customTypes.Add(type)) return;
|
||||
|
||||
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
|
||||
{
|
||||
return mapped;
|
||||
if (field.IsStatic) continue;
|
||||
|
||||
ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
|
||||
}
|
||||
|
||||
return elementTypeName.ToHlslIdentifierName();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapped HLSL-compatible type name for the input type name.
|
||||
/// </summary>
|
||||
/// <param name="name">The input type name to map.</param>
|
||||
/// <returns>The HLSL-compatible type name that can be used in an HLSL shader.</returns>
|
||||
public static bool TryGetMappedName(string originalName, out string? mappedName)
|
||||
HashSet<INamedTypeSymbol> customTypes = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Explore all input types and their nested types
|
||||
foreach (INamedTypeSymbol type in discoveredTypes)
|
||||
{
|
||||
return KnownHlslTypes.TryGetValue(originalName, out mappedName);
|
||||
ExploreTypes(type, customTypes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sequence of unique custom types from a collection of discovered types.
|
||||
/// </summary>
|
||||
/// <param name="discoveredTypes">The input collection of discovered types.</param>
|
||||
/// <returns>The list of unique custom types.</returns>
|
||||
public static IEnumerable<INamedTypeSymbol> GetCustomTypes(IEnumerable<INamedTypeSymbol> discoveredTypes)
|
||||
return OrderByDependency(customTypes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Orders the input sequence of types so that they can be declared in HLSL successfully.
|
||||
/// </summary>
|
||||
/// <param name="types">The input collection of types to declare.</param>
|
||||
/// <returns>The same list as input, but in a valid HLSL declaration order.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<INamedTypeSymbol> OrderByDependency(IEnumerable<INamedTypeSymbol> types)
|
||||
{
|
||||
Queue<(INamedTypeSymbol Type, HashSet<INamedTypeSymbol> Fields)> queue = new();
|
||||
|
||||
// Build a mapping of type dependencies for all the captured types. A type depends on another
|
||||
// when the latter is a field in the first type. HLSL requires custom types to be declared in
|
||||
// order of usage, so we need to ensure that types are declared in an order that guarantees
|
||||
// that no type will be referenced before being defined. To do so, we can create a mapping of
|
||||
// types and their dependencies, and iteratively remove items from the map when they have no
|
||||
// dependencies left. When one type is processed and removed, it is also removed from the list
|
||||
// of dependencies of all other remaining types in the map, until there is none left.
|
||||
foreach (var type in types)
|
||||
{
|
||||
// Local function to recursively gather nested types
|
||||
static void ExploreTypes(INamedTypeSymbol type, HashSet<INamedTypeSymbol> customTypes)
|
||||
HashSet<INamedTypeSymbol> dependencies = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Only add other custom types as dependencies, and ignore HLSL types
|
||||
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
|
||||
{
|
||||
if (KnownHlslTypes.ContainsKey(type.GetFullMetadataName())) return;
|
||||
if (field.IsStatic) continue;
|
||||
|
||||
if (!customTypes.Add(type)) return;
|
||||
INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
|
||||
|
||||
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
|
||||
if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
|
||||
{
|
||||
if (field.IsStatic) continue;
|
||||
|
||||
ExploreTypes((INamedTypeSymbol)field.Type, customTypes);
|
||||
_ = dependencies.Add(fieldType);
|
||||
}
|
||||
}
|
||||
|
||||
HashSet<INamedTypeSymbol> customTypes = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Explore all input types and their nested types
|
||||
foreach (INamedTypeSymbol type in discoveredTypes)
|
||||
{
|
||||
ExploreTypes(type, customTypes);
|
||||
}
|
||||
|
||||
return OrderByDependency(customTypes);
|
||||
queue.Enqueue((type, dependencies));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Orders the input sequence of types so that they can be declared in HLSL successfully.
|
||||
/// </summary>
|
||||
/// <param name="types">The input collection of types to declare.</param>
|
||||
/// <returns>The same list as input, but in a valid HLSL declaration order.</returns>
|
||||
[Pure]
|
||||
private static IEnumerable<INamedTypeSymbol> OrderByDependency(IEnumerable<INamedTypeSymbol> types)
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
Queue<(INamedTypeSymbol Type, HashSet<INamedTypeSymbol> Fields)> queue = new();
|
||||
var entry = queue.Dequeue();
|
||||
|
||||
// Build a mapping of type dependencies for all the captured types. A type depends on another
|
||||
// when the latter is a field in the first type. HLSL requires custom types to be declared in
|
||||
// order of usage, so we need to ensure that types are declared in an order that guarantees
|
||||
// that no type will be referenced before being defined. To do so, we can create a mapping of
|
||||
// types and their dependencies, and iteratively remove items from the map when they have no
|
||||
// dependencies left. When one type is processed and removed, it is also removed from the list
|
||||
// of dependencies of all other remaining types in the map, until there is none left.
|
||||
foreach (var type in types)
|
||||
// No dependencies left, we can declare this type
|
||||
if (entry.Fields.Count == 0)
|
||||
{
|
||||
HashSet<INamedTypeSymbol> dependencies = new(SymbolEqualityComparer.Default);
|
||||
|
||||
// Only add other custom types as dependencies, and ignore HLSL types
|
||||
foreach (var field in type.GetMembers().OfType<IFieldSymbol>())
|
||||
// Remove the current type from dependencies of others
|
||||
foreach (var pair in queue)
|
||||
{
|
||||
if (field.IsStatic) continue;
|
||||
|
||||
INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type;
|
||||
|
||||
if (!KnownHlslTypes.ContainsKey(fieldType.GetFullMetadataName()))
|
||||
{
|
||||
_ = dependencies.Add(fieldType);
|
||||
}
|
||||
_ = pair.Fields.Remove(entry.Type);
|
||||
}
|
||||
|
||||
queue.Enqueue((type, dependencies));
|
||||
}
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var entry = queue.Dequeue();
|
||||
|
||||
// No dependencies left, we can declare this type
|
||||
if (entry.Fields.Count == 0)
|
||||
{
|
||||
// Remove the current type from dependencies of others
|
||||
foreach (var pair in queue)
|
||||
{
|
||||
_ = pair.Fields.Remove(entry.Type);
|
||||
}
|
||||
|
||||
yield return entry.Type;
|
||||
}
|
||||
else queue.Enqueue(entry);
|
||||
yield return entry.Type;
|
||||
}
|
||||
else queue.Enqueue(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする