From f2c9478631e83b55c39e4c8de1d019afa6b392d6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 May 2022 17:19:52 +0200 Subject: [PATCH 1/4] Fix rewriting of integer double literals --- .../SyntaxRewriters/HlslSourceRewriter.cs | 19 ++++++++++++++++--- .../SyntaxRewriters/HlslSourceRewriter.cs | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index 83e1cecd..0e43e9f7 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -190,7 +190,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // used in the HLSL representation. Floating point values accept either f or F, but they don't work // when the literal doesn't contain a decimal point. Since 32 bit floating point values are the default // in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix. - if (type.GetFullMetadataName().Equals(typeof(float).FullName)) + if (type.SpecialType == SpecialType.System_Single) { string literal = updatedNode.Token.ValueText; @@ -202,9 +202,22 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter return updatedNode.WithToken(Literal(literal, 0f)); } - else if (type.GetFullMetadataName().Equals(typeof(double).FullName)) + else if (type.SpecialType == SpecialType.System_Double) { - return updatedNode.WithToken(Literal(updatedNode.Token.ValueText + "L", 0d)); + string literal = updatedNode.Token.ValueText; + + // If the numeric literal is neither a decimal nor an exponential, add the ".0L" suffix. + // This is necessary because otherwise integer literals would actually be of type long. + if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + { + literal += ".0L"; + } + else + { + literal += "L"; + } + + return updatedNode.WithToken(Literal(literal, 0d)); } } else if (updatedNode.IsKind(SyntaxKind.StringLiteralExpression)) diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index b34408c7..757b8f33 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -195,7 +195,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // used in the HLSL representation. Floating point values accept either f or F, but they don't work // when the literal doesn't contain a decimal point. Since 32 bit floating point values are the default // in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix. - if (type.GetFullMetadataName().Equals(typeof(float).FullName)) + if (type.SpecialType == SpecialType.System_Single) { string literal = updatedNode.Token.ValueText; @@ -207,9 +207,22 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter return updatedNode.WithToken(Literal(literal, 0f)); } - else if (type.GetFullMetadataName().Equals(typeof(double).FullName)) + else if (type.SpecialType == SpecialType.System_Double) { - return updatedNode.WithToken(Literal(updatedNode.Token.ValueText + "L", 0d)); + string literal = updatedNode.Token.ValueText; + + // If the numeric literal is neither a decimal nor an exponential, add the ".0L" suffix. + // This is necessary because otherwise integer literals would actually be of type long. + if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + { + literal += ".0L"; + } + else + { + literal += "L"; + } + + return updatedNode.WithToken(Literal(literal, 0d)); } } else if (updatedNode.IsKind(SyntaxKind.StringLiteralExpression)) From 6c4ae61fbef8e8a34727e447a80677a1014be6b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 May 2022 17:43:06 +0200 Subject: [PATCH 2/4] Add unit tests for double literals handling --- .../ComputeSharp.Tests/ShaderRewriterTests.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs index bd957392..a07e6999 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -178,4 +178,69 @@ public partial class ShaderRewriterTests buffer[5] = lerp; } } + + // See https://github.com/Sergio0694/ComputeSharp/issues/278 + [CombinatorialTestMethod] + [AllDevices] + public void DoubleConstantsInShaderConstantFields(Device device) + { + if (!device.Get().IsDoublePrecisionSupportAvailable()) + { + Assert.Inconclusive(); + } + + using ReadWriteBuffer buffer1 = device.Get().AllocateReadWriteBuffer(4); + using ReadWriteBuffer buffer2 = device.Get().AllocateReadWriteBuffer(4); + + device.Get().For(1, new DoubleConstantsInShaderConstantFieldsShader(buffer1, buffer2)); + + float[] results1 = buffer1.ToArray(); + + Assert.AreEqual(results1[0], DoubleConstantsInShaderConstantFieldsShader.DoubleToFloatConstant, 0.0001f); + Assert.AreEqual(results1[1], DoubleConstantsInShaderConstantFieldsShader.DoubleToFloatField, 0.0001f); + Assert.AreEqual(results1[2], DoubleConstants.PI, 0.0001f); + Assert.AreEqual(results1[3], (float)(1 / 255.0), 0.0001f); + + double[] results2 = buffer2.ToArray(); + + Assert.AreEqual(results2[0], DoubleConstantsInShaderConstantFieldsShader.DoubleConstant, 0.0001f); + Assert.AreEqual(results2[1], DoubleConstantsInShaderConstantFieldsShader.DoubleField, 0.0001f); + Assert.AreEqual(results2[2], DoubleConstants.PI2, 0.0001f); + Assert.AreEqual(results2[3], 1 / 255.0, 0.0001f); + } + + private static class DoubleConstants + { + public const float PI = (float)3.14; + public const double PI2 = 3.14 * 2; + } + + [AutoConstructor] + internal readonly partial struct DoubleConstantsInShaderConstantFieldsShader : IComputeShader + { + public const float DoubleToFloatConstant = (float)(1 / 255.0); + public const double DoubleConstant = 1.0 / 255; + + public static readonly float DoubleToFloatField = (float)(1D / 255); + public static readonly double DoubleField = 1 / 255D; + + public readonly ReadWriteBuffer buffer1; + public readonly ReadWriteBuffer buffer2; + + public void Execute() + { + const float localFloat = (float)(1 / 255.0); + const double localDouble = 1.0 / 255; + + buffer1[0] = DoubleToFloatConstant; + buffer1[1] = DoubleToFloatField; + buffer1[2] = DoubleConstants.PI; + buffer1[3] = localFloat; + + buffer2[0] = DoubleConstant; + buffer2[1] = DoubleField; + buffer2[2] = DoubleConstants.PI2; + buffer2[3] = localDouble; + } + } } From f66342d21256b7c5a24164198a25a7705101579d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 May 2022 23:14:25 +0200 Subject: [PATCH 3/4] Minor optimization to float literal rewriting --- .../SyntaxRewriters/HlslSourceRewriter.cs | 9 +++++++-- .../SyntaxRewriters/HlslSourceRewriter.cs | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index 0e43e9f7..af78bc83 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -21,6 +21,11 @@ namespace ComputeSharp.D2D1.SourceGenerators.SyntaxRewriters; /// internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter { + /// + /// An array with the '.' and 'E' characters. + /// + private static readonly char[] FloatLiteralSpecialCharacters = { '.', 'E' }; + /// /// The instance with semantic info on the target syntax tree. /// @@ -195,7 +200,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter string literal = updatedNode.Token.ValueText; // If the numeric literal is neither a decimal nor an exponential, add the ".0" suffix - if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + if (literal.IndexOfAny(FloatLiteralSpecialCharacters) == -1) { literal += ".0"; } @@ -208,7 +213,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // If the numeric literal is neither a decimal nor an exponential, add the ".0L" suffix. // This is necessary because otherwise integer literals would actually be of type long. - if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + if (literal.IndexOfAny(FloatLiteralSpecialCharacters) == -1) { literal += ".0L"; } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index 757b8f33..215daa8b 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -21,6 +21,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters; /// internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter { + /// + /// An array with the '.' and 'E' characters. + /// + private static readonly char[] FloatLiteralSpecialCharacters = { '.', 'E' }; + /// /// The instance with semantic info on the target syntax tree. /// @@ -200,7 +205,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter string literal = updatedNode.Token.ValueText; // If the numeric literal is neither a decimal nor an exponential, add the ".0" suffix - if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + if (literal.IndexOfAny(FloatLiteralSpecialCharacters) == -1) { literal += ".0"; } @@ -213,7 +218,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // If the numeric literal is neither a decimal nor an exponential, add the ".0L" suffix. // This is necessary because otherwise integer literals would actually be of type long. - if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + if (literal.IndexOfAny(FloatLiteralSpecialCharacters) == -1) { literal += ".0L"; } From f0c64337c6336641642dd032c804c887fa77c89f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 May 2022 23:41:21 +0200 Subject: [PATCH 4/4] Add unit tests for float literals with lowercase 'e' --- .../ComputeSharp.Tests/ShaderRewriterTests.cs | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs index a07e6999..ebb3db4a 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -147,6 +147,10 @@ public partial class ShaderRewriterTests Assert.AreEqual(results[3], -(float)Math.Sin((float)(137.2 / 180.0 * Constants.PI2)), 0.0001f); Assert.AreEqual(results[4], (float)Math.Cos(Math.Sin(ConstantsInShaderConstantFieldsShader.sin)), 0.0001f); Assert.AreEqual(results[5], (float)(Math.Sin(ConstantsInShaderConstantFieldsShader.sin) + Math.Cos(Math.Sin(ConstantsInShaderConstantFieldsShader.sin))), 0.0001f); + Assert.AreEqual(results[6], ConstantsInShaderConstantFieldsShader.exponentLowercase, 0.0001f); + Assert.AreEqual(results[7], ConstantsInShaderConstantFieldsShader.exponentUppercase, 0.0001f); + Assert.AreEqual(results[8], ConstantsInShaderConstantFieldsShader.exponentLowercaseField, 0.0001f); + Assert.AreEqual(results[9], ConstantsInShaderConstantFieldsShader.exponentUppercaseField, 0.0001f); } private static class Constants @@ -160,11 +164,15 @@ public partial class ShaderRewriterTests public const float rot_angle = (float)(137.2 / 180.0 * Math.PI); public const float rot_angle2 = rot_angle + (float)Math.E * 100; public const float sin = 42; + public const float exponentLowercase = 1234567e-4f; + public const float exponentUppercase = 1234567E-4f; private static readonly float rot_11 = Hlsl.Cos(rot_angle); private static readonly float rot_12 = -Hlsl.Sin((float)(137.2 / 180.0 * Constants.PI2)); private static readonly float cos = Hlsl.Cos(Hlsl.Sin(sin)); private static readonly float lerp = Hlsl.Sin(sin) + cos; + public static readonly float exponentLowercaseField = 1234567e-4f; + public static readonly float exponentUppercaseField = 1234567E-4f; public readonly ReadWriteBuffer buffer; @@ -176,6 +184,10 @@ public partial class ShaderRewriterTests buffer[3] = rot_12; buffer[4] = cos; buffer[5] = lerp; + buffer[6] = exponentLowercase; + buffer[7] = exponentUppercase; + buffer[8] = exponentLowercaseField; + buffer[9] = exponentUppercaseField; } } @@ -189,8 +201,8 @@ public partial class ShaderRewriterTests Assert.Inconclusive(); } - using ReadWriteBuffer buffer1 = device.Get().AllocateReadWriteBuffer(4); - using ReadWriteBuffer buffer2 = device.Get().AllocateReadWriteBuffer(4); + using ReadWriteBuffer buffer1 = device.Get().AllocateReadWriteBuffer(8); + using ReadWriteBuffer buffer2 = device.Get().AllocateReadWriteBuffer(8); device.Get().For(1, new DoubleConstantsInShaderConstantFieldsShader(buffer1, buffer2)); @@ -200,6 +212,10 @@ public partial class ShaderRewriterTests Assert.AreEqual(results1[1], DoubleConstantsInShaderConstantFieldsShader.DoubleToFloatField, 0.0001f); Assert.AreEqual(results1[2], DoubleConstants.PI, 0.0001f); Assert.AreEqual(results1[3], (float)(1 / 255.0), 0.0001f); + Assert.AreEqual(results1[4], DoubleConstantsInShaderConstantFieldsShader.floatExponentLowercase, 0.0001f); + Assert.AreEqual(results1[5], DoubleConstantsInShaderConstantFieldsShader.floatExponentUppercase, 0.0001f); + Assert.AreEqual(results1[6], DoubleConstantsInShaderConstantFieldsShader.floatExponentLowercaseField, 0.0001f); + Assert.AreEqual(results1[7], DoubleConstantsInShaderConstantFieldsShader.floatExponentUppercaseField, 0.0001f); double[] results2 = buffer2.ToArray(); @@ -207,6 +223,10 @@ public partial class ShaderRewriterTests Assert.AreEqual(results2[1], DoubleConstantsInShaderConstantFieldsShader.DoubleField, 0.0001f); Assert.AreEqual(results2[2], DoubleConstants.PI2, 0.0001f); Assert.AreEqual(results2[3], 1 / 255.0, 0.0001f); + Assert.AreEqual(results2[4], DoubleConstantsInShaderConstantFieldsShader.exponentLowercase, 0.0001f); + Assert.AreEqual(results2[5], DoubleConstantsInShaderConstantFieldsShader.exponentUppercase, 0.0001f); + Assert.AreEqual(results2[6], DoubleConstantsInShaderConstantFieldsShader.exponentLowercaseField, 0.0001f); + Assert.AreEqual(results2[7], DoubleConstantsInShaderConstantFieldsShader.exponentUppercaseField, 0.0001f); } private static class DoubleConstants @@ -220,9 +240,17 @@ public partial class ShaderRewriterTests { public const float DoubleToFloatConstant = (float)(1 / 255.0); public const double DoubleConstant = 1.0 / 255; + public const float floatExponentLowercase = (float)1234567e-4; + public const float floatExponentUppercase = (float)1234567E-4; + public const double exponentLowercase = 1234567e-4; + public const double exponentUppercase = 1234567E-4; public static readonly float DoubleToFloatField = (float)(1D / 255); public static readonly double DoubleField = 1 / 255D; + public static readonly float floatExponentLowercaseField = (float)1234567e-4; + public static readonly float floatExponentUppercaseField = (float)1234567E-4; + public static readonly double exponentLowercaseField = 1234567e-4; + public static readonly double exponentUppercaseField = 1234567E-4; public readonly ReadWriteBuffer buffer1; public readonly ReadWriteBuffer buffer2; @@ -236,11 +264,19 @@ public partial class ShaderRewriterTests buffer1[1] = DoubleToFloatField; buffer1[2] = DoubleConstants.PI; buffer1[3] = localFloat; + buffer1[4] = floatExponentLowercase; + buffer1[5] = floatExponentUppercase; + buffer1[6] = floatExponentLowercaseField; + buffer1[7] = floatExponentUppercaseField; buffer2[0] = DoubleConstant; buffer2[1] = DoubleField; buffer2[2] = DoubleConstants.PI2; buffer2[3] = localDouble; + buffer2[4] = exponentLowercase; + buffer2[5] = exponentUppercase; + buffer2[6] = exponentLowercaseField; + buffer2[7] = exponentUppercaseField; } } }