diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index a1ce59a5..84734904 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -196,7 +196,7 @@ internal abstract partial class HlslSourceRewriter( } /// - public sealed override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node) + public sealed unsafe override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node) { CancellationToken.ThrowIfCancellationRequested(); @@ -219,6 +219,7 @@ internal abstract partial class HlslSourceRewriter( // in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix. if (type.SpecialType == SpecialType.System_Single) { +#if D3D12_SOURCE_GENERATOR string literal = updatedNode.Token.ValueText; // If the numeric literal is neither a decimal nor an exponential, add the ".0" suffix @@ -228,6 +229,25 @@ internal abstract partial class HlslSourceRewriter( } return updatedNode.WithToken(Literal(literal, 0f)); +#else + // When compiling for D2D, we need to emit 'asfloat' expressions to work around an FXC + // bug that can cause it to sometimes emit incorrect values for float literals. For + // additional context, see: https://github.com/Sergio0694/ComputeSharp/issues/780. + // + // This code rewrites expressions as follows: + // C#: 3.14f + // HLSL: asfloat(1078523331) + float literalValue = (float)operation.ConstantValue.Value!; + uint literalValueAsUInt = *(uint*)&literalValue; + + return + InvocationExpression(IdentifierName("asfloat")) + .AddArgumentListArguments( + Argument( + LiteralExpression( + SyntaxKind.NumericLiteralExpression, + Literal(literalValueAsUInt)))); +#endif } else if (type.SpecialType == SpecialType.System_Double) { diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index 43ef4515..91348cec 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -186,8 +186,8 @@ public class Test_D2DPixelShaderDescriptorGenerator { int2 xy = (int2)D2DGetScenePosition().xy; float2 uv = xy / (float2)dispatchSize; - float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); - return float4(col, 1.0); + float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); + return float4(col, asfloat(1065353216U)); } """; @@ -482,8 +482,8 @@ public class Test_D2DPixelShaderDescriptorGenerator { int2 xy = (int2)D2DGetScenePosition().xy; float2 uv = xy / (float2)dispatchSize; - float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); - return float4(col, 1.0); + float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); + return float4(col, asfloat(1065353216U)); } """; @@ -830,8 +830,8 @@ public class Test_D2DPixelShaderDescriptorGenerator { int2 xy = (int2)D2DGetScenePosition().xy; float2 uv = xy / (float2)dispatchSize; - float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); - return float4(col, 1.0); + float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); + return float4(col, asfloat(1065353216U)); } """; @@ -1124,8 +1124,8 @@ public class Test_D2DPixelShaderDescriptorGenerator { int2 xy = (int2)D2DGetScenePosition().xy; float2 uv = xy / (float2)dispatchSize; - float3 col = 0.5 + (0.5 * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); - return float4(col, 1.0); + float3 col = asfloat(1056964608U) + (asfloat(1056964608U) * cos(time + float3(uv, uv.x) + float3(0, 2, 4))); + return float4(col, asfloat(1065353216U)); } """; diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/Green32x32.png b/tests/ComputeSharp.D2D1.Tests/Assets/Green32x32.png new file mode 100644 index 00000000..7cacfc9d Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/Green32x32.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/TerracedHills.png b/tests/ComputeSharp.D2D1.Tests/Assets/TerracedHills.png new file mode 100644 index 00000000..8a58406a Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/TerracedHills.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 6b7b3491..b8c4ad58 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -90,12 +90,15 @@ PreserveNewest - + PreserveNewest PreserveNewest + + PreserveNewest + diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index d9fd2adf..a447701d 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -42,7 +42,7 @@ public partial class D2D1ReflectionServicesTests float4 input1 = D2DGetInput(1); float4 input2 = D2DSampleInputAtPosition(2, xy + offset); float value3 = __reserved__buffer[0]; - float4 value4 = __reserved__texture.Sample(__sampler____reserved__texture, float2(0, 0.5)); + float4 value4 = __reserved__texture.Sample(__sampler____reserved__texture, float2(0, asfloat(1056964608U))); return input0 + input1 + input2 + value3 + value4; } """, shaderInfo.HlslSource); diff --git a/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs index acc94bab..b1fab05a 100644 --- a/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs @@ -1,4 +1,5 @@ using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; #pragma warning disable CA1822 @@ -82,7 +83,7 @@ public partial class ShaderRewriterTests D2D_PS_ENTRY(Execute) { bool4 mask4 = bool4(true, false, true, true); - float4 float4_1 = float4(1, 2, 3.14, 4); + float4 float4_1 = float4(1, 2, asfloat(1078523331U), 4); float4 float4_2 = float4(5, 6, 7, 8); float4 float4_r = (mask4 ? float4_1 : float4_2); bool1x3 mask1x3 = bool1x3((bool)true, (bool)true, (bool)false); @@ -93,7 +94,7 @@ public partial class ShaderRewriterTests uint2x4 uint2x4_1 = uint2x4((uint)1, (uint)2, (uint)3, (uint)4, (uint)5, (uint)6, (uint)7, (uint)8); uint2x4 uint2x4_2 = uint2x4((uint)111, (uint)222, (uint)333, (uint)444, (uint)555, (uint)666, (uint)777, (uint)888); uint2x4 uint2x4_r = (mask2x4 ? uint2x4_1 : uint2x4_2); - float2x2 f2x2_r = mul((float2x4)uint2x4_r, float4x2((float2)float4_r.xy, (float2)float4_r.zw, (float2)float2(int1x3_r._m00, int1x3_r._m01), (float2)float2(int1x3_r._m02, 1.0))); + float2x2 f2x2_r = mul((float2x4)uint2x4_r, float4x2((float2)float4_r.xy, (float2)float4_r.zw, (float2)float2(int1x3_r._m00, int1x3_r._m01), (float2)float2(int1x3_r._m02, asfloat(1065353216U)))); return float4(f2x2_r._m00, f2x2_r._m01, f2x2_r._m10, f2x2_r._m11); } """, shaderInfo.HlslSource); @@ -179,4 +180,32 @@ public partial class ShaderRewriterTests mask2x3_r_or.M11 ? 1 : 0); } } + + // See https://github.com/Sergio0694/ComputeSharp/issues/780 + [TestMethod] + public void ProblematicFloatLiteralValue_IsRewrittenCorrectly() + { + D2D1TestRunner.RunAndCompareShader( + new ProblematicFloatLiteralValueShader(131072.65f), + 32, + 32, + "Green32x32.png"); + } + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DOutputBuffer(D2D1BufferPrecision.Float32)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ProblematicFloatLiteralValueShader(float x) : ID2D1PixelShader + { + public float4 Execute() + { + if (x != 131072.65f) + { + return 0; + } + + return new float4(0.0f, 1.0f, 0.0f, 1.0f); + } + } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs index 3751a4aa..b3c59e01 100644 --- a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs @@ -110,7 +110,7 @@ public class ShadersTests [TestMethod] public void TerracedHills() { - RunTest(0.000026f); + RunTest(0.000027f); } private static void RunTest(float threshold = 0.00001f) diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj b/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj index d0cd1fc4..133ab310 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/ComputeSharp.D2D1.WinUI.Tests.csproj @@ -42,7 +42,7 @@ Assets\Shaders\ProteanClouds.png PreserveNewest - + Assets\Shaders\TerracedHills.png PreserveNewest diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/ShadersTests.cs index 756726ea..98854516 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests/Tests/ShadersTests.cs @@ -68,7 +68,7 @@ public class ShadersTests [DataRow(WrapperType.CanvasEffect)] public async Task TerracedHills(WrapperType wrapperType) { - await RunTestAsync(wrapperType, 0.000026f); + await RunTestAsync(wrapperType, 0.000027f); } private static async Task RunTestAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(WrapperType wrapperType, float threshold = 0.00001f)