diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 1294657b..6ccf0f91 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -228,6 +228,11 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter if (fieldOperation.Field.IsConst && fieldOperation.Type!.TypeKind != TypeKind.Enum) { + if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral)) + { + return ParseExpression(constantLiteral!); + } + ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index 5d27d5a5..56623724 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -58,6 +58,11 @@ internal sealed class StaticFieldRewriter : HlslSourceRewriter fieldOperation.Field.IsConst && fieldOperation.Type!.TypeKind != TypeKind.Enum) { + if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral)) + { + return ParseExpression(constantLiteral!); + } + ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index 7ed7d69a..f41ade8b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -11,6 +11,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs new file mode 100644 index 00000000..7728ab5b --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownFields.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; + +namespace ComputeSharp.SourceGeneration.Mappings; + +/// +/// A that contains and maps known HLSL field names to common .NET fields. +/// +internal static partial class HlslKnownFields +{ + /// + /// The mapping of supported known fields to HLSL names. + /// + private static readonly IReadOnlyDictionary KnownFields = BuildKnownFieldsMap(); + + /// + /// Builds the mapping of supported known fields to HLSL names. + /// + private static IReadOnlyDictionary BuildKnownFieldsMap() + { + Dictionary knownMembers = new() + { + [$"float.{nameof(float.NaN)}"] = "asfloat(0xFFC00000)", + [$"float.{nameof(float.PositiveInfinity)}"] = "asfloat(0x7F800000)", + [$"float.{nameof(float.NegativeInfinity)}"] = "asfloat(0xFF800000)", + + [$"double.{nameof(double.NaN)}"] = "asdouble(0x00000000, 0xFFF80000)", + [$"double.{nameof(double.PositiveInfinity)}"] = "asdouble(0x00000000, 0x7FF00000)", + [$"double.{nameof(double.NegativeInfinity)}"] = "asdouble(0x00000000, 0xFFF00000)" + }; + + return knownMembers; + } + + /// + /// Tries to get the mapped HLSL-compatible field name for the input field name. + /// + /// The input fully qualified field name. + /// The mapped name, if one is found. + /// The HLSL-compatible field name that can be used in an HLSL shader. + public static bool TryGetMappedName(string name, out string? mapped) + { + return KnownFields.TryGetValue(name, out mapped); + } +} diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs index 60688557..699fbb2f 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownProperties.cs @@ -9,7 +9,7 @@ using System.Text.RegularExpressions; namespace ComputeSharp.SourceGeneration.Mappings; /// -/// A that contains and maps known HLSL properties names to common .NET members. +/// A that contains and maps known HLSL properties names to common .NET properties. /// internal static partial class HlslKnownProperties { diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index f3df845c..f005e1ac 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -260,6 +260,11 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter if (fieldOperation.Field.IsConst && fieldOperation.Type!.TypeKind != TypeKind.Enum) { + if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral)) + { + return ParseExpression(constantLiteral!); + } + ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs index 04888a4d..939f8b79 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs @@ -59,6 +59,11 @@ internal sealed class StaticFieldRewriter : HlslSourceRewriter fieldOperation.Field.IsConst && fieldOperation.Type!.TypeKind != TypeKind.Enum) { + if (HlslKnownFields.TryGetMappedName(fieldOperation.Member.ToDisplayString(), out string? constantLiteral)) + { + return ParseExpression(constantLiteral!); + } + ConstantDefinitions[fieldOperation.Field] = ((IFormattable)fieldOperation.Field.ConstantValue!).ToString(null, CultureInfo.InvariantCulture); var ownerTypeName = ((INamedTypeSymbol)fieldOperation.Field.ContainingSymbol).ToDisplayString().ToHlslIdentifierName(); diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs new file mode 100644 index 00000000..741a05fb --- /dev/null +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -0,0 +1,85 @@ +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests; + +[TestClass] +[TestCategory("ShaderRewriter")] +public partial class ShaderRewriterTests +{ + [CombinatorialTestMethod] + [AllDevices] + public void NanAndInfinite(Device device) + { + if (!device.Get().IsDoublePrecisionSupportAvailable()) + { + Assert.Inconclusive(); + } + + using ReadWriteBuffer buffer1 = device.Get().AllocateReadWriteBuffer(16); + using ReadWriteBuffer buffer2 = device.Get().AllocateReadWriteBuffer(16); + + device.Get().For(1, new NanAndInfiniteShader(buffer1, buffer2)); + + float[] results1 = buffer1.ToArray(); + double[] results2 = buffer2.ToArray(); + + Assert.IsTrue(float.IsNaN(results1[0])); + Assert.IsTrue(float.IsPositiveInfinity(results1[1])); + Assert.IsTrue(float.IsNegativeInfinity(results1[2])); + Assert.AreEqual(results1[3], 1); + Assert.AreEqual(results1[4], 1); + Assert.AreEqual(results1[5], 1); + Assert.IsTrue(float.IsNaN(results1[6])); + Assert.IsTrue(float.IsPositiveInfinity(results1[7])); + Assert.IsTrue(float.IsNegativeInfinity(results1[8])); + Assert.AreEqual(results1[9], 1); + Assert.AreEqual(results1[10], 1); + Assert.AreEqual(results1[11], 1); + + Assert.IsTrue(double.IsNaN(results2[0])); + Assert.IsTrue(double.IsPositiveInfinity(results2[1])); + Assert.IsTrue(double.IsNegativeInfinity(results2[2])); + Assert.IsTrue(double.IsNaN(results2[3])); + Assert.IsTrue(double.IsPositiveInfinity(results2[4])); + Assert.IsTrue(double.IsNegativeInfinity(results2[5])); + } + + [AutoConstructor] + internal readonly partial struct NanAndInfiniteShader : IComputeShader + { + public readonly ReadWriteBuffer buffer1; + public readonly ReadWriteBuffer buffer2; + + static readonly float float1 = float.NaN; + static readonly float float2 = float.PositiveInfinity; + static readonly float float3 = float.NegativeInfinity; + static readonly double double1 = double.NaN; + static readonly double double2 = double.PositiveInfinity; + static readonly double double3 = double.NegativeInfinity; + + public void Execute() + { + buffer1[0] = float.NaN; + buffer1[1] = float.PositiveInfinity; + buffer1[2] = float.NegativeInfinity; + buffer1[3] = Hlsl.IsNaN(buffer1[0]) ? 1 : 0; + buffer1[4] = Hlsl.IsInfinite(buffer1[1]) ? 1 : 0; + buffer1[5] = Hlsl.IsInfinite(buffer1[2]) ? 1 : 0; + buffer1[6] = float1; + buffer1[7] = float2; + buffer1[8] = float3; + buffer1[9] = Hlsl.IsNaN(buffer1[6]) ? 1 : 0; + buffer1[10] = Hlsl.IsInfinite(buffer1[7]) ? 1 : 0; + buffer1[11] = Hlsl.IsInfinite(buffer1[8]) ? 1 : 0; + + buffer2[0] = double.NaN; + buffer2[1] = double.PositiveInfinity; + buffer2[2] = double.NegativeInfinity; + buffer2[3] = double1; + buffer2[4] = double2; + buffer2[5] = double3; + } + } +}